############################################################### # # Scriptname: PRTGCustomCitrixDGAvailableVDIs.ps1 # # Autor: Urs Heeb # Date: 13.09.2022 # # Version: 2022.09.01 / 13.09.22 / Urs Heeb # Create script based on other custom PRTG scripts # 2022.09.02 / 14.09.22 / Urs Heeb # Create the output and calculation of percent # Check all delivery groups and set the array # 2022.09.03 / 15.09.22 / Urs Heeb # Create the output and the final notification $RetString # 2022.10.01 / 03.10.22 / Urs Heeb # Modify the output for test delivery groups (no Alarm) # # # Description: Script does following: # Connects to a Citrix controller # Gets the information of all delivery groups # Calculates the available VDIs # # Requirements: # PRTG variables are needed while configuration # %host %windowsdomain %windowsuser %windowspassword # PRTG service user needs read permission on Citrix controller # ############################################################### ### # Get parameter from PRTG param ( [string]$server, [string]$domain, [string]$username, [string]$password ) # For troubleshooting <# $server="controller.domain.pit" $username="username" $password="password" #> # Prepare credentials $credentials = New-Object System.Management.Automation.PSCredential (($domain+'\'+$username), (ConvertTo-SecureString $password -AsPlainText -Force)) ### # Prepare an array and get the information from a Citrix controller by an Invoke-Command # Citrix PS commands are only available on those servers $Data = $null $Data=@() $Data += Invoke-Command -Computername $server -credential $credentials -ScriptBlock { # Load Citrix PS Snapin add-pssnapin citrix* # Get delivery groups with the needed information $DeliveryGroups=Get-BrokerDesktopGroup | Select-Object -Property Name, TotalDesktops, DesktopsAvailable, InMaintenanceMode # Set the array for later work $DG=@() ForEach ($group in $DeliveryGroups){ $DG += [PSCustomObject]@{Name=$group.Name;Total=$group.TotalDesktops;Available=$group.DesktopsAvailable;Maintenance=$group.InMaintenanceMode} } # Return data to PRTG probe return $DG } ### # Prepare the data and the PRTG XML output # Reset some variables and define standard values $returnState=$null $returnState=@() $returnStateOK = 0 $returnStateWarning = 1 $returnStateCritical = 2 # Start preparing XML output $retXml = "`n" # Get data for each delivery group from the array created on the Citrix Controller ForEach ($Dataset in $Data){ $AlertString = "ALERT - low VDI capacity" $AlertLimit = 10 $WarningString = "Warning - VDI capacity reachs the end" $WarningLimit = 20 # Prepare variables based on the array data $DGName = $Dataset.Name $DGTotal = $Dataset.Total $DGAvailable = $Dataset.Available # Calculate the percentage of the available VDIs $DGPercent = "{0:N0}" -f (100/$DGTotal*$DGAvailable) $DGMaintenance = $Dataset.Maintenance # Determine if a delivery group is a test group # Test DGs will never go into alarm state # Test DGs in maintenance will have a percentage value of 100 to avoid an alarm If ($DGName -like "*test*"){ $AlertString = $null $AlertLimit = $null $WarningString = "Warning - Test VDI capacity reachs the end" If ([Int64]$DGPercent -ge 20) { $RetState = $returnStateOK $returnState += [PSCustomObject]@{Name=$DGName;State=[Int64]$RetState} } ElseIf ($DGMaintenance){ $RetState=$returnStateWarning $DGPercent=100 } Else { $RetState = $returnStateWarning $returnState += [PSCustomObject]@{Name=$DGName;State=[Int64]$RetState} } } # Determine if a delivery group is in maintenance # DGs in maintenance goes into warning state, but doesn't affect the whole sensor # DGs in maintenance will not be stated based on her usage # DGs in maintenance will have a percentage value of 100 to avoid an alarm ElseIf ($DGMaintenance){ $RetState=$returnStateWarning $DGPercent=100 } # Determine return state if delivery group is not in maintenance # The values depend on the size of the delivery group Else { If ([Int64]$DGPercent -lt 10){ $RetState = $returnStateCritical $AlertString = "ALERT - low VDI capacity" $returnState += [PSCustomObject]@{Name=$DGName;State=[Int64]$RetState} } ElseIf ([Int64]$DGPercent -ge 20) { $RetState = $returnStateOK # $WarningString = "Warning - VDI capacity reachs the end" $returnState += [PSCustomObject]@{Name=$DGName;State=[Int64]$RetState} } Else { $RetState = $returnStateWarning $returnState += [PSCustomObject]@{Name=$DGName;State=[Int64]$RetState} } } #$retXml += " `n" #$retXml += " $DGName State`n" #$retXml += " $RetState`n" #$retXml += " `n" $retXml += " `n" $retXml += " $DGName Free %`n" $retXml += " $DGPercent`n" $retXml += " Percent`n" $retXml += " 1`n" $retXml += " $AlertLimit`n" $retXml += " $AlertString`n" $retXml += " $WarningLimit`n" $retXml += " $WarningString`n" $retXml += " `n" $retXml += " `n" $retXml += " $DGName Free VDIs`n" $retXml += " $DGAvailable`n" $retXml += " Count`n" $retXml += " `n" } # Determine return string depends on the several states If ($returnState.State -contains 2) { $RetString = "ALERT - low VDI capacity" } ElseIf ($returnState.State -contains 1) { $RetString = "Warning - VDI capacity reachs the end" } Else { $RetString = "OK" } $retXml += " $RetString`n" $retXml += "`n" ### # Return info to PRTG write-host $retXml