I created a powershell script that connects to RabbitMQ in order to the the total number of messages in RabbitMQ queues. Our developers keep adding new queues, so I wanted a script that could dynamically get the list of queues and then get the number of messages in each queue.
Invoke-RestMethod is executed twice - the first execution works without issue and returns the list of queues.
However, the second execution of Invoke-RestMethod to get the number of messages in the queue does NOT work from PRTG - it throws a 404 error. This particular call has a forward slash HTML encoded as %2f in the URI (required per the RabbitMQ api - "/" is the default RabbitMQ vHost).
The crazy thing is that the script WORKS when executed manually on the PRTG probe server using the same AD account. The URI is also accessible via the browser on the PRTG probe server.
Any idea why this works everywhere except when run from PRTG?
Here is the code:
$Payload = "<prtg>`n" #$cred = Import-Clixml $CredPath $RabbitUser = "USER" $Token = "PASSWORD" #REDACTED #Set up credentials $SecPasswd = ConvertTo-SecureString $Token -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential ($RabbitUser, $secpasswd) [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $Logfile = "C:\Temp\test\queuetestoutput.txt" if ( test-path -Path $Logfile ) { Remove-Item -Path $Logfile } New-Item -Path $Logfile -ItemType "File" | Out-Null #Get list of queue names $URI = "https://myhost.mydomain.com:2001/api/queues" # <----- This URI works - I get a list of queues back $jsonQueues = Invoke-RestMethod -Uri $URI -Method Get -Credential $cred -TimeoutSec 10 $QueueNames = $jsonQueues.name foreach ($q in $QueueNames) { $QN = $q.Trim() if(!( $qn.contains("Retry"))) { #THIS NEXT LINE HAS %2F HTML ENCODING for "/" - I think that is causing the 404 error. $URI = "https://myhost.mydomain.com:2001/api/queues/%2f/$QN" $URI | add-content -Path $Logfile #Get queue statistics - interested in total messages (messages statistic) try { $QueueInfo = Invoke-RestMethod -Uri $URI -Method Get -Credential $cred -TimeoutSec 60 } catch { add-content -Path $LogFile -Value "ERROR: $QN - Invoke-RestMethod $URI Failed: $($_.Exception.Message)" Write-Output "<prtg>" Write-Output "<error>1</error>" Write-Output "<text>Invoke-RestMethod $URI Failed: $($_.Exception.Message)</text>" Write-Output "</prtg>" Exit } $QueuedMessages = $QueueInfo.messages $QueueInfo | add-Content -Path $Logfile #Output into variable holding JSON results $payload = $payload + " <result><channel>$QN</channel><unit>Messages</unit><value>$QueuedMessages</value></result>`n" #Set to nonsense value for troubleshooting. $QueuedMessages = -1 } } $payload = $payload + "</prtg>`n" $payload | add-content -Path $Logfile $payload
Add comment