Citrix session down data source

Citrix.PresentationServer.CitrixSessionDown.DataSource (DataSourceModuleType)

Provides for detection of Citrix sessions in a down state on a computer running Citrix Presentation Server

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource System.CommandExecuterPropertyBagSource Default

Overrideable Parameters:

IDParameterTypeSelector
IntervalInSecondsint$Config/IntervalInSeconds$
SecondsDownint$Config/SecondsDown$

Source Code:

<DataSourceModuleType ID="Citrix.PresentationServer.CitrixSessionDown.DataSource" Accessibility="Public" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="IntervalInSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SecondsDown" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalInSeconds" Selector="$Config/IntervalInSeconds$" ParameterType="int"/>
<OverrideableParameter ID="SecondsDown" Selector="$Config/SecondsDown$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="System!System.CommandExecuterPropertyBagSource">
<IntervalSeconds>$Config/IntervalInSeconds$</IntervalSeconds>
<ApplicationName>%windir%\system32\cscript.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>//d $file/CitrixSessionDown.js$ $Config/SecondsDown$</CommandLine>
<TimeoutSeconds>3600</TimeoutSeconds>
<RequireOutput>false</RequireOutput>
<Files>
<File>
<Name>CitrixSessionDown.js</Name>
<Contents><Script>
/*************************************************************************
*
*
*
*
* Copyright 2006 Citrix Systems, Inc. All Rights Reserved.
*
*************************************************************************/

var SCRIPT_NAME = "Citrix Session In Down State";
CITRIX_WMI_CONNECT = "WINMGMTS:{impersonationLevel=impersonate,authenticationLevel=pktPrivacy}\\\\.\\root\\Citrix";

// event constants
var EVENT_TYPE_SUCCESS = 0;
var EVENT_TYPE_ERROR = 1;
var EVENT_TYPE_WARNING = 2;
var EVENT_TYPE_INFORMATION = 4;

// Citrix WMI constants
var SESSION_DOWN = 8;

var SecondsDown;

if ( WScript.Arguments.Unnamed.Count == 1 )
{
SecondsDown = WScript.Arguments(0);
}
else
{
WScript.quit()
}

//Logs an error event to the event log
function logError(message, error)
{
var str = SCRIPT_NAME + ":\n" + message;

if (error)
{
var num = (error.number &lt; 0) ? (error.number + 0x100000000) : error.number;
var estr = error.description;

if (num == 0x80010105)
estr += " [RPC_E_SERVERFAULT: The server threw an exception.]";
else if (num == 0x80041001)
estr += " [WBEM_E_FAILED]";
else if (num == 0x80010108)
estr += " [RPC_E_DISCONNECTED]";
else if (num == 0x8007007e)
estr += " [ERROR_MOD_NOT_FOUND: The specified module could not be found.]";
else if (num == 0x80041010)
estr += " [WBEM_E_INVALID_CLASS]";
else if (num == 0x8004100e)
estr += " [WBEM_E_INVALID_NAMESPACE]";

if (estr == "")
estr = "&lt;error description not given&gt;";

str += ":\n\tReceived error: 0x" + num.toString(16) + ": " + estr;
}

var WshShell = new ActiveXObject("WScript.Shell");
WshShell.LogEvent(EVENT_TYPE_ERROR, str);
}

function Main()
{
try
{
var oWMIService = GetObject(CITRIX_WMI_CONNECT);
}
catch(ex)
{
logError("Error connecting to Citrix WMI service", ex);
return;
}

try
{
var oSessions = oWMIService.ExecQuery("SELECT * FROM MetaFrame_Session");
}
catch (ex)
{
logError("WMI error retrieving Citrix session array", ex);
return;
}

try
{
var down = new Array();
var e = new Enumerator( oSessions );
for (; !e.atEnd(); e.moveNext())
{
var session = e.item();
if (session.SessionState == SESSION_DOWN)
down[down.length] = session.SessionID;
}
}
catch (ex)
{
logError("WMI error retrieving Citrix session information", ex);
}

if (down.length == 0) {
return;
}

var isDown = new Array();

for (var i = 0; i &lt; down.length; i++)
{
isDown[ down[i] ] = true;
}

// wait for given number of seconds before rechecking to avoid
// alert for spurious 'down' sessions while resetting
try
{
WScript.Sleep(1000 * SecondsDown);
}
catch (ex)
{
logError("Failed to sleep for " + SecondsDown + " seconds", ex);
return;
}

try
{
var oAPI = new ActiveXObject("MOM.ScriptAPI");
var oBag = oAPI.CreatePropertyBag();
}
catch (ex)
{
logError("Could not create property bag", ex);
return;
}

// Do a second pass over sessions array to make find sessions that are still in a down state
try
{
var strSessions = "";
var e = new Enumerator( oSessions );
for (; !e.atEnd(); e.moveNext())
{
var session = e.item();
if (isDown[session.SessionID] &amp;&amp;
session.SessionState == SESSION_DOWN)
{
strSessions += session.SessionName + " (" + session.SessionID + ") ";
}
}
}
catch (ex)
{
logError("WMI error retrieving Citrix session information", ex);
}

oBag.AddValue("IsSessionDown", (strSessions != "") );
oBag.AddValue("DownSessions", strSessions);
oAPI.Return(oBag);

}

Main();
</Script></Contents>
<Unicode>true</Unicode>
</File>
</Files>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
</DataSourceModuleType>