Wednesday, February 3, 2016

How to monitor Citrix XenDesktop with PRTG (Part 2)

In part 1 of this series I wrote about how to extract data from the Citrix Desktop Delivery Controllers through remote PowerShell. Today I want to zoom in on the data returned from the Get-BrokerDesktop command and how to create useful sensors in PRTG to monitor a Citrix XenDesktop environment. As you can see in the documentation from the Get-BrokerDesktop command, there are actually a lot of useful parameters we can use to monitor the status of our desktops in Citrix XenDesktop. Let's start with a short recap:

# Get the Citrix Desktop Delivery Controller
$citrixddc = "YourCitrixDDCServer"

# Import module
if (-not (Get-module prtgshell)) {
    Import-Module prtgshell
    }

# Create a connection to the remote computer
$session = New-PSSession -ComputerName $citrixddc

# Run the commands on the remote computer
Invoke-command -Session $session -scriptblock {

    asnp Citrix*
    $result = Get-BrokerDesktop -MaxRecordCount 10000
} 

# Count the variable $result and print it to the variable $state.
$state = Invoke-Command -Session $session -ScriptBlock {$result}

To get a total of all desktops in Citrix XenDesktop:
$total = $state.count

If you want all the desktops in Citrix XenDesktops who are connected, you can do:
$connected = ($state | Where-Object {$_.SessionState -ilike "Active"} | Measure-Object -Property sessionstate).count

To get all the desktops in a disconnected state:
$disconnected = ($state | Where-Object {$_.SessionState -ilike "Disconnected"} | Measure-Object -Property sessionstate).count

To get all the desktops in which are in Maintenaince mode and Powered On.
$DesktopsInMaintenaince = ($state | Where-Object {$_.InMaintenanceMode -ilike "True" -and $_.PowerState -eq "On"} | Measure-Object -Property InMaintenanceMode).Count

I think you will get the idea right? You can make as many combinations as you want for the things you want to monitor!

The last thing we have to do is to write the result to PRTG. Here you have two options:

1: You create one sensor with different channels: This would be the most suitable solution in most cases. The advantage is that, since you have all the data under one sensor, PRTG sets up one remote connection for each scanning interval, gathers all the data at once and updates all the values in the sensor. The disadvantage with this solution is that it is not possible to have the value of each channel on a PRTG map, since the map function in PRTG always shows the value of the primary channel. This is why I go for option 2.

2: You create different sensors with one channel for each sensor. As described above, with this option we can create a PRTG map to make an overview of the status of the desktops: number connected/disconnected/in maintenance/powered off etc. The disadvantage is that each polling interval, PRTG sets-up a remote connection to the Desktop Delivery Controller per sensor. So if we create 15 sensors to monitor different desktop statuses, PRTG creates 15 different connections to the Citrix Desktop Delivery Controller.

To limit to amount of scripts you use for PRTG, I always use the parameter setting under the advanced exe script sensor. With the parameter setting, it is possible to define a parameter for the script.



Here is the full script, to use this script, you will need the prtgshell PowerShell function as described here

# Get variables from PRTG
param (
   [parameter(Mandatory=$true,Position=0)]
   [string]$object
   )

# Import module
if (-not (Get-module prtgshell)) {
    Import-Module prtgshell
    }

# Get the Citrix Desktop Delivery Controller
$citrixddc = "YourCitrixDDCServer"
# Create a connection to the remote computer
$session = New-PSSession -ComputerName $citrixddc

# Run the commands on the remote computer
Invoke-command -Session $session -scriptblock {

    asnp Citrix*
    $result = Get-BrokerDesktop -MaxRecordCount 10000
} 


# Count the variable $result and print it to the variable $state.
$state = Invoke-Command -Session $session -ScriptBlock {$result}

# Start writing output to PRTG.
$XMLOutput = "<prtg>`n"

# Get the total number of desktops to calculate the percentage.
$TotalDesktops = $state.count

# TotalDesktops
if ($object -eq "TotalDesktops"){
$XMLOutput += Set-PrtgResult "Desktops: Totaal Aantal" $TotalDesktops "Desktops"
}

#Desktops Connected
if ($object -eq "DesktopsConnected"){
$connected = ($state | Where-Object {$_.SessionState -ilike "Connected"} | Measure-Object -Property sessionstate).count
$XMLOutput += Set-PrtgResult "Desktops: Connected / Active" $connected "Desktops"
}


#Desktops Disconnected
if ($object -eq "DesktopsDisconnected"){
$disconnected = ($state | Where-Object {$_.SessionState -ilike "Disconnected"} | Measure-Object -Property sessionstate).count
$XMLOutput += Set-PrtgResult "Desktops: Disconnected / Active" $disconnected "Desktops"
}


# Desktops in Maintenaince mode
if ($object -eq "DesktopsInMaintenaince"){
$DesktopsInMaintenaince = ($state | Where-Object {$_.InMaintenanceMode -ilike "True" -and $_.PowerState -eq "On"} | Measure-Object -Property InMaintenanceMode).Count
$XMLOutput += Set-PrtgResult "Desktops: In Maintenance mode" $DesktopsInMaintenaince "Desktops"
}

# Disconnect the session
Remove-PSSession $session

#Finish writing to PRTG
$XMLOutput += "</prtg>"
write-host $XMLOutput

And the result

2 comments:

  1. hi, am always getting "no result or error in xml response, when i try the ps1 script on the prtg server, it works..do you have an idea? kinds regards, sebastian

    ReplyDelete
  2. How to play pokies online - JTM Hub
    · 5 min · Materials: Email, Post navigation, Laptop 진주 출장샵 or mobile device. 천안 출장안마 Online 수원 출장샵 slot machines and roulette machines 오산 출장마사지 of 남원 출장안마 this country are

    ReplyDelete