-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntp_rrd
executable file
·113 lines (89 loc) · 3.54 KB
/
ntp_rrd
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
from leobodnar import *
from os import system
from time import sleep, time
import json
last_hits = 0
last_ts1 = 0
last_ts0 = 0
INSTRUMENT_IP = '10.155.155.50'
FILE_FULLCOUNT = '.leontp_full_count'
FILE_DEVICECOUNT = '.leontp_device_count'
FILE_DEVICEUPTIME = '.leontp_device_uptime'
FILE_DEVICEOVERFLOWS = '.leontp_device_overflows'
OUTPUT_FILE = 'htdocs/ntp_status.json'
def count_save(new_count, _filename):
try:
with open(_filename, 'w+') as count_file:
count_file.write(str(new_count))
except Exception as e:
print("Failed to write count file (%s): %s" % (_filename,e))
def count_load(_filename):
loaded_count = 0
try:
with open(_filename, 'r') as count_file:
loaded_count = int(count_file.read())
except Exception as e:
loaded_count = 0
count_save(loaded_count, _filename)
return loaded_count
full_count = count_load(FILE_FULLCOUNT)
device_count = count_load(FILE_DEVICECOUNT)
device_uptime = count_load(FILE_DEVICEUPTIME)
device_overflows = count_load(FILE_DEVICEOVERFLOWS)
while True:
# Try hard to run at the top of every second
newtime = int(time()) + 1
while int(time()) < newtime:
sleep(0.001)
stats = leontp_stats(INSTRUMENT_IP)
if stats is not None:
try:
while stats.ref_ts1 == last_ts1:
sleep(0.01)
stats = leontp_stats(INSTRUMENT_IP)
if(stats.uptime < device_uptime):
# Device has rebooted, get new raw device count
device_overflows = 0
# Put existing device count on full
full_count += device_count
# Use new raw count on device
device_count = stats.NTP_served
elif stats.NTP_served < (device_count - (device_overflows * (2**32))):
# Device has overflowed
device_overflows += 1
device_count = stats.NTP_served + (device_overflows * (2**32))
else:
# Normal increment
device_count = stats.NTP_served + (device_overflows * (2**32))
device_uptime = stats.uptime
count_save(full_count, FILE_FULLCOUNT)
count_save(device_uptime, FILE_DEVICEUPTIME)
count_save(device_count, FILE_DEVICECOUNT)
count_save(device_overflows, FILE_DEVICEOVERFLOWS)
avg_hits = int((stats.NTP_served - last_hits) / ((stats.ref_ts1 - last_ts1) + (stats.ref_ts0 - last_ts0) / 4294967296.0 ) + 0.5)
if (avg_hits > 0) and ((stats.ref_ts1 - last_ts1) < 120):
callargs = "rrdtool update ntp1.rrd " + str(last_ts1 - TIME1970) + ":" + str(avg_hits) # timestamp is beginning of interval
system(callargs)
last_hits = stats.NTP_served
last_ts1 = stats.ref_ts1
last_ts0 = stats.ref_ts0
except Exception as e:
print(e)
outputObject = {
'ntp-response-datetime': '%s' % stats.timestamp,
'ntp-response-timestamp': '%.3f' % (stats.ref_ts1 + stats.ref_ts0),
'ntp-requests-total': (full_count+device_count),
'ntp-requests-rate': round(device_count / device_uptime, 1),
'ntp-gps-uptime-seconds': stats.lock_time,
'ntp-gps-uptime-days': round(stats.lock_time / 86400, 1),
'ntp-requests-device': device_count,
'ntp-gps-flags': '0x%02x' % stats.flags,
'ntp-gps-svs': stats.numSV,
'ntp-device-uptime-seconds': stats.uptime,
'ntp-device-uptime-days': round(stats.uptime / 86400, 1),
'ntp-device-firmware': '%2x.%02x' % (stats.FW_ver>>8, stats.FW_ver&0xFF),
'ntp-device-serial': stats.ser_num
}
with open(OUTPUT_FILE, 'w', encoding='utf-8') as file_out:
json.dump(outputObject, file_out, ensure_ascii=False, indent=2)