Tuesday, September 1, 2009

Script to monitor service status

We had a requirement to monitor a service status on multiple servers and send a mail to the team when the service is no longer is available /stopped.
This script is used to monitor a particular service and uses the SMTP server to send a mail to the team using the CDO object method.
===============================
'variable declaration
dim objShell, objNetdim strToEmailAddress, strFromEmailAddress, strServiceNameToCheckdim strSMTPServer, strSMTPServerPortdim strComputerName
Set objNet = CreateObject("WScript.NetWork")
' **** Set Variables here *****
strComputerName = "server name that you need to monitor"
strToEmailAddress = "email address"
strFromEmailAddress = "email address"
strServiceNameToCheck = "service display name to monitor "
strSMTPServer = "smtp server ip address or FQDN "
strSMTPServerPort = "25"
strsub = "service status"
set objShell = CreateObject("Shell.Application")
If objShell.IsServiceRunning(strServiceNameToCheck) Then
Wscript.Quit
else
Set objMessage = CreateObject("CDO.Message")
'Send Message
objMessage.From = strFromEmailAddress
objMessage.To = strToEmailAddress
objMessage.Subject = strsub
objMessage.TextBody = "The Service '" & strServiceNameToCheck & "' stopped on " & strComputerName & " Server " & vbcrlf & "Time: " & Now
objMessage.Configuration.Fields.Item http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPserverobjMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = strSMTPServerPortobjMessage.Configuration.Fields.UpdateobjMessage.Send
End If
set objShell = nothing
set objNet = Nothing
=======================
This can be set as a scheduled task to monitor daily or depending on the schedule when required.

No comments:

Post a Comment