forked from Ratfink/ds1054z-screen-capture
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathleontp-capture
executable file
·110 lines (97 loc) · 3.82 KB
/
leontp-capture
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
#!/usr/bin/env python3
from leobodnar import *
from common import *
import os
import sys
from time import sleep, gmtime, strftime
import argparse
script_name = os.path.basename(sys.argv[0])
filepath = os.getcwd()
parser = argparse.ArgumentParser(description='LeoBodnar LeoNTP NTP Server statistics capture')
parser.add_argument("ip_address", nargs='?', default=None,
help=("IP address of a network or a host. Can be "
"IPv4 or IPv6 address"))
parser.add_argument("-t", "--timed", action='store_true',
help="Measure rate over 1s")
parser.add_argument("-g", "--graph", action='store_true',
help="Graph ongoing load")
args = parser.parse_args()
if args.ip_address is not None:
INSTRUMENT_IP = args.ip_address
else:
INSTRUMENT_IP = find_arp(LEOBODNAR_OUI)
if INSTRUMENT_IP is None:
sys.exit("Error: Hardware address not found in ARP table, IP will have to be specified manually.")
print ("Target server: %s" % INSTRUMENT_IP)
print("System time: %s" % strftime("%Y-%m-%d %H:%M:%S", gmtime()))
sntp = sntp_request(INSTRUMENT_IP)
if sntp is not None:
if sntp.stratum != 0:
print ("SNTP response: %s" % sntp.timestamp)
print ("SNTP Stratum: %d" % sntp.stratum)
else:
print ("SNTP: KoD response!")
else:
print ("Error: SNTP Request Failed.")
stats = leontp_stats(INSTRUMENT_IP)
if stats is not None:
# actual statistics received from the server
print ("NTP response: %s (%.3f)" % (stats.timestamp, (stats.ref_ts1 + stats.ref_ts0)))
# derived statistics
print ("Average load since restart: ~%01.0f requests per second" % (1.0 * stats.NTP_served / stats.uptime))
print ("NTP requests served: %d" % stats.NTP_served)
print ("Uptime: %d seconds ( %.2f days )" % (stats.uptime, stats.uptime/86400))
print ("GPS lock time: %d seconds ( %.2f days )" % (stats.lock_time, stats.lock_time/86400))
print ("GPS flags: 0x%02x" % stats.flags)
print ("Active satellites: %d" % stats.numSV)
print ("Firmware version: %x.%02x" % (stats.FW_ver>>8, stats.FW_ver&0xFF))
print ("Serial number: %d" % stats.ser_num)
else:
print ("Error: Stats Request Failed.")
if args.timed:
print ("Waiting ~1s..")
sleep(1)
stats_1s = leontp_stats(INSTRUMENT_IP)
if stats_1s is not None:
print ("Request rate: %.1f / s" % ((stats_1s.NTP_served - stats.NTP_served) / ((stats_1s.ref_ts1 + stats_1s.ref_ts0) - (stats.ref_ts1 + stats.ref_ts0))))
else:
print ("Error: Second Stats Request Failed.")
elif args.graph:
try:
print ("Waiting ~1s..")
sleep(1)
stats_next = leontp_stats(INSTRUMENT_IP)
r_rate = ((stats_next.NTP_served - stats.NTP_served) / ((stats_next.ref_ts1 + stats_next.ref_ts0) - (stats.ref_ts1 + stats.ref_ts0)))
time_width = 9
rate_width = 10
if r_rate == 0:
# Avoid divide-by-zero for targets with low load
scale = 1 / ((80-time_width-rate_width)/2.0)
else:
scale = r_rate / ((80-time_width-rate_width)/2.0)
rate_scaled = r_rate / scale
print("%s % 4.1f/s " % (strftime("%H:%M:%S", gmtime()), r_rate), end ="")
while rate_scaled > 0:
print("*", end ="")
rate_scaled = rate_scaled - 1
print("")
while True:
sleep(1)
stats_last = stats_next
try:
stats_next = leontp_stats(INSTRUMENT_IP)
except KeyboardInterrupt:
print("exiting..")
break
except:
print("Request Failed")
continue
r_rate = ((stats_next.NTP_served - stats_last.NTP_served) / ((stats_next.ref_ts1 + stats_next.ref_ts0) - (stats_last.ref_ts1 + stats_last.ref_ts0)))
rate_scaled = r_rate / scale
print("%s % 4.1f/s " % (strftime("%H:%M:%S", gmtime()), r_rate), end ="")
while rate_scaled > 0:
print("*", end ="")
rate_scaled = rate_scaled - 1
print("")
except KeyboardInterrupt:
print('exiting..')