Tuesday, February 2, 2016

How to monitor Citrix XenDesktop with PRTG (Part 1)

A few weeks ago I wrote a post about the exe script advanced sensor and how to use this sensor in PRTG together with the prtgshell PowerShell module. One of the things that I use the exe script advanced sensor for is to monitor our VDI environment (Citrix XenDesktop), in particular the Citrix Desktop Delivery Controllers (Citrix DDC's) which deliver the virtual desktops to the end users. The Citrix Desktop Delivery controllers hold the status of all desktops in the VDI environment, so it is very useful to monitor the status of all the virtual desktops in PRTG. Let me go through the implementation step-by-step.

1. Create a new PowerShell script on the PRTG Core Server or Probe Server (depending where the DDC's are located) and call it "Citrix - Desktops statistics.ps1"

2. By design, PRTG doesn't have the capability to execute a PowerShell script on a remote server, even if you create the sensor under a device in PRTG which refers to that server. Therefore, we need to create a way to remotely connect to another server, gather the data we need and write this data to PRTG. I prefer to use Remote PowerShell for this. To use remote PowerShell, we have to enable this first on the DDC server. Login on the Citrix DDC server, open a PowerShell window (as administrator) and enter the following command:
Enable-psremoting -force
3. Next we can create a remote session to the Citrix Desktop Delivery Controller in the PowerShell script:

# 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}

Please check the Citrix support page Get-BrokerDesktop for more information.

4. As a result, we have the output of all desktops in the variable $state. To get the total of all desktop registrered in Citrix:
$total = $state.count




In this (small) environment we have a total of 311 desktops registered with the Desktop Delivery Controller.

5. Write this result to PRTG:
# Start writing output to PRTG.
$XMLOutput = "<prtg>`n"
$XMLOutput += Set-PrtgResult "Desktops: Total" $total "Desktops" 
$XMLOutput += "</prtg>"
write-host $XMLOutput

6. Result:
















The final script would be:

# 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}

# Count all the desktops. 
$TotalDesktops = $state.count

# Start writing output to PRTG.
$XMLOutput = "<prtg>`n"
$XMLOutput += Set-PrtgResult "Desktops: Total" $TotalDesktops "Desktops" 
$XMLOutput += "</prtg>"
write-host $XMLOutput

Now we have one sensor with one channel showing all the desktops registered. In part 2 of this series I'll give some more examples of fun things that we can do to monitor the status of the desktops in Citrix XenDesktop.

Continue with part 2 of this series!

0 comments:

Post a Comment