10/09: Monitoring e-mail service availability
I just found a bunch of scripts from way back in time. One of them is particularly interesting. It is a script that monitors the availability of the e-mail service. This might be useful for some of you, so I decided to share it here. The script monitors the availability of the e-mail service by sending an e-mail message, and then making sure that the message arrived in the mailbox it was sent to. If the message doesn't arrive within a predefined time, an SMS will be sent using Microsoft's SMS sender program. This means that you need to have a mobile phone connected to your computer, but the script could be modified to start any program if the message didn't arrive on time. I wouldn't recommend sending an e-mail though...
The script uses w3sockets (socketreg.exe & socket.dll) from http://www.dimac.net. I won't post those files here, but you can download them from here.
Also, the script uses Microsoft's SMS sender for sending the SMS messages using a mobile phone connected to the computer that runs the script. The SMS sender program can be found here.
The SMS sender program is kind of problematic. When you send an SMS it will throw a notification on the screen. I've used pskill.exe (part of the pstools, available here) to kill the SMS sender program and its notifications.
And finally, the script itself is shown below...
'This script sends e-mail using SMTP, and then checks that the messages have arrived in the Excgange server mailbox using POP3.
'The purpose of the script is to monitor SMTP mail flow and alert if there is a problem in the mail flow.
'The script is dependent upon the socket.dll, which should be registered on the server (by running SocketReg.exe).
'The required files should be kept in the same dir as this script, in order to be available.
'This script also utilizes Microsoft's free smssender.exe to send alerts using a mobile phone connected to the computer.
'Also pskill.exe is used to kill the smssender.exe's notification.
'The following variables may be changed:
arrSMTPServers = array("SERVER1", "SERVER2") 'Put your servers here
intSMTPPort = 25 'Set the port to use for sending SMTP mail
strFromAddr = "youraddress@yourdomain.com" 'Set the sender address for the messages
strToAddr = "destinationaddress@yourdomain.com" 'Set the recipient address for the messages
strPOPServer = "popserver.yourdomain.com" 'Set the POP server
intPOPPort = 110 'Set the POP server port
strSMSSender = "c:\Program files\Microsoft SMS Sender\smssender.exe" 'Path to smssender.exe
strPhoneNumber = "5551234" 'Phone number to send alert messages to
strPSKill = "c:\temp\pskill.exe" 'Path to pskill.exe
strExpectedGreeting = "Microsoft Exchange" 'Expected greeting text of POP server
strPOPUser = "username" 'Username for the POP mailbox
strPOPPass = "password" 'Password for the POP mailbox
'Do not change anything below this line!
'---------------------------------------
Dim arrID() 'Array, containing the ID's for the messages to send.
On Error Resume Next
arrPath = Split(strSMSSender, "\")
strSMSexe = arrPath(Ubound(arrPath))
For i=0 to Ubound(arrSMTPServers)
'Generate ID's for the messages to send:
ReDim Preserve arrID(i)
arrID(i) = fGenerateID
'Send message:
subSendMail arrSMTPServers(i), intSMTPPort, strFromAddr, strToAddr, arrID(i), arrID(i)
Next
'Sleep for a while to allow time for the messages to be scanned and delivered:
Wscript.sleep 20000
For i=0 to Ubound(arrSMTPServers)
'Read mailbox, and search for our messages:
If fReadPOPMsgs (arrID(i), strPOPServer, intPOPPort, strExpectedGreeting, strPOPUser, strPOPPass) = 1 Then
Wscript.echo "Inbound SMTP route through " & arrSMTPServers(i) & " is OK!"
Else
Wscript.echo "Inbound SMTP route through " & arrSMTPServers(i) & " is DOWN!"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run (Chr(34) & strSMSSender & Chr(34) & " /p:" & strPhoneNumber & " /m:" & Chr(34) & "Inbound SMTP route through " & arrSMTPServers(i) & " is DOWN!" & Chr(34)),0
wscript.sleep 30000
objShell.Run (Chr(34) & strPSKill & Chr(34) & " " & strSMSexe)
End If
Next
fDelPOPMsgs strPOPServer, intPOPPort, strExpectedGreeting, strPOPUser, strPOPPass
'***************** subSendMail() **********************
Sub subSendMail(server, port, sender, recipient, subject, body)
Set objEmail = CreateObject("CDO.Message")
objEmail.From = sender
objEmail.To = recipient
objEmail.Subject = subject
objEmail.Textbody = body
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = server
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = port
objEmail.Configuration.Fields.Update
objEmail.Send
End Sub
'***************** fGenerateID() **********************
Function fGenerateID()
Randomize
b = 0
ID = ""
Do while b <= 40
ID = ID & Int(10 * Rnd)
b = b + 1
Loop
fGenerateID = ID
End Function
'***************** fReadPOPMsgs() **********************
Function fReadPOPMsgs(identifier, strPOPServer, intPOPPort, strExpectedGreeting, strPOPUser, strPOPPass)
Dim oSocket, iErr, sSocketText
sSocketText = ""
a = 1
strStat = ""
Set oSocket = CreateObject("Socket.TCP")
oSocket.DoTelnetEmulation = True
oSocket.TelnetEmulation = "TTY"
oSocket.Host = strPOPServer & ":" & intPOPPort
On Error Resume Next
oSocket.Open
iErr = Err.Number
If iErr <> 0 Then
fReadPOPMsgs = 0
Exit Function
End If
sSocketText = oSocket.GetLine
If InStr(sSocketText, strExpectedGreeting) = 0 Then WScript.Echo sSocketText
oSocket.SendLine "user " & strPOPUser
sSocketText = oSocket.GetLine
If InStr(sSocketText,"OK") = 0 Then WScript.Echo sSocketText
oSocket.SendLine "pass " & strPOPPass
sSocketText = oSocket.GetLine
If InStr(sSocketText,"User successfully logged on") = 0 Then WScript.Echo sSocketText
oSocket.SendLine "stat"
sSocketText = oSocket.GetLine
strStat = sSocketText
arrStat = Split(strStat, " ")
Do while Int(arrStat(1)) >= a
sSocketText = ""
oSocket.SendLine "retr " & a
Do while sSocketText <> "."
sSocketText = oSocket.GetLine
If InStr(sSocketText, identifier) <> 0 Then fReadPOPMsgs = 1
'WScript.Echo sSocketText
Loop
a = a + 1
Loop
oSocket.SendLine "quit"
oSocket.Close
On Error GoTo 0
'fReadPOPMsgs = 0
End Function
'***************** fDelPOPMsgs() **********************
Function fDelPOPMsgs(strPOPServer, intPOPPort, strExpectedGreeting, strPOPUser, strPOPPass)
Dim oSocket, iErr, sSocketText
sSocketText = ""
a = 1
strStat = ""
Set oSocket = CreateObject("Socket.TCP")
oSocket.DoTelnetEmulation = True
oSocket.TelnetEmulation = "TTY"
oSocket.Host = strPOPServer & ":" & intPOPPort
On Error Resume Next
oSocket.Open
iErr = Err.Number
If iErr <> 0 Then
fReadPOPMsgs = 0
Exit Function
End If
sSocketText = oSocket.GetLine
If InStr(sSocketText, strExpectedGreeting) = 0 Then WScript.Echo sSocketText
oSocket.SendLine "user " & strPOPUser
sSocketText = oSocket.GetLine
If InStr(sSocketText,"OK") = 0 Then WScript.Echo sSocketText
oSocket.SendLine "pass " & strPOPPass
sSocketText = oSocket.GetLine
If InStr(sSocketText,"User successfully logged on") = 0 Then WScript.Echo sSocketText
oSocket.SendLine "stat"
sSocketText = oSocket.GetLine
strStat = sSocketText
arrStat = Split(strStat, " ")
a=1
Do while Int(arrStat(1)) >= a
sSocketText = ""
oSocket.SendLine "dele " & a
sSocketText = oSocket.GetLine
' wscript.echo sSocketText
a = a + 1
Loop
oSocket.SendLine "quit"
oSocket.Close
On Error GoTo 0
fDelPOPMsgs = 0
End Function
The script uses w3sockets (socketreg.exe & socket.dll) from http://www.dimac.net. I won't post those files here, but you can download them from here.
Also, the script uses Microsoft's SMS sender for sending the SMS messages using a mobile phone connected to the computer that runs the script. The SMS sender program can be found here.
The SMS sender program is kind of problematic. When you send an SMS it will throw a notification on the screen. I've used pskill.exe (part of the pstools, available here) to kill the SMS sender program and its notifications.
And finally, the script itself is shown below...
'This script sends e-mail using SMTP, and then checks that the messages have arrived in the Excgange server mailbox using POP3.
'The purpose of the script is to monitor SMTP mail flow and alert if there is a problem in the mail flow.
'The script is dependent upon the socket.dll, which should be registered on the server (by running SocketReg.exe).
'The required files should be kept in the same dir as this script, in order to be available.
'This script also utilizes Microsoft's free smssender.exe to send alerts using a mobile phone connected to the computer.
'Also pskill.exe is used to kill the smssender.exe's notification.
'The following variables may be changed:
arrSMTPServers = array("SERVER1", "SERVER2") 'Put your servers here
intSMTPPort = 25 'Set the port to use for sending SMTP mail
strFromAddr = "youraddress@yourdomain.com" 'Set the sender address for the messages
strToAddr = "destinationaddress@yourdomain.com" 'Set the recipient address for the messages
strPOPServer = "popserver.yourdomain.com" 'Set the POP server
intPOPPort = 110 'Set the POP server port
strSMSSender = "c:\Program files\Microsoft SMS Sender\smssender.exe" 'Path to smssender.exe
strPhoneNumber = "5551234" 'Phone number to send alert messages to
strPSKill = "c:\temp\pskill.exe" 'Path to pskill.exe
strExpectedGreeting = "Microsoft Exchange" 'Expected greeting text of POP server
strPOPUser = "username" 'Username for the POP mailbox
strPOPPass = "password" 'Password for the POP mailbox
'Do not change anything below this line!
'---------------------------------------
Dim arrID() 'Array, containing the ID's for the messages to send.
On Error Resume Next
arrPath = Split(strSMSSender, "\")
strSMSexe = arrPath(Ubound(arrPath))
For i=0 to Ubound(arrSMTPServers)
'Generate ID's for the messages to send:
ReDim Preserve arrID(i)
arrID(i) = fGenerateID
'Send message:
subSendMail arrSMTPServers(i), intSMTPPort, strFromAddr, strToAddr, arrID(i), arrID(i)
Next
'Sleep for a while to allow time for the messages to be scanned and delivered:
Wscript.sleep 20000
For i=0 to Ubound(arrSMTPServers)
'Read mailbox, and search for our messages:
If fReadPOPMsgs (arrID(i), strPOPServer, intPOPPort, strExpectedGreeting, strPOPUser, strPOPPass) = 1 Then
Wscript.echo "Inbound SMTP route through " & arrSMTPServers(i) & " is OK!"
Else
Wscript.echo "Inbound SMTP route through " & arrSMTPServers(i) & " is DOWN!"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run (Chr(34) & strSMSSender & Chr(34) & " /p:" & strPhoneNumber & " /m:" & Chr(34) & "Inbound SMTP route through " & arrSMTPServers(i) & " is DOWN!" & Chr(34)),0
wscript.sleep 30000
objShell.Run (Chr(34) & strPSKill & Chr(34) & " " & strSMSexe)
End If
Next
fDelPOPMsgs strPOPServer, intPOPPort, strExpectedGreeting, strPOPUser, strPOPPass
'***************** subSendMail() **********************
Sub subSendMail(server, port, sender, recipient, subject, body)
Set objEmail = CreateObject("CDO.Message")
objEmail.From = sender
objEmail.To = recipient
objEmail.Subject = subject
objEmail.Textbody = body
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = server
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = port
objEmail.Configuration.Fields.Update
objEmail.Send
End Sub
'***************** fGenerateID() **********************
Function fGenerateID()
Randomize
b = 0
ID = ""
Do while b <= 40
ID = ID & Int(10 * Rnd)
b = b + 1
Loop
fGenerateID = ID
End Function
'***************** fReadPOPMsgs() **********************
Function fReadPOPMsgs(identifier, strPOPServer, intPOPPort, strExpectedGreeting, strPOPUser, strPOPPass)
Dim oSocket, iErr, sSocketText
sSocketText = ""
a = 1
strStat = ""
Set oSocket = CreateObject("Socket.TCP")
oSocket.DoTelnetEmulation = True
oSocket.TelnetEmulation = "TTY"
oSocket.Host = strPOPServer & ":" & intPOPPort
On Error Resume Next
oSocket.Open
iErr = Err.Number
If iErr <> 0 Then
fReadPOPMsgs = 0
Exit Function
End If
sSocketText = oSocket.GetLine
If InStr(sSocketText, strExpectedGreeting) = 0 Then WScript.Echo sSocketText
oSocket.SendLine "user " & strPOPUser
sSocketText = oSocket.GetLine
If InStr(sSocketText,"OK") = 0 Then WScript.Echo sSocketText
oSocket.SendLine "pass " & strPOPPass
sSocketText = oSocket.GetLine
If InStr(sSocketText,"User successfully logged on") = 0 Then WScript.Echo sSocketText
oSocket.SendLine "stat"
sSocketText = oSocket.GetLine
strStat = sSocketText
arrStat = Split(strStat, " ")
Do while Int(arrStat(1)) >= a
sSocketText = ""
oSocket.SendLine "retr " & a
Do while sSocketText <> "."
sSocketText = oSocket.GetLine
If InStr(sSocketText, identifier) <> 0 Then fReadPOPMsgs = 1
'WScript.Echo sSocketText
Loop
a = a + 1
Loop
oSocket.SendLine "quit"
oSocket.Close
On Error GoTo 0
'fReadPOPMsgs = 0
End Function
'***************** fDelPOPMsgs() **********************
Function fDelPOPMsgs(strPOPServer, intPOPPort, strExpectedGreeting, strPOPUser, strPOPPass)
Dim oSocket, iErr, sSocketText
sSocketText = ""
a = 1
strStat = ""
Set oSocket = CreateObject("Socket.TCP")
oSocket.DoTelnetEmulation = True
oSocket.TelnetEmulation = "TTY"
oSocket.Host = strPOPServer & ":" & intPOPPort
On Error Resume Next
oSocket.Open
iErr = Err.Number
If iErr <> 0 Then
fReadPOPMsgs = 0
Exit Function
End If
sSocketText = oSocket.GetLine
If InStr(sSocketText, strExpectedGreeting) = 0 Then WScript.Echo sSocketText
oSocket.SendLine "user " & strPOPUser
sSocketText = oSocket.GetLine
If InStr(sSocketText,"OK") = 0 Then WScript.Echo sSocketText
oSocket.SendLine "pass " & strPOPPass
sSocketText = oSocket.GetLine
If InStr(sSocketText,"User successfully logged on") = 0 Then WScript.Echo sSocketText
oSocket.SendLine "stat"
sSocketText = oSocket.GetLine
strStat = sSocketText
arrStat = Split(strStat, " ")
a=1
Do while Int(arrStat(1)) >= a
sSocketText = ""
oSocket.SendLine "dele " & a
sSocketText = oSocket.GetLine
' wscript.echo sSocketText
a = a + 1
Loop
oSocket.SendLine "quit"
oSocket.Close
On Error GoTo 0
fDelPOPMsgs = 0
End Function