Someone asked me a question last week about using the Queue class’s in Exchange 2003 and this got me thinking about link monitors. Link monitors are a little bit old terminology these days although I really used to like the old site and link monitors in Exchange 5.5 especially the visual representation. Now there are monitors on Exchange 2003 and while useful they are really lacking in being able to tell you if there actually is a problem and what that problem might be.
So I decided to see if I could build a better link monitor that would one tell me when there is a problem and also let me know in the warning email what that problem might be. Eg if there are lot of messages queuing up send me a dump of what the message queues looks like and tell me what messages are in the queue. Then with any luck I can tell from the email if there really is a problem that I might need to look at or if its just a temporarily large volume of email being sent. Eg the first thing you do when you get a warning about a problem with mail queues is to go and check what’s in them so I was trying to cut this step out.
So the solution I put together was a script that would listen for modification events on the Exchange_SMTPQueue Wmi class with a filter so it would only take action when the number of message in any of the queues went over a configured threshold. When the threshold is reached it would query all queues on the box and build a html table of the results and it would also then enumerate the messages in the queues that were over threshold and create a html table of the result of this enumeration. The html tables would then form the body of an alert email which would be sent. To stop the script sending email every update period which is about every 15 seconds or so the script tracks the last time an alert was sent so it will only send 1 alert per hour if the queues are still over threshold.
The script itself uses 3 WMI queries the first query listens for the Queue modification events. The second query enumerates the queues the third query enumerates the messages within a queue that are over threshold. A mail is then sent over SMTP using CDOEX/CDOSYS. The script itself can be run locally or remotely as long as there is CDOEX or CDOSYS installed on avaible on the machine to send the message.
To use the script you need to configure four things within the script the first is the computer name the second is the email address its sending from the email address its sending to and the server its sending through so the following 4 lines needs to be customised.
cComputerName = "."
objEmail.From = "Queuewarnings@yourdomain.com"
objEmail.To = "somebody@yourdomain.com"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Servername"
I’ve put a download copy of the script here the script itself looks like
cComputerName = "."
MessageThreshold = 5
LastAlertSent = dateadd("h",-1,now())
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
cComputerName & "\root\MicrosoftExchangeV2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'Exchange_SMTPQueue' and TargetInstance.MessageCount >= "
& MessageThreshold)
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Wscript.echo now() & " " & objLatestEvent.TargetInstance.LinkName & " " &
objLatestEvent.TargetInstance.MessageCount & " " &
objLatestEvent.TargetInstance.Size
if LastAlertSent < dateadd("h",-1,now()) then
call EnumSMTPQueues()
LastAlertSent = now()
end if
Loop
sub EnumSMTPQueues()
Const cWMINameSpace = "root/MicrosoftExchangeV2"
Const cWMIInstance = "Exchange_SMTPQueue"
HtmlMsgbody = "<table border=""1"" width=""100%"" cellpadding=""0"" bordercolor=""#000000""><tr><td
bordercolor=""#FFFFFF"" align=""center"" bgcolor=""#000080"">" _
& "<b><font color=""#FFFFFF"">Queue Name</font></b></td><td bordercolor=""#FFFFFF""
align=""center"" bgcolor=""#000080""<b><font color=""#FFFFFF"">Message
Count</font></b></td>" _
& "<td bordercolor=""#FFFFFF"" align=""center"" bgcolor=""#000080""><b><font
color=""#FFFFFF"">Queue Size</font></b></td></tr>"
strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//"& _
cComputerName&"/"&cWMINameSpace
Set objWMIExchange = GetObject(strWinMgmts)
If Err.Number <> 0 Then
WScript.Echo "ERROR: Unable to connect to the WMI namespace."
Else
Set listExchange_PublicFolders = objWMIExchange.InstancesOf(cWMIInstance)
For Each objExchange_SMTPQueue in listExchange_PublicFolders
HtmlMsgbody = HtmlMsgbody & "<tr><td>" & objExchange_SMTPQueue.LinkName &
"</td><td>" & objExchange_SMTPQueue.MessageCount _
& "</td><td>" & objExchange_SMTPQueue.size & "</td></tr>"
WScript.echo objExchange_SMTPQueue.LinkName & " " &
objExchange_SMTPQueue.MessageCount & " " & objExchange_SMTPQueue.size
if objExchange_SMTPQueue.MessageCount >= MessageThreshold then
wql ="Select * From Exchange_QueuedSMTPMessage Where LinkId='" &
objExchange_SMTPQueue.LinkID
wql = wql & "' And LinkName='" & objExchange_SMTPQueue.Linkname & "' And
ProtocolName='SMTP' And "
wql = wql & "QueueId='" & objExchange_SMTPQueue.QueueID & "' And QueueName='" &
objExchange_SMTPQueue.Queuename &"' And"
wql = wql & " VirtualMachine='" & objExchange_SMTPQueue.VirtualMachine & "'"
wql = wql & " And VirtualServerName='" & objExchange_SMTPQueue.VirtualServerName
& "'"
quehtml = quehtml & getmess(wql)
end if
next
End If
HtmlMsgbody = HtmlMsgbody & "</table><BR><B>Message Queues</B><BR>" & quehtml
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "Queuewarnings@yourdomain.com"
objEmail.To = "somebody@yourdomain.com"
objEmail.Subject = "Queue Threshold Exceeded"
objEmail.HTMLbody = HtmlMsgbody
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")
= 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")
= "Servername"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")
= 25
objEmail.Configuration.Fields.Update
objEmail.Send
wscript.echo "message sent"
End sub
function getmess(wql)
quehtml = "<table border=""1"" width=""100%""><tr><td bgcolor=""#008000""
align=""center""><b><font color=""#FFFFFF"">Date Sent</font></b></td>" _
& "<td bgcolor=""#008000"" align=""center""><b><font color=""#FFFFFF"">Sent
By</font></b></td>"_
& " <td bgcolor=""#008000"" align=""center""><b><font color=""#FFFFFF"">Recipients</font></b></td>"_
& " <td bgcolor=""#008000"" align=""center""><b><font color=""#FFFFFF"">Subject</font></b></td>"_
& " <td bgcolor=""#008000"" align=""center""><b><font color=""#FFFFFF"">Size</font></b></td></tr>"
Const cWMINameSpace = "root/MicrosoftExchangeV2"
strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//" & cComputerName &
"/" & cWMINameSpace
Set objWMIExchange = GetObject(strWinMgmts)
Set listExchange_MessageQueueEntries = objWMIExchange.ExecQuery(wql)
For each objExchange_MessageQueueEntries in listExchange_MessageQueueEntries
recieved =
dateadd("h",toffset,cdate(DateSerial(Left(objExchange_MessageQueueEntries.Received,
4), Mid(objExchange_MessageQueueEntries.Received, 5, 2),
Mid(objExchange_MessageQueueEntries.Received, 7, 2)) & " " &
timeserial(Mid(objExchange_MessageQueueEntries.Received, 9,
2),Mid(objExchange_MessageQueueEntries.Received, 11,
2),Mid(objExchange_MessageQueueEntries.Received,13, 2))))
Wscript.echo recieved & " " & objExchange_MessageQueueEntries.Sender & " " &
objExchange_MessageQueueEntries.Subject _
& " " & objExchange_MessageQueueEntries.size & " " &
replace(replace(objExchange_MessageQueueEntries.Recipients(0),vbcrlf,""),"Envelope
Recipients:","")
quehtml = quehtml & "<tr><td>" & recieved &"</td><td>" &
objExchange_MessageQueueEntries.Sender & "</td><td>" &
replace(replace(objExchange_MessageQueueEntries.Recipients(0),vbcrlf,""),"Envelope
Recipients:","") & "</td><td>" _
& objExchange_MessageQueueEntries.Subject & "</td><td>" &
objExchange_MessageQueueEntries.size & "</td></tr>"
next
quehtml = quehtml & "</table>"
getmess = quehtml
end function
So I decided to see if I could build a better link monitor that would one tell me when there is a problem and also let me know in the warning email what that problem might be. Eg if there are lot of messages queuing up send me a dump of what the message queues looks like and tell me what messages are in the queue. Then with any luck I can tell from the email if there really is a problem that I might need to look at or if its just a temporarily large volume of email being sent. Eg the first thing you do when you get a warning about a problem with mail queues is to go and check what’s in them so I was trying to cut this step out.
So the solution I put together was a script that would listen for modification events on the Exchange_SMTPQueue Wmi class with a filter so it would only take action when the number of message in any of the queues went over a configured threshold. When the threshold is reached it would query all queues on the box and build a html table of the results and it would also then enumerate the messages in the queues that were over threshold and create a html table of the result of this enumeration. The html tables would then form the body of an alert email which would be sent. To stop the script sending email every update period which is about every 15 seconds or so the script tracks the last time an alert was sent so it will only send 1 alert per hour if the queues are still over threshold.
The script itself uses 3 WMI queries the first query listens for the Queue modification events. The second query enumerates the queues the third query enumerates the messages within a queue that are over threshold. A mail is then sent over SMTP using CDOEX/CDOSYS. The script itself can be run locally or remotely as long as there is CDOEX or CDOSYS installed on avaible on the machine to send the message.
To use the script you need to configure four things within the script the first is the computer name the second is the email address its sending from the email address its sending to and the server its sending through so the following 4 lines needs to be customised.
cComputerName = "."
objEmail.From = "Queuewarnings@yourdomain.com"
objEmail.To = "somebody@yourdomain.com"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Servername"
I’ve put a download copy of the script here the script itself looks like
cComputerName = "."
MessageThreshold = 5
LastAlertSent = dateadd("h",-1,now())
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
cComputerName & "\root\MicrosoftExchangeV2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'Exchange_SMTPQueue' and TargetInstance.MessageCount >= "
& MessageThreshold)
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Wscript.echo now() & " " & objLatestEvent.TargetInstance.LinkName & " " &
objLatestEvent.TargetInstance.MessageCount & " " &
objLatestEvent.TargetInstance.Size
if LastAlertSent < dateadd("h",-1,now()) then
call EnumSMTPQueues()
LastAlertSent = now()
end if
Loop
sub EnumSMTPQueues()
Const cWMINameSpace = "root/MicrosoftExchangeV2"
Const cWMIInstance = "Exchange_SMTPQueue"
HtmlMsgbody = "<table border=""1"" width=""100%"" cellpadding=""0"" bordercolor=""#000000""><tr><td
bordercolor=""#FFFFFF"" align=""center"" bgcolor=""#000080"">" _
& "<b><font color=""#FFFFFF"">Queue Name</font></b></td><td bordercolor=""#FFFFFF""
align=""center"" bgcolor=""#000080""<b><font color=""#FFFFFF"">Message
Count</font></b></td>" _
& "<td bordercolor=""#FFFFFF"" align=""center"" bgcolor=""#000080""><b><font
color=""#FFFFFF"">Queue Size</font></b></td></tr>"
strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//"& _
cComputerName&"/"&cWMINameSpace
Set objWMIExchange = GetObject(strWinMgmts)
If Err.Number <> 0 Then
WScript.Echo "ERROR: Unable to connect to the WMI namespace."
Else
Set listExchange_PublicFolders = objWMIExchange.InstancesOf(cWMIInstance)
For Each objExchange_SMTPQueue in listExchange_PublicFolders
HtmlMsgbody = HtmlMsgbody & "<tr><td>" & objExchange_SMTPQueue.LinkName &
"</td><td>" & objExchange_SMTPQueue.MessageCount _
& "</td><td>" & objExchange_SMTPQueue.size & "</td></tr>"
WScript.echo objExchange_SMTPQueue.LinkName & " " &
objExchange_SMTPQueue.MessageCount & " " & objExchange_SMTPQueue.size
if objExchange_SMTPQueue.MessageCount >= MessageThreshold then
wql ="Select * From Exchange_QueuedSMTPMessage Where LinkId='" &
objExchange_SMTPQueue.LinkID
wql = wql & "' And LinkName='" & objExchange_SMTPQueue.Linkname & "' And
ProtocolName='SMTP' And "
wql = wql & "QueueId='" & objExchange_SMTPQueue.QueueID & "' And QueueName='" &
objExchange_SMTPQueue.Queuename &"' And"
wql = wql & " VirtualMachine='" & objExchange_SMTPQueue.VirtualMachine & "'"
wql = wql & " And VirtualServerName='" & objExchange_SMTPQueue.VirtualServerName
& "'"
quehtml = quehtml & getmess(wql)
end if
next
End If
HtmlMsgbody = HtmlMsgbody & "</table><BR><B>Message Queues</B><BR>" & quehtml
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "Queuewarnings@yourdomain.com"
objEmail.To = "somebody@yourdomain.com"
objEmail.Subject = "Queue Threshold Exceeded"
objEmail.HTMLbody = HtmlMsgbody
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")
= 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")
= "Servername"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")
= 25
objEmail.Configuration.Fields.Update
objEmail.Send
wscript.echo "message sent"
End sub
function getmess(wql)
quehtml = "<table border=""1"" width=""100%""><tr><td bgcolor=""#008000""
align=""center""><b><font color=""#FFFFFF"">Date Sent</font></b></td>" _
& "<td bgcolor=""#008000"" align=""center""><b><font color=""#FFFFFF"">Sent
By</font></b></td>"_
& " <td bgcolor=""#008000"" align=""center""><b><font color=""#FFFFFF"">Recipients</font></b></td>"_
& " <td bgcolor=""#008000"" align=""center""><b><font color=""#FFFFFF"">Subject</font></b></td>"_
& " <td bgcolor=""#008000"" align=""center""><b><font color=""#FFFFFF"">Size</font></b></td></tr>"
Const cWMINameSpace = "root/MicrosoftExchangeV2"
strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//" & cComputerName &
"/" & cWMINameSpace
Set objWMIExchange = GetObject(strWinMgmts)
Set listExchange_MessageQueueEntries = objWMIExchange.ExecQuery(wql)
For each objExchange_MessageQueueEntries in listExchange_MessageQueueEntries
recieved =
dateadd("h",toffset,cdate(DateSerial(Left(objExchange_MessageQueueEntries.Received,
4), Mid(objExchange_MessageQueueEntries.Received, 5, 2),
Mid(objExchange_MessageQueueEntries.Received, 7, 2)) & " " &
timeserial(Mid(objExchange_MessageQueueEntries.Received, 9,
2),Mid(objExchange_MessageQueueEntries.Received, 11,
2),Mid(objExchange_MessageQueueEntries.Received,13, 2))))
Wscript.echo recieved & " " & objExchange_MessageQueueEntries.Sender & " " &
objExchange_MessageQueueEntries.Subject _
& " " & objExchange_MessageQueueEntries.size & " " &
replace(replace(objExchange_MessageQueueEntries.Recipients(0),vbcrlf,""),"Envelope
Recipients:","")
quehtml = quehtml & "<tr><td>" & recieved &"</td><td>" &
objExchange_MessageQueueEntries.Sender & "</td><td>" &
replace(replace(objExchange_MessageQueueEntries.Recipients(0),vbcrlf,""),"Envelope
Recipients:","") & "</td><td>" _
& objExchange_MessageQueueEntries.Subject & "</td><td>" &
objExchange_MessageQueueEntries.size & "</td></tr>"
next
quehtml = quehtml & "</table>"
getmess = quehtml
end function