我需要監控一些在 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