Monitorowanie stanu hostów#

   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

Monday, November 14, 2005 7:27:15 PM (Central European Standard Time, UTC+01:00) #    Comments [1]  |  Trackback

 

Automatyczne powiadamianie o restarcie/shutdownie#

  Po zupdetowaniu serwery się niestety muszą zrestartować. I rzadko im to wychodzi dobrze. Dlatego zawsze warto wiedzieć kiedy serwer będzie wykonywał tą operację. W tym celu zakładamy trigera na event log systemowy , na zdarzenie 1074, za pomocą eventtrigger.exe - dostępnego kazdym systemie xp i 2k3- i podpinamy do ponizszego skryptu.

On Error Resume Next

addr = "LSD"
msgTo = "kfaz@pjwstk.edu.pl;pkrzysz@pjwstk.edu.pl"
textBody="Serwer " & addr &" wła¶nie wszedł w tryb restart/shutdown "& vbCRLF

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
strComputer ="localhost"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'System' And EventCode=1074 ", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
textBody=textBody& "Category: " & objItem.Category & vbCRLF
textBody=textBody& "CategoryString: " & objItem.CategoryString&vbCRLF
textBody=textBody& "ComputerName: " & objItem.ComputerName&vbCRLF
strData = Join(objItem.Data, ",")
textBody=textBody& "Data: " & strData& vbCRLF
textBody=textBody&"EventCode: " & objItem.EventCode& vbCRLF
textBody=textBody&"EventIdentifier: " & objItem.EventIdentifier& vbCRLF
textBody=textBody&"EventType: " & objItem.EventType& vbCRLF
strInsertionStrings = Join(objItem.InsertionStrings, ",")
textBody=textBody& "InsertionStrings: " & strInsertionStrings& vbCRLF
textBody=textBody& "Logfile: " & objItem.Logfile& vbCRLF
textBody=textBody& "Message: " & objItem.Message& vbCRLF
textBody=textBody& "RecordNumber: " & objItem.RecordNumber& vbCRLF
textBody=textBody& "SourceName: " & objItem.SourceName& vbCRLF
textBody=textBody& "TimeGenerated: " & WMIDateStringToDate(objItem.TimeGenerated)& vbCRLF
textBody=textBody& "TimeWritten: " & WMIDateStringToDate(objItem.TimeWritten)& vbCRLF
textBody=textBody& vbCRLF
textBody=textBody& vbCRLF
textBody=textBody&"by peki"& vbCRLF
Exit For
Next


oSendUsingPickup = 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 = "Serwer " & addr & " shutdown/restart"
objMessage.From = """RRdTooL Server Watch"" <pkrzysz@pjwstk.edu.pl>"
objMessage.To = msgTo
objMessage.TextBody = textBody

'==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


Function WMIDateStringToDate(dtmDate)
WScript.Echo dtm:
    WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
    Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
    & " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function

 

Skrypt ten wysyła emaila do administratora , ze maszyna przeszła w tryb restartu/shutdownu, oraz podaje odpowiedni wpis z dziennika zdarzeń. Jak mozna zauwazyc, w skrypcie sa pobierane wszystkie wartosci z logu system o id 1074, a nie tylko ostatnia - wynika to ze w WQL brakuję prostego słowa kluczowego TOP.

 

Monday, November 14, 2005 7:20:50 PM (Central European Standard Time, UTC+01:00) #    Comments [1]  |  Trackback

 

All content © 2010, Krzysztof Pietrzak
On this page
This site
Calendar
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Archives
Sitemap
Blogroll OPML
  Tokyo by night
blog WiTa
  W-Files
blog n€x¤Ra
 .:fotoblog:.
blog Kfaza
 \\archon\blog$
blog archona
 Czasowstrzymywacz
Blog Fookyego
 Jog Pstryka
Jog Pstryka
 Mac OS X vs. Active Directory
techniczny blog kfaza

Maps
Locations of visitors to this page