-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_gps_time
99 lines (84 loc) · 2.91 KB
/
check_gps_time
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import argparse
import gps
import time
import os
import sys
#-------------------------------------------------------------------------------
_candebug = False
def candebug():
global _candebug
return _candebug
def setcandebug(value):
global _candebug
_candebug = value
def infomsg(msg):
if candebug() == True:
print(msg, flush=True)
def exitnagios(status,message):
if status=="OK":
exitcode = 0
elif status=="WARNING":
exitcode = 1
elif status=="CRITICAL":
exitcode = 2
elif status=="UNKNOWN":
exitcode = 3
else:
exitcode = 4
print(status+": "+message, flush=True)
exit(exitcode)
#-------------------------------------------------------------------------------
#collect arguments
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--timeout", dest="timeout", default="90:CRITICAL", help="timeout value:STATUS")
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
return args
def dogpstimecheck(timeoutvalue,timeoutstatus):
gpsd = gps.gps(mode=gps.watch_options.WATCH_ENABLE|gps.watch_options.WATCH_NEWSTYLE)
timeout = False
gps_time = None
try:
start = time.time()
while True:
if gpsd.waiting():
if gpsd.read() == 0:
report = gpsd.data
infomsg(report)
if report['class'] == 'TPV':
test_time = report.get('time','')
if (test_time != ''):
gps_time = test_time
break
if time.time() > start + timeoutvalue:
timeout = True
break
else:
time.sleep(1)
except Exception as e:
exitnagios("UNKNOWN","internal error - "+str(e))
finally:
if timeout:
exitnagios(timeoutstatus,"GPS time not received")
elif (gps_time==None):
exitnagios("WARNING","GPS data cannot be parsed: "+str(gps_time))
else:
exitnagios("OK","GPS time is: "+str(gps_time))
def main():
args = parse_args()
setcandebug(args.debug)
parts = args.timeout.split(":")
timeoutvalue = int(parts[0])
if len(parts)>1:
timeoutstatus = parts[1]
else:
timeoutstatus = "CRITICAL"
if timeoutstatus in ["OK", "WARNING", "CRITICAL", "UNKNOWN"]:
dogpstimecheck(timeoutvalue,timeoutstatus)
else:
exitnagios("CRITICAL","timeout status is not valid")
main()
#-------------------------------------------------------------------------------