ウィンドウサービスの監視スクリプト

ウィンドウサービスの監視スクリプト

Windows 上で複数のサービスを実行するサードパーティ ソフトウェアを監視する必要があります。サービスが再起動したり、失敗したりした場合に電子メールを受け取るように設定したいだけです。

これは簡単にスクリプト化できるものなのでしょうか? このタスクのための基本的なものを持っている人はいますか?

答え1

これは少し見苦しいですが、おそらくニーズに合わせて変更することができます。スクリプトを定期的に呼び出すスケジュールされたタスクを設定するだけです。

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 = "[email protected]" 
    objMessage.To = "[email protected]" 
    objMessage.TextBody = "The following service(s) is/are down: " & Message
    objMessage.Send                 
End If

関連情報