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?
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?
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
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.