Advertisement
quanta

Untitled

Nov 18th, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' Author: Dmitry Vayntrub (dvayntrub@yahoo.com)
  2. ' Loosely based off the check_ad_time.vbs by Mattias Ryrlén (mr@op5.com)
  3. ' Version: 1.01 17/01/2010
  4. ' Description: Check time offset of a Windows server against NTPD server(s).
  5.  
  6.  
  7. Set Args = WScript.Arguments
  8. If WScript.Arguments.Count < 3 Then
  9. Err = 3
  10. WScript.Echo "check_time.vbs V1.01"
  11. WScript.Echo "Usage: cscript /NoLogo check_time.vbs serverlist warn crit [biggest]"
  12. Wscript.Echo ""
  13. Wscript.Echo "Options:"
  14. Wscript.Echo " serverlist (required): one or more server names, coma-separated"
  15. Wscript.Echo " warn  (required): warning offset in seconds, can be partial"
  16. Wscript.Echo " crit  (required): critical offset in seconds, can be partial"
  17. Wscript.Echo " biggest (optional): if multiple servers, else use default least offset"
  18. Wscript.Echo ""
  19. Wscript.Echo "Example:"
  20. Wscript.Echo "cscript /NoLogo check_time.vbs myserver1,myserver2 0.4 5 biggest"
  21. Wscript.Quit(Err)
  22. End If
  23.  
  24. serverlist = Args.Item(0)
  25. warn = Args.Item(1)
  26. crit = Args.Item(2)
  27. If WScript.Arguments.Count > 3 Then
  28.     criteria = Args.Item(3)
  29.   Else
  30.     criteria = least
  31. End If
  32.  
  33. Set objShell = CreateObject("Wscript.Shell")
  34. strCommand = "%SystemRoot%\System32\w32tm.exe /monitor /nowarn /computers:" & serverlist
  35. set objProc = objShell.Exec(strCommand)
  36.  
  37. input = ""
  38. strOutput = ""
  39. Do While Not objProc.StdOut.AtEndOfStream
  40.     input = objProc.StdOut.ReadLine
  41.     If InStr(input, "NTP") Then
  42.         strOutput = strOutput & input
  43.     End If
  44. Loop
  45.  
  46. Set myRegExp = New RegExp
  47. myRegExp.IgnoreCase = True
  48. myRegExp.Global = True
  49. myRegExp.Pattern = " NTP: ([+-][0-9]+\.[0-9]+)s"
  50. Set myMatches = myRegExp.Execute(strOutput)
  51.  
  52. result = ""
  53. If myMatches(0).SubMatches(0) <> "" Then
  54.     result = myMatches(0).SubMatches(0)
  55. End If
  56. For Each myMatch in myMatches
  57.     If myMatch.SubMatches(0) <> "" Then
  58.         If criteria = "biggest" Then
  59.             If abs(result) < Abs(myMatch.SubMatches(0)) Then
  60.                 result = myMatch.SubMatches(0)
  61.             End If
  62.         Else
  63.             If abs(result) > Abs(myMatch.SubMatches(0)) Then
  64.                 result = myMatch.SubMatches(0)
  65.             End If
  66.         End If
  67.     End If
  68. '   Wscript.Echo myMatch.SubMatches(0) & " -debug"
  69. Next
  70.  
  71. If result = "" Then
  72.     Err = 3
  73.     Status = "UNKNOWN"
  74.   ElseIf result > crit Then
  75.     Err = 2
  76.     status = "CRITICAL"
  77.   ElseIf result > warn Then
  78.     Err = 1
  79.     status = "WARNING"
  80.   Else
  81.     Err = 0
  82.     status = "OK"
  83. End If
  84.  
  85. Wscript.Echo "NTP " & status & ": Offset " & result & " secs|'offset'=" & result & "s;" & warn & ";" & crit & ";"
  86. Wscript.Quit(Err)
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement