Page 1 of 1

Check_Time check_nrpe always OK

Posted: Tue Oct 27, 2015 12:30 pm
by SavaSC
Hello,

I have been tasked with getting a check created that verifies our web servers are in sync with our local time server. I've got the check working. However, no matter how off the Host's time, Nagios always returns an "OK" result. Here is what I have.

Service

Code: Select all

define service {
	service_description		NTP sync
	use				Sava Service Settings
	hostgroup_name			SavaCentral Production Servers
	display_name			Win Time Sync
	check_command			check_nrpe!check_time!!!!!!!
	register			1
	}
NSC.ini

Code: Select all

[External Scripts]
check_time=cscript.exe //T:30 //NoLogo scripts\check_time.vbs time.ltcsvc.com 30 60
check_time.vbs

Code: Select all

' Author: Dmitry Vayntrub ([email protected])
' Loosely based off the check_ad_time.vbs by Mattias Ryrlén ([email protected]) 
' Version: 1.01 17/01/2010
' Description: Check time offset of a Windows server against NTPD server(s).


Set Args = WScript.Arguments
If WScript.Arguments.Count < 3 Then
Err = 3
WScript.Echo "check_time.vbs V1.01"
WScript.Echo "Usage: cscript /NoLogo check_time.vbs serverlist warn crit [biggest]"
Wscript.Echo ""
Wscript.Echo "Options:"
Wscript.Echo " serverlist (required): one or more server names, coma-separated"
Wscript.Echo " warn  (required): warning offset in seconds, can be partial"
Wscript.Echo " crit  (required): critical offset in seconds, can be partial"
Wscript.Echo " biggest (optional): if multiple servers, else use default least offset" 
Wscript.Echo ""
Wscript.Echo "Example:"
Wscript.Echo "cscript /NoLogo check_time.vbs myserver1,myserver2 0.4 5 biggest"
Wscript.Quit(Err)
End If

serverlist = Args.Item(0)
warn = Args.Item(1)
crit = Args.Item(2)
If WScript.Arguments.Count > 3 Then
	criteria = Args.Item(3)
  Else
	criteria = least
End If

Set objShell = CreateObject("Wscript.Shell")
strCommand = "%SystemRoot%\System32\w32tm.exe /monitor /computers:" & serverlist
set objProc = objShell.Exec(strCommand)

input = ""
strOutput = ""
Do While Not objProc.StdOut.AtEndOfStream
	input = objProc.StdOut.ReadLine
	If InStr(input, "NTP") Then
		strOutput = strOutput & input
	End If
Loop

Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = " NTP: ([+-][0-9]+\.[0-9]+)s"
Set myMatches = myRegExp.Execute(strOutput)

result = ""
If myMatches(0).SubMatches(0) <> "" Then
	result = myMatches(0).SubMatches(0)
End If
For Each myMatch in myMatches
	If myMatch.SubMatches(0) <> "" Then
		If criteria = "biggest" Then
			If abs(result) < Abs(myMatch.SubMatches(0)) Then
				result = myMatch.SubMatches(0)
			End If
		Else
			If abs(result) > Abs(myMatch.SubMatches(0)) Then
				result = myMatch.SubMatches(0)
			End If
		End If
	End If
'	Wscript.Echo myMatch.SubMatches(0) & " -debug"
Next

If result = "" Then
	Err = 3
	Status = "UNKNOWN"
  ElseIf result > crit Then
	Err = 2
	status = "CRITICAL"
  ElseIf result > warn Then
	Err = 1
	status = "WARNING"
  Else
	Err = 0
	status = "OK"
End If

Wscript.Echo "NTP " & status & ": Offset " & result & " secs|'offset'=" & result & "s;" & warn & ";" & crit & ";"
Wscript.Quit(Err)
Any ideas on where I've gone wrong?

Thanks!

Re: Check_Time check_nrpe always OK

Posted: Tue Oct 27, 2015 1:54 pm
by tgriep
Replace your script with the following updated script.

Code: Select all

' Author: Dmitry Vayntrub ([email protected])
' Loosely based off the check_ad_time.vbs by Mattias Ryrlén ([email protected]) 
' Version: 1.01 17/01/2010
' Description: Check time offset of a Windows server against NTPD server(s).


Set Args = WScript.Arguments
If WScript.Arguments.Count < 3 Then
Err = 3
WScript.Echo "check_time.vbs V1.01"
WScript.Echo "Usage: cscript /NoLogo check_time.vbs serverlist warn crit [biggest]"
Wscript.Echo ""
Wscript.Echo "Options:"
Wscript.Echo " serverlist (required): one or more server names, coma-separated"
Wscript.Echo " warn  (required): warning offset in seconds, can be partial"
Wscript.Echo " crit  (required): critical offset in seconds, can be partial"
Wscript.Echo " biggest (optional): if multiple servers, else use default least offset" 
Wscript.Echo ""
Wscript.Echo "Example:"
Wscript.Echo "cscript /NoLogo check_time.vbs myserver1,myserver2 0.4 5 biggest"
Wscript.Quit(Err)
End If

serverlist = Args.Item(0)
warn = Args.Item(1)
crit = Args.Item(2)
If WScript.Arguments.Count > 3 Then
	criteria = Args.Item(3)
  Else
	criteria = least
End If

Set objShell = CreateObject("Wscript.Shell")
strCommand = "%SystemRoot%\System32\w32tm.exe /monitor /nowarn /computers:" & serverlist
set objProc = objShell.Exec(strCommand)

input = ""
strOutput = ""
Do While Not objProc.StdOut.AtEndOfStream
	input = objProc.StdOut.ReadLine
	If InStr(input, "NTP") Then
		strOutput = strOutput & input
	End If
Loop

Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = " NTP: ([+-][0-9]+\.[0-9]+)s"
Set myMatches = myRegExp.Execute(strOutput)

result = ""
If myMatches(0).SubMatches(0) <> "" Then
	result = myMatches(0).SubMatches(0)
End If
For Each myMatch in myMatches
	If myMatch.SubMatches(0) <> "" Then
		If criteria = "biggest" Then
			If abs(result) < Abs(myMatch.SubMatches(0)) Then
				result = myMatch.SubMatches(0)
			End If
		Else
			If abs(result) > Abs(myMatch.SubMatches(0)) Then
				result = myMatch.SubMatches(0)
			End If
		End If
	End If
'	Wscript.Echo myMatch.SubMatches(0) & " -debug"
Next

If result = "" Then
Err = 3
Status = "UNKNOWN"
ElseIf Abs(result) > Abs(crit) Then
Err = 2
status = "CRITICAL"
ElseIf Abs(result) > Abs(warn) Then
Err = 1
status = "WARNING"
Else
Err = 0
status = "OK"
End If 

Wscript.Echo "NTP " & status & ": Offset " & result & " secs|'offset'=" & result & "s;" & warn & ";" & crit & ";"
Wscript.Quit(Err)
I found the information for the change on the Exchange Site for comparing sub seconds. After the changes, the plugin functioned correctly.
https://exchange.nagios.org/directory/U ... 29/details

Re: Check_Time check_nrpe always OK

Posted: Tue Oct 27, 2015 3:01 pm
by SavaSC
Thanks, that fixed it!!

Re: Check_Time check_nrpe always OK

Posted: Tue Oct 27, 2015 3:03 pm
by rkennedy
Glad to see that worked for you. I will now close this thread, but feel free to open another if you need more assistance.