I just wrote a powershell script to use in an EXE/Script Advanced sensor. I include channels for "Message Publish Rate", "Message Count", Connections, Consumers, and Queues. You could certainly do others.
I adapted the script found here: https://kb.paessler.com/en/topic/71263
Here's the script:
# Monitor stats from RabbitMQ in PRTG v0.1 20/01/2021
# Adapted from the script written to monitor Unifi Access Points Published Here: https://kb.paessler.com/en/topic/71263
#
# Parameters in PRTG are: Controller's URI, Port, Username and Password. Example without placeholders:
# -server 'unifi.domain.tld' -port '8443' -username 'admin' -password 'somepassword'
#
# -server '%host' -port '8443' -site 'default' -username '%windowsuser' -password '%windowspassword'
# This second option requires the device's address in PRTG to be the controller's address, the credentials for windows devices
# must also match the log-in/password from the controller. This way you don't leave the password exposed in the sensor's settings.
#
# It's recommended to use larger scanning intervals for exe/xml scripts. Please also mind the 50 exe/script sensor's recommendation per probe.
# The sensor will not generate alerts by default, after creating your sensor, define limits accordingly.
# This sensor is to be considered experimental.
#
# Source(s):
# https://blog.cdemi.io/monitoring-rabbitmq-in-prtg/
# https://documentation.solarwinds.com/en/Success_Center/appoptics/content/kb/host_infrastructure/integrations/rabbitmq.htm
# https://www.paessler.com/manuals/prtg/custom_sensors
param(
[string]$server = 'testserver.domain.com',
[string]$port = '15672',
[string]$username = 'guest',
[string]$password = 'guest',
[switch]$debug = $false
)
#Ignore SSL Errors
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
#Define supported Protocols
[System.Net.ServicePointManager]::SecurityProtocol = @("Tls12","Tls11","Tls","Ssl3")
# Confirm Powershell Version.
if ($PSVersionTable.PSVersion.Major -lt 3) {
Write-Output "<prtg>"
Write-Output "<error>1</error>"
Write-Output "<text>Powershell Version is $($PSVersionTable.PSVersion.Major) Requires at least 3. </text>"
Write-Output "</prtg>"
Exit
}
# Create $controller and $credential using multiple variables/parameters.
[string]$controller = "http://$($server):$($port)"
[string]$credential = "`{`"username`":`"$username`",`"password`":`"$password`"`}"
$pair = "$($username):$($password)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
Authorization = $basicAuthValue
}
# Start debug timer
$queryMeasurement = [System.Diagnostics.Stopwatch]::StartNew()
# Perform the authentication and store the token to myWebSession
try {
$null = Invoke-Restmethod -Uri "$controller/api/overview" -Headers $Headers -ContentType "application/json; charset=utf-8" -SessionVariable myWebSession
}catch{
Write-Output "<prtg>"
Write-Output "<error>1</error>"
Write-Output "<text>Authentication Failed: $($_.Exception.Message)</text>"
Write-Output "</prtg>"
Exit
}
#Query API providing token from first query.
try {
$jsonresultat = Invoke-Restmethod -Uri "$controller/api/overview" -WebSession $myWebSession
}catch{
Write-Output "<prtg>"
Write-Output "<error>1</error>"
Write-Output "<text>API Query Failed: $($_.Exception.Message)</text>"
Write-Output "</prtg>"
Exit
}
# Load File from Debug Log
# $jsonresultatFile = Get-Content '.\unifi_sensor2017-15-02-05-42-24_log.json'
# $jsonresultat = $jsonresultatFile | ConvertFrom-Json
# Stop debug timer
$queryMeasurement.Stop()
#Write Results
write-host "<prtg>"
Write-Host "<result>"
Write-Host "<channel>Message Publish Rate</channel>"
Write-Host "<value>$($jsonresultat.message_stats.publish_details.rate)</value>"
Write-Host "<float>1</float>"
Write-Host "</result>"
Write-Host "<result>"
Write-Host "<channel>Message Count</channel>"
Write-Host "<value>$($jsonresultat.queue_totals.messages)</value>"
Write-Host "</result>"
Write-Host "<result>"
Write-Host "<channel>Connections</channel>"
Write-Host "<value>$($jsonresultat.object_totals.connections)</value>"
Write-Host "</result>"
Write-Host "<result>"
Write-Host "<channel>Consumers</channel>"
Write-Host "<value>$($jsonresultat.object_totals.consumers)</value>"
Write-Host "</result>"
Write-Host "<result>"
Write-Host "<channel>Queues</channel>"
Write-Host "<value>$($jsonresultat.object_totals.queues)</value>"
Write-Host "</result>"
write-host "</prtg>"
# Write JSON file to disk when -debug is set. For troubleshooting only.
if ($debug){
[string]$logPath = ((Get-ItemProperty -Path "hklm:SOFTWARE\Wow6432Node\Paessler\PRTG Network Monitor\Server\Core" -Name "Datapath").DataPath) + "Logs (Sensors)\"
$timeStamp = (Get-Date -format yyyy-dd-MM-hh-mm-ss)
$json = $jsonresultat | ConvertTo-Json
$json | Out-File $logPath"rabbit_sensor$($timeStamp)_log.json"
}
Add comment