Skip to content

Instantly share code, notes, and snippets.

@gbiellem
Last active February 18, 2017 23:02
Show Gist options
  • Save gbiellem/abc1872a939e4bfa639eca51d2ed244f to your computer and use it in GitHub Desktop.
Save gbiellem/abc1872a939e4bfa639eca51d2ed244f to your computer and use it in GitHub Desktop.
Report NSB messages in error queue and TxDLQ to Slack and also purge any expired ServiceControl Heartbeats that end up in txDLQ
#Requires -RunAsAdministrator
Add-Type -Assembly "System.Messaging"
Function RemoveServiceControlPluginMessagesFromTXDLQ {
$filter = New-Object System.Messaging.MessagePropertyFilter
$filter.Acknowledgment = $true
$filter.LookupId = $true
$filter.Extension = $true
$counter = 0
$messagequeue = New-Object System.Messaging.MessageQueue "FormatName:DIRECT=OS:$env:COMPUTERNAME\SYSTEM$;DEADXACT"
$messagequeue.MessageReadPropertyFilter = $filter
foreach($msg in ($messagequeue | ? Acknowledgment -eq "ReceiveTimeout")) {
$MsgType = GetMessageType -Message $msg
if ($MsgType.StartsWith("ServiceControl.Plugin.")) {
[void] $txdlq.ReceiveByLookupId($msg.LookupId)
$counter++;
}
}
$messagequeue.Dispose()
Write-Verbose "$counter plugin message(s) removed"
}
Function GetMessageType {
param (
[Parameter(Mandatory=$true)]
[System.Messaging.Message] $Message
)
try {
$ms = New-Object System.IO.MemoryStream(,[byte[]]$message.Extension)
$extensionXml = New-Object System.Xml.XmlDocument
$extensionXml.PreserveWhitespace = $true
$extensionXml.Load($ms)
$ms.Close()
$ms.Dispose()
$msgtype = ([xml] $extensionXml.OuterXml).ArrayOfHeaderInfo.HeaderInfo | ? Key -Eq "NServiceBus.EnclosedMessageTypes" | Select -ExpandProperty Value
return $msgtype.Split(',')[0]
}
catch {
return ""
}
}
Function GetMessageSummaryFromTXDLQ {
$filter = New-Object System.Messaging.MessagePropertyFilter
$filter.Extension = $true
$messagequeue = New-Object System.Messaging.MessageQueue "FormatName:DIRECT=OS:$env:COMPUTERNAME\SYSTEM$;DEADXACT"
$messagequeue.MessageReadPropertyFilter = $filter
$messagequeue | % { GetMessageType $_ } | ? { -not $_.StartsWith("ServiceControl.Plugin.") } | Group-Object | Select Name, Count
$messagequeue.Dispose()
}
Function GetMessageSummaryFromError {
$filter = New-Object System.Messaging.MessagePropertyFilter
$filter.Extension = $true
$messagequeue = New-Object System.Messaging.MessageQueue "FormatName:DIRECT=OS:$env:COMPUTERNAME\Private$\Error"
$messagequeue.MessageReadPropertyFilter = $filter
$messagequeue | % { GetMessageType $_ } | Group-Object | Select Name, Count
$messagequeue.Dispose()
}
Function BuildReport {
param(
$data,
[Parameter(Mandatory=$true)]
$name
)
if ($data -eq $null) {
$attachment = @{
'title' = "$name Report"
'pretext' = 'Backend MSMQ Report'
'color' = 'good'
'fallback' = "Queue is empty"
'text' = "Queue is empty"
}
}
else {
$count = $data | Select -ExpandProperty Count | Measure-Object -Sum | Select -ExpandProperty Sum
$attachment = @{
'pretext' = 'Backend MSMQ Report'
'title' = "$name Report"
'color' = 'warning'
'fallback' = "$name Message Count - $count"
'text' = (($data | % { ("{0} ({1})`n" -f $_.MessageType, $_.Count)}) -join "")
}
}
$report = @{
# Can set channel here and override webhook setting
# 'channel' = 'general'
'attachments' = @($attachment)
}
ConvertTo-Json -Depth 10 -InputObject $report
}
RemoveServiceControlPluginMessagesFromTXDLQ
$txdlqdata = GetMessageSummaryFromTXDLQ | Select @{Name="MessageType";Expression={$_.Name.Split('.') | Select -Last 1}}, Count
$errordata = GetMessageSummaryFromError | Select @{Name="MessageType";Expression={$_.Name.Split('.') | Select -Last 1}}, Count
$body = ConvertTO-Json $report -Depth 10
# Load the webhook url from file
$url = (Get-Content -Path 'C:\Monitron\slackwebhookurl.txt')
IWR -UseBasicParsing -Uri $url -Body (BuildReport -data $errordata -name "Error Queue") -Method Post
IWR -UseBasicParsing -Uri $url -Body (BuildReport -data $txdlqdata -name "TX DLQ") -Method Post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment