Instantly share code, notes, and snippets.

Embed
What would you like to do?
try
{
$ReadOnlyUser = "Read Only Username"
$EncryptedPassword = "Encrypted Password"
#Decrypt the password for use in the script
$SecurePassword = $EncryptedPassword | ConvertTo-SecureString
$Marshal = [System.Runtime.InteropServices.Marshal]
$Bstr = $Marshal::SecureStringToBSTR($SecurePassword)
$Password = $Marshal::PtrToStringAuto($Bstr)
$Marshal::ZeroFreeBSTR($Bstr)
# Disable SSL certificate checking so we can connect to servers using a cert signed by a private CA and enable TLS1.2
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#Grab the Host IP/FQDN from the Device Config in PRTG
$TargetHost = $env:prtg_host
#Send our request and convert the result into a JSON object
$NetScalerQuery = @{uri = "https://$TargetHost/nitro/v1/config/lbvserver";
Method = 'GET';
Headers = @{'X-NITRO-USER' = '$ReadOnlyUser'; 'X-NITRO-PASS' = '$Password';}
}
$NetScalerQueryResult = Invoke-WebRequest @NetScalerQuery -UseBasicParsing
$NetScalerQueryJSON = ConvertFrom-Json -InputObject $NetScalerQueryResult
#Count the number of Virtual Servers returned
$LBServerTotal = $NetScalerQueryJSON.lbvserver.count
$MessageText = "OK"
Write-Host "<prtg>"
#Loop through the Virtual Servers, evaluate their state and output for PRTG
For ($LBServerCount=0; $LBServerCount -lt $LBServerTotal; $LBServerCount++){
$ChannelName = $NetScalerQueryJSON.lbvserver[$LBServerCount].name
$CurrentState = $NetScalerQueryJSON.lbvserver[$LBServerCount].curstate
$EffectiveState = $NetScalerQueryJSON.lbvserver[$LBServerCount].effectivestate
$Health = $NetScalerQueryJSON.lbvserver[$LBServerCount].health
If ($CurrentState -eq "DOWN" -or $EffectiveState -eq "DOWN" -or $Health -ne "100") {
$StateValue = 1
If ($MessageText -eq "OK") {
$MessageText = $ChannelName
}
Else {
$MessageText = $MessageText + " " + $ChannelName
}
}
Else {
$StateValue = 0
}
Write-Host "<result>"
"<channel>$ChannelName</channel>"
"<value>$StateValue</value>"
"<showChart>1</showChart>"
"<showTable>1</showTable>"
"<LimitMaxError>0</LimitMaxError>"
"<LimitMode>1</LimitMode>"
"</result>"
}
Write-Host "<Text>$MessageText</Text>"
Write-Host "</prtg>"
}
#If try code block fails then throw an error to PRTG
catch
{
Write-Host "<prtg>"
Write-host "<error>1</error>"
Write-Host "<text>Sensor Error</text>"
Write-Host "</prtg>"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment