Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to monitor some third party software that runs several services on Windows. I simply want to set it up so that I get an email if the service restarts, fails, etc.

Is this something that can be easily scripted? Does someone have something basic for this task?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

This is a bit ugly, but you could probably modify it to suit your needs. Just set up a scheduled task to call the script every so often.

Dim ServiceDown
Dim Message

Function sGetServiceStatus(ByVal strServiceName)
    wbemImpersonationLevelImpersonate = 3
    wbemAuthenticationLevelPktPrivacy = 6

    Set objLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objWMIService = objLocator.ConnectServer("localhost")

    objWMIService.Security_.ImpersonationLevel = wbemImpersonationLevelImpersonate
    objWMIService.Security_.AuthenticationLevel = wbemAuthenticationLevelPktPrivacy

    Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where DisplayName ='"& strServiceName & "'")

    if(colListOfServices.Count=0) then
        sGetServiceStatus = ""
        Exit function
    end if

    For Each objItem in colListOfServices
        If objItem.DisplayName = strServiceName and objItem.State = "Running" Then
            sGetServiceStatus = objItem.State                       
            Exit Function
        else
            sGetServiceStatus = objItem.State
            ServiceDown = True
            Message = Message & vbcrlf & strServiceName
            Exit function
        End If
    Next

    sGetServiceStatus = ""
End Function

sGetServiceStatus("ISC BIND")
sGetServiceStatus("Apache2.2")
sGetServiceStatus("MySQL")

If ServiceDown Then     
    Set objMessage = CreateObject("CDO.Message") 
    objMessage.Subject = "Check Services" 
    objMessage.From = "admin@yourhost.com" 
    objMessage.To = "admin@yourhost.com" 
    objMessage.TextBody = "The following service(s) is/are down: " & Message
    objMessage.Send                 
End If
share|improve this answer
    
thanks, i will definitely try this out.. –  jdamae Jul 30 '11 at 1:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.