Today I was asked for a script to count the number of active ICA sessions on a XenApp server: the needing was to check if all the servers in our Citrix farm were online and balanced.
Citrix developed useful snap-in for Windows PowerShell; you can use them inserting the command
Add-PSSnapin Citrix* |
in your scripts.
In particular, Get-XASession cmdlet returns a collection of XASession objects and it gets, as parameter, the server name you want to count sessions on.
With a filter for sessions with protocol ICA and reading Count property, you can get the result. Here’s the complete script: if session’s number is 0, an error is raised and script’s return code is set to 2 (for compatibility with Nagios monitoring tool):
Add-PSSnapin Citrix* if($args.Count -eq 0) { write-host "Usage: CountICASessions <serverName>" exit 1 } $sessions = Get-XASession -ServerName $args[0] $count = ($sessions | ? {$_.Protocol -eq "Ica"}).Count if($count -eq 0) { write-host "ERROR - No ICA sessions" exit 2 } write-host "OK - Active ICA sessions: $count" exit 0 |
This script is not working properly, in fact, in powershell, when an array have just one entry, it’s impossible to use the count method and also, you have to filter on the value of the session ID because there are some listener which is useless to count. So here a correction of this script : (i have added some modification which allow someone to use it with nagios and check_mk, that’s why the argument is not needed anymore)
Add-PSSnapin Citrix*
$hostname = get-content env:computername
$sessions = Get-XASession -Servername $hostname
$count = $sessions | where {$_.Protocol -eq “Ica” -and $_.SessionId -lt 65536} | measure-object
$count = $count.count
if ($count -lt 1) {
$count = 0
}
if($count -eq 0) {
write-host “0 _Xenapp_Session_Counter count=0 OK – Active ICA sessions: 0”
exit 2
}
write-host “0 _Xenapp_Session_Counter count=$count OK – Active ICA sessions: $count”
exit 0
Thanks!