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