Podobnie jak poprzedni ten skrypt słuzy do
monitorowania stanu hostów. W tym celu próbuję pingnąć hosta
docelowego po ICMP wykorzystując mechanizm WMI, oraz pingnąć hosta
zródłowego po ICMP korzystając z zdalnego wywołania procedury po WMI.
Jako wynik dostajemy 1 jeśli ping się powiódł , i 0 jeśli zawiódł.
Oczywiście jeśli ping nie doszedł, skrypt jeszcze raz próbuje się
dostać do hosta, a jesli i ta próba zakończy się niepowodzeniem, wysyła
emaila do administratora.
On Error Resume Next
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set objArgs = WScript.Arguments
if objArgs.count > 0 then
addr=objArgs(0)
else
addr = "localhost"
end if
strComputer="localhost"
pingstatus=0
wmistatus=0
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT StatusCode FROM Win32_PingStatus where address = '"&addr&"'", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
if IsNull(objItem.StatusCode) then
pingstatus=0
else
pingstatus=1
end if
Next
Set objWMIServiceR = GetObject("winmgmts:\\" & addr & "\root\CIMV2")
Set colItemsR = objWMIServiceR.ExecQuery("SELECT StatusCode FROM Win32_PingStatus where address = '"&strComputer&"'", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItemsR
if IsNull(objItem.StatusCode) then
wmistatus=0
else
wmistatus=1
end if
Next
if (wmistatus=0 or pingstatus=0) then
wmistatus=0
pingstatus=0
WScript.Sleep 10000
Set objWMIServiceN = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItemsN = objWMIServiceN.ExecQuery("SELECT StatusCode FROM Win32_PingStatus where address = '"&addr&"'", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItemsN
if IsNull(objItem.StatusCode) then
pingstatus=0
else
pingstatus=1
end if
Next
Set objWMIServiceNR = GetObject("winmgmts:\\" & addr & "\root\CIMV2")
Set colItemsNR = objWMIServiceNR.ExecQuery("SELECT StatusCode FROM Win32_PingStatus where address = '"&strComputer&"'", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItemsNR
if IsNull(objItem.StatusCode) then
wmistatus=0
else
wmistatus=1
end if
Next
if (wmistatus=0 or pingstatus=0) then
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Brak odpowiedzi z serwera " & addr
objMessage.From = """RRdTooL Server Watch"" <pkrzysz@pjwstk.edu.pl>"
objMessage.To = "pkrzysz@pjwstk.edu.pl;kfaz@pjwstk.edu.pl"
objMessage.TextBody = "Serwer " & addr &"nie odpowiada na Pingi "&vbCRLF&_
"Trzeba sprawdzic http://peki.pjwstk.edu.pl/rrdtool "&vbCRLF&_
"zobaczyć co się dzieje i ewentualnie zrestartować."
'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "dfs2"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoNTLM
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
end if
end if
wscript.echo wmistatus
wscript.echo pingstatus
wscript.echo Date() & " " & Time()
wscript.echo "WMI ping"
wscript.echo "ICMP Ping"
Wscript.echo cmpName
Skrypcik oczywiście chodzi w Schedulerze co 10min, i jest podpiety do MRTG/rrdtool generujacego statystyki - http://peki.pjwstk.edu.pl/rrdtool