Check Message Queue Count and Alert

Hi,

We have been asked to set up some sort of alerting on the Windows Server Message Queues for a server. We use CheckMK for our alerting and to do this we need to write a custom check using something like Powershell and my useless brain is not figuring out how we do this.

What is the best way in Powershell to run a command that gets a list of queue names and the current message count, check to see if any of those counts exceed a value and then produce and appropriate output based on that check?

Example:
PS C:\Users\ADP> Get-MsmqQueue -QueueType Private | format-table -Property QueueName,MessageCount

QueueName MessageCount

private$\ahsl.ice.datafeeders.ares.telepath.outbound 0
private$\ahsl.ice.datafeeders.copath.inbound 0
private$\ahsl.ice.datafeeders.copath.order.inbound 0
private$\ahsl.ice.datafeeders.copath.outbound 0

How would you get Powershell to check private$\ahsl.ice.datafeeders.ares.telepath.outbound for example and if it had a message count of 50 it could output text to a log file something like “count is less than 500” but if it was 510 for example it would output “Count is more than 500”?

I have an idea of the logic required but i can never find the way to do this type of thing so hope someone will be kind enough to offer some guidance on this?

Hi, welcome back :wave:

Can’t test, but looking at the docs, it would be something like:

# Output all private queues with more than 50 messages
$maxQueueLength = 50

Get-MsmqQueue -QueueType Private | 
   Where-Object {$_.MessageCount -gt $maxQueueLength} | 
      Select-Object QueueName, MessageCount
# Get a specific queue and write results to a log
$logPath = 'E:\Temp\logs\queue.log'
$maxQueueLength = 500
$queueName = 'ahsl.ice.datafeeders.ares.telepath.outbound'

$queueLength = (Get-MsmqQueue -Name $queueName -QueueType Private).MessageCount

$result = switch ($queueLength) {
    {$_ -gt $maxQueueLength} {
        "Count for $queueName is more than $maxQueueLength ($queueLength)."
    }

    default {
        "Count for $queueName is less than $maxQueueLength."
    }    
}

Add-Content -Path $logPath -Value $result
2 Likes

Hi Matt,

Thanks for that. Really helpful and worked nicely. Can i ask one more piece of help/advice.

How easy is it to get that in a loop to check multiple queue’s?

Can you output the original get-MsmqQueue command into a variable and then loop through that with the same check?

Thanks

Easy, just build an array of queue names, and iterate over it.

$queueList = @(
    'queue1'
    'queue2'
)

foreach ($queueName in $queueList) {
    do stuff
}

I’m not sure what you’re asking here, but I think you probably have enough information to give it a try yourself.

1 Like

Apologies for the late response.

Yes thank you got ore than enough to play around with. Something with Powershell (and other scripting methods to be fair) that my brain struggles to figure it out. I had been looking at foreach loops but could not get it anywhere near working.

Will try again.

Thanks anyway for the help.