123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- '''
- ntpshmviz - graph the drift of NTP servers
- Written by Keane Wolter <daemoneye2@gmail.com>
- '''
- from __future__ import absolute_import, print_function, division
- import argparse
- import sys
- import numpy
- try:
- import matplotlib.pyplot as PLT
- except ImportError:
- print("Please make sure matplotlib is installed properly:",
- sys.exc_info()[0])
- sys.exit(1)
- gps_version = '3.19.1~dev'
- class ntpOffset(object):
- "The master Class"
- def __init__(self, stream):
- "Initialize class ntpOffset"
-
- self.read_data(stream)
-
- self.display()
- def display(self):
- "display the graphs"
-
- print ("Please note that the graph can be closed with the "
- "key combination of <Ctrl-w>")
- PLT.figure()
- subplot_value = 211
- for ntp_item in self.ntp_data:
-
- PLT.subplot(subplot_value)
-
- t = numpy.arange(0, self.line_counts[ntp_item], 1)
- PLT.vlines(t, 0, self.ntp_data[ntp_item], color='r')
-
- PLT.title("NTP drift for " + ntp_item)
- PLT.xlabel('sample')
- PLT.ylabel('drift')
-
-
- subplot_value += 1
-
-
- PLT.tight_layout()
- PLT.show()
- def read_data(self, stream):
- "Read data from a ntp log file."
-
-
-
-
-
-
-
-
-
- self.ntp_data = {}
- self.line_counts = {}
- record = []
- offset = 0
- self.lines = 0
- for line in stream:
- if len(line.split(' ')) > 6:
- if line[:1] != '#':
- line = line.lstrip()
- record = line.split(' ')
- try:
- offset = (numpy.longdouble(record[3]) -
- numpy.longdouble(record[4]))
- except:
- print ("Invalid data: ", sys.exc_info()[0],
- ". Data was: ", line)
- continue
-
-
-
-
- if record[1] not in self.ntp_data:
- self.ntp_data[record[1]] = []
- self.line_counts[record[1]] = 0
- self.ntp_data[record[1]].append(offset)
- self.line_counts[record[1]] += 1
- stream.close()
- def args():
- parser = argparse.ArgumentParser(description='NTP drift visualizer')
- parser.add_argument('-V', '--version',
- action='version',
- help='Get version of GPS project',
- version = "ntpshmviz: version" + str(gps_version))
- return parser.parse_args()
- if __name__ == "__main__":
- if len(sys.argv) >= 2:
- arguments = args()
- ntpOffset(sys.stdin)
- sys.exit()
|