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 NextConst wbemFlagReturnImmediately = &h10Const wbemFlagForwardOnly = &h20Set objArgs = WScript.Argumentsif objArgs.count > 0 then addr=objArgs(0)else addr = "localhost"end ifstrComputer="localhost"pingstatus=0wmistatus=0Set 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 colItemsif IsNull(objItem.StatusCode) thenpingstatus=0elsepingstatus=1end ifNextSet 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 colItemsRif IsNull(objItem.StatusCode) thenwmistatus=0elsewmistatus=1end ifNext if (wmistatus=0 or pingstatus=0) thenwmistatus=0pingstatus=0WScript.Sleep 10000Set 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 colItemsNif IsNull(objItem.StatusCode) thenpingstatus=0elsepingstatus=1end ifNextSet 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 colItemsNRif IsNull(objItem.StatusCode) thenwmistatus=0elsewmistatus=1end ifNext if (wmistatus=0 or pingstatus=0) thenConst 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 authenticateConst cdoBasic = 1 'basic (clear-text) authenticationConst cdoNTLM = 2 'NTLMSet objMessage = CreateObject("CDO.Message") objMessage.Subject = "Brak odpowiedzi z serwera " & addrobjMessage.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 ServerobjMessage.Configuration.Fields.Item _("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "dfs2"'Type of authentication, NONE, Basic (Base64 encoded), NTLMobjMessage.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") = 60objMessage.Configuration.Fields.Update'==End remote SMTP server configuration section==objMessage.Send end ifend ifwscript.echo wmistatuswscript.echo pingstatuswscript.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
Remember Me