Oggi mi è stato chiesto di preparare uno script per contare il numero di sessioni ICA attive su un server XenApp: la necessità era verificare che tutti i server della farm Citrix fossero utilizzati e bilanciati.
Citrix offre dei comodi snap-in per la PowerShell di Windows, utilizzabili inserendo il comando
Add-PSSnapin Citrix* |
all’interno del nostro script.
In particolare il cmdlet Get-XASession restituisce una collection di oggetti XASession e tra i suoi parametri è possibile specificare il nome del server di cui vogliamo ottenere le sessioni.
Filtrando per le sole sessioni con protocollo ICA e leggendo la proprietà Count è possibile ottenere il risultato voluto. Ecco lo script completo: se il numero di sessioni è pari a 0 viene generato un errore e il suo return code è impostato a 2 (per essere compatibile con il software di monitoraggio Nagios):
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!