Skip to content

Instantly share code, notes, and snippets.

@willejs
Created June 27, 2013 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willejs/5873147 to your computer and use it in GitHub Desktop.
Save willejs/5873147 to your computer and use it in GitHub Desktop.
check_ntp.py checks ntp time
#!/usr/bin/python
import re
import sys
import os
import getopt
def help():
print ''
print 'Usage: check_ntp.py -b ntpq_binary'
print ''
print '-b ntpq binary (Default: /usr/sbin/ntpq)'
print '-w offset warning threshold in ms (Default: 300000 ms)'
print '-c offset critical threshold in ms (Default: 900000 ms)'
print ''
sys.exit(3)
try:
opts, args = getopt.getopt(sys.argv[1:], "ho:b:w:c:", ["help", "binary", "warning", "critical"])
except getopt.GetoptError, err:
# print help information and exit:
help()
ntpq='/usr/bin/ntpq'
warning = '300000'
critical = '900000'
output = None
verbose = False
for o, a in opts:
if o in ("-h", "--help"):
help()
elif o in ("-b", "--binary"):
ntpq = a
elif o in ("-w", "--warning"):
warning = a
elif o in ("-c", "--critical"):
critical = a
else:
assert False, "unhandled option"
help()
comp = os.path.isfile(ntpq)
if comp:
p = os.popen(ntpq + ' -c peers')
f = p.readlines()
flag = 0
synchronized = False
desfasado = False
desfase = 0
local = False
serverokfound = False
for line in f:
if flag < 2:
flag = flag + 1
else:
try:
list = line.split()
server = list[0]
st = list[2]
offset = list[8]
when = list[4]
when = int(when)
poll = list[5]
poll = int(poll)
offset = float(offset)
if serverokfound == False:
if re.search ('\*LOCAL', server):
local = True
elif local == False:
cmp = re.search ('LOCAL', server)
if cmp == None:
if int(st) < 16:
synchronized = True
if offset > int(warning):
if offset > int(critical):
synchronized = False
if desfasado == False:
desfase = offset
elif desfase > offset:
desfase = offset
desfasado = True
elif re.search ('\*', line):
serverokfound = True
desfasado = False
desfase = offset
except:
pass
if local == True:
print "NTP CRITICAL - Synchronized locally | Status=2;1;2;0;3"
sys.exit(2)
elif synchronized == False:
print "NTP CRITICAL - Unsynchronized | Status=2;1;2;0;3 Delay=" + desfase.__str__() + ";300000;900000;0;"
sys.exit(2)
elif synchronized == True:
if desfasado == True:
print "NTP WARNING - Synchronized but late: " + desfase.__str__() + " (ms) | Status=1;1;2;0;3 Delay=" + desfase.__str__() + ";300000;900000;0;"
sys.exit(1)
elif desfasado == False:
print "NTP OK - Synchronized : " + desfase.__str__() + " (ms) | Status=1;1;2;0;3 Delay=" + desfase.__str__() + ";300000;900000;0;"
sys.exit(0)
else:
print "NTP UNKNOWN - Can\'t find " + ntpq + " on system | Status=3;1;2;0;3"
sys.exit(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment