What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

404 Error running custom xmlexesensor to get total messages in RabbitMQ queues.

Votes:

1

Your Vote:

Up

Down

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

404 invoke-restmethod powershell rabbitmq xmlexesensor

Created on Apr 12, 2022 12:28:15 AM by  GM0593 (1) 1

Last change on Apr 12, 2022 5:34:21 AM by  Felix Wiesneth [Paessler Support]



3 Replies

Votes:

0

Your Vote:

Up

Down

So it basically has three slashes when decoded - and that URL really works in rabbitMQ?

Created on Apr 15, 2022 10:44:23 AM by  Stephan Linke [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Yes that is correct and it really does work in RabbitMQ if we have the middle "/" escaped as %2f.

Created on Apr 18, 2022 5:05:56 PM by  GM0593 (1) 1



Votes:

0

Your Vote:

Up

Down

This is odd - could you try to hardcode the credentials instead of loading them externally? I think it may be due to the credentials not being loaded properly here...

Created on Apr 22, 2022 5:48:42 AM by  Stephan Linke [Paessler Support]



Please log in or register to enter your reply.


Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.