############################################################### # # Scriptname: PRTGCustomCitrixADMInstancePerformance.ps1 # # Autor: Urs Heeb # Date: 26.09.2022 # # Version: 2022.10.01 / 31.10.22 / Urs Heeb # Create script # 2022.11.01 / 01.11.22 / Urs Heeb # Create output sorted by hostname # 2022.11.02 / 02.11.22 / Urs Heeb # Corrections in calculations and outputs # Add channel indexes for a proper sort in the PRTG web GUI # # Description: Script does following: # Connects to a Citrix ADM # Gets the information of existing config drifts # # Requirements: # PRTG variables are needed while configuration # FQDN of the ADM %windowsdomain %windowsuser %windowspassword # PRTG service user needs read permission on Citrix ADM # PRTGCustomCitrixADM.psm1 module is needed in the same folder as this script # ############################################################### ### # Get parameter from PRTG param ( [string]$server, [string]$domain, [string]$username, [string]$password ) # For troubleshooting <# $server="adm.domain.pit" $username="username" $password="password" $CustomSensors="\\domain.pit\development\PRTG Custom sensors" #> # Import ADM PS module $CustomSensors="C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML\" Import-Module $CustomSensors\PRTGCustomCitrixADM.psm1 # Create the ADM session $ADMHost = "https://"+$server $ADMSession = Connect-ADM -ADMHost $ADMHost -CredUser $username -CredPW $password # Prepare the output variables $ADCInstance = $null $ADCInstance2 = $null $ADCInstances = $null $ADCInstances2 = $null # Get the Instances from the ADM $ADCInstance = Invoke-ADMNitro -ADMSession $ADMSession -OperationMethod GET -ResourceType ns # Create the variable only with the NS parameters $ADCInstance2 = $ADCInstance | Select-Object ns # For troubleshooting #$ADCInstance2.ns | FT hostname, ns_mgmt_cpu_usage, ns_cpu_usage, vm_memory_usage, diskperusage, disk0_used, disk1_used, ns_tx, model_id -AutoSize $ADCInstances = $ADCInstance2.ns # Sort output based on hostname $ADCInstances2 = $ADCInstances | Sort-Object hostname # For troubleshooting #$ADCInstances2 | FT hostname # Prepare the PRTG output # Output will be created in the loop for each appliance $retXml = $null $retXml = "`n" #region prtgvariables $returnState=$null $returnState=@() $returnStateOK = 0 $returnStateWarning = 1 $returnStateCritical = 2 $RetCritical = $null $CPUWarnLevel = 60 $CPUWarnString = "High CPU warning" $CPUAlertLevel = 80 $CPUAlertString = "High CPU alert" $RAMWarnLevel = 60 $RAMWarnString = "High RAM usage warning" $RAMAlertLevel = 80 $RAMAlertString = "High RAM usage alert" $DiskWarnLevel = 60 $DiskWarnString = "High Disk usage warning" $DiskAlertLevel = 75 $DiskAlertString = "High Disk usage alert" $BWWarnLevel = 60 $BWWarnString = "High bandwidth usage warning" $BWAlertLevel = 80 $BWAlertString = "High bandwidth usage alert" $AlertString = "ALERT - some ADCs needs attention!" $AlertLevel = "0.9" $WarningString = "Warning - some ADCs needs attention!" #endregion prtgvariables $Instances = $null $Instances = @() $ChannelLetterIndex = 65 # Start index with 65 for ASCII 'A' ForEach ($Instance in $ADCInstances2){ #region returnvariables $ChannelLetterIndex = $ChannelLetterIndex + 1 # Increase letter index by one for each appliance instance $RetChannelLetter = [char]($ChannelLetterIndex) $RetHostname = $Instance.hostname $RetVersion = $Instance.version $RetMgmtCPU = [math]::Round($Instance.ns_mgmt_cpu_usage) $RetCPU = [math]::Round($Instance.ns_cpu_usage) $RetRAM = [math]::Round($Instance.vm_memory_usage) $RetDiskUsage = $Instance.diskperusage $RetDisk0Used = $Instance.disk0_used $RetDisk1Used = $Instance.disk1_used $RetBWTX = $Instance.ns_tx $RetModel = $Instance.model_id #endregion returnvariables #region version # version channel $ChannelIndex = 0 $RetChannelIndex = [char]($ChannelLetterIndex)+$ChannelIndex $retXml += " `n" $retXml += " $RetChannelIndex $RetHostname Version`n" $retXml += " $RetVersion`n" $retXml += " Custom`n" $retXml += " $RetVersion`n" $retXml += " 0`n" $retXml += " `n" #endregion version #region bwcalculation # Calculate the percentage of the bandwidth usage # based on the license and the current bandwith # A conversation to string is needed because of the # exponential results which needed to be trimmed $BWPercentCalc = $null $BWPercent = $null $BWPercentCalc = $Instance.ns_tx*100/$Instance.model_id $BWPercentString = [String]$BWPercentCalc If ($BWPercentString.Length -ge 5){ $SubLength = 5 } Else { $SubLength = $BWPercentString.Length } $BWPercentStringShort = $BWPercentString.Substring(0,$SubLength) $BWPercentShort = [Decimal]$BWPercentStringShort $RetBWPercent = [math]::Round($BWPercentShort) #endregion bwcalculation #region mgmtcpu # Enumerate and prepare output for Management CPU load $ChannelIndex = $ChannelIndex + 1 $RetChannelIndex = [char]($ChannelLetterIndex)+$ChannelIndex $retXml += " `n" $retXml += " $RetChannelIndex $RetHostname Management CPU Load`n" $retXml += " $RetMgmtCPU`n" $retXml += " Percent`n" If ($RetMgmtCPU -gt $CPUAlertLevel) { $RetState = $returnStateCritical $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } ElseIf ($RetMgmtCPU -le $CPUWarnLevel) { $RetState = $returnStateOK $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } Else { $RetState = $returnStateWarning $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } $retXml += " 1`n" $retXml += " $CPUAlertLevel`n" $retXml += " $CPUAlertString`n" $retXml += " $CPUWarnLevel`n" $retXml += " $CPUWarnString`n" $retXml += " `n" #endregion mgmtcpu #region paketcpu # Enumerate and prepare output for Paket CPU load $ChannelIndex = $ChannelIndex + 1 $RetChannelIndex = [char]($ChannelLetterIndex)+$ChannelIndex $retXml += " `n" $retXml += " $RetChannelIndex $RetHostname Paket CPU Load`n" $retXml += " $RetCPU`n" $retXml += " Percent`n" If ($RetCPU -gt $CPUAlertLevel) { $RetState = $returnStateCritical $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } ElseIf ($RetCPU -le $CPUWarnLevel) { $RetState = $returnStateOK $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } Else { $RetState = $returnStateWarning $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } $retXml += " 1`n" $retXml += " $CPUAlertLevel`n" $retXml += " $CPUAlertString`n" $retXml += " $CPUWarnLevel`n" $retXml += " $CPUWarnString`n" $retXml += " `n" #endregion #region ram # Enumerate and prepare output for RAM load $ChannelIndex = $ChannelIndex + 1 $RetChannelIndex = [char]($ChannelLetterIndex)+$ChannelIndex $retXml += " `n" $retXml += " $RetChannelIndex $RetHostname RAM Load`n" $retXml += " $RetRAM`n" $retXml += " Percent`n" If ($RetRAM -gt $RAMAlertLevel) { $RetState = $returnStateCritical $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } ElseIf ($RetRAM -le $RAMWarnLevel) { $RetState = $returnStateOK $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } Else { $RetState = $returnStateWarning $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } $retXml += " 1`n" $retXml += " $RAMAlertLevel`n" $retXml += " $RAMAlertString`n" $retXml += " $RAMWarnLevel`n" $retXml += " $RAMWarnString`n" $retXml += " `n" #endregion ram #region diskusage # Enumerate and prepare output for disk usage $ChannelIndex = $ChannelIndex + 1 $RetChannelIndex = [char]($ChannelLetterIndex)+$ChannelIndex $retXml += " `n" $retXml += " $RetChannelIndex $RetHostname disk usage`n" $retXml += " $RetDiskUsage`n" $retXml += " Percent`n" If ($RetDiskUsage -gt $DiskAlertLevel) { $RetState = $returnStateCritical $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } ElseIf ($RetDiskUsage -lt $DiskWarnLevel) { $RetState = $returnStateOK $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } Else { $RetState = $returnStateWarning $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } $retXml += " 1`n" $retXml += " $DiskAlertLevel`n" $retXml += " $DiskAlertString`n" $retXml += " $DiskWarnLevel`n" $retXml += " $DiskWarnString`n" $retXml += " `n" #endregion diskusage #region disk0usage # Enumerate and prepare output for disk0 usage $ChannelIndex = $ChannelIndex + 1 $RetChannelIndex = [char]($ChannelLetterIndex)+$ChannelIndex $retXml += " `n" $retXml += " $RetChannelIndex $RetHostname disk0 usage`n" $retXml += " $RetDisk0Used`n" $retXml += " Percent`n" If ($RetDisk0Used -gt $DiskAlertLevel) { $RetState = $returnStateCritical $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } ElseIf ($RetDisk0Used -lt $DiskWarnLevel) { $RetState = $returnStateOK $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } Else { $RetState = $returnStateWarning $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } $retXml += " 1`n" $retXml += " $DiskAlertLevel`n" $retXml += " $DiskAlertString`n" $retXml += " $DiskWarnLevel`n" $retXml += " $DiskWarnString`n" $retXml += " `n" #endregion disk0usage #region disk1usage # Enumerate and prepare output for disk1 usage $ChannelIndex = $ChannelIndex + 1 $RetChannelIndex = [char]($ChannelLetterIndex)+$ChannelIndex $retXml += " `n" $retXml += " $RetChannelIndex $RetHostname disk1 usage`n" $retXml += " $RetDisk1Used`n" $retXml += " Percent`n" If ($RetDisk1Used -gt $DiskAlertLevel) { $RetState = $returnStateCritical $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } ElseIf ($RetDisk1Used -lt $DiskWarnLevel) { $RetState = $returnStateOK $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } Else { $RetState = $returnStateWarning $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } $retXml += " 1`n" $retXml += " $DiskAlertLevel`n" $retXml += " $DiskAlertString`n" $retXml += " $DiskWarnLevel`n" $retXml += " $DiskWarnString`n" $retXml += " `n" #endregion disk1usage #region bwusage # Enumerate and prepare output for bandwidth usage $ChannelIndex = $ChannelIndex + 1 $RetChannelIndex = [char]($ChannelLetterIndex)+$ChannelIndex $retXml += " `n" $retXml += " $RetChannelIndex $RetHostname bandwith usage`n" $retXml += " $RetBWPercent`n" $retXml += " Percent`n" If ($RetBWPercent -gt $BWAlertLevel) { $RetState = $returnStateCritical $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } ElseIf ($RetBWPercent -le $BWWarnLevel) { $RetState = $returnStateOK $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } Else { $RetState = $returnStateWarning $returnState += [PSCustomObject]@{Name=$RetHostname;State=[Int64]$RetState} } $retXml += " 1`n" $retXml += " $BWAlertLevel`n" $retXml += " $BWAlertString`n" $retXml += " $BWWarnLevel`n" $retXml += " $BWWarnString`n" $retXml += " `n" #endregion bwusage } # For troubleshooting #$Instances | FT -AutoSize #region returnstring # Determine return string depends on the several states If ($returnState.State -contains 2) { $RetString = $AlertString $RetHealth = 0 } ElseIf ($returnState.State -contains 1) { $RetString = $WarningString $RetHealth = 50 } Else { $RetString = "OK" $RetHealth = 100 } #endregion # For troubleshooting #$RetString #region health # Channel for overall health $retXml += " `n" $retXml += " A0 Overall health`n" $retXml += " $RetHealth`n" $retXml += " Count`n" $retXml += " 1`n" $retXml += " 49`n" $retXml += " $AlertString`n" $retXml += " 99`n" $retXml += " $WarningString`n" $retXml += " `n" #endregion health $retXml += " $RetString`n" $retXml += "`n" ### # Return info to PRTG write-host $retXml