summaryrefslogtreecommitdiff
path: root/contrib/ntpshmviz
blob: 5d9a228dc3b948c0aad926aee85184cd17b36529 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
# 
# ntpshmviz - graph the drift of NTP servers
# Written by Keane Wolter <daemoneye2@gmail.com>
# 
# pystripchart can be found at
# https://sourceforge.net/projects/jstripchart
#
# To do:
#
# 1. Exaggerate vertical scale so data spans about 3/4ths of graph height
#    and we can actually see features.
# 2. Try using an impulse rather than line plut - this is bursty noise, not
#    really a contour.
# 3. Exit button - WM might be a tiler like i3.
#

import array, gtk, stripchart, sys

class ntpOffset:
    def __init__(self, stream):
        # Initialize the class

        # get the data
        self.read_data(stream)

        # create the GUI for the application
        self.create_GUI()

        # enter the GTK main loop
        gtk.main()

    def create_GUI(self):
        # Creates the gui for the class

        # create a standard top-level GTK window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("NTP Offset")
        self.window.connect("destroy", gtk.mainquit)
        self.window.set_default_size(700, 400)
        self.window.show()

        # create a VBox to hold all the top-level GUI items
        self.vbox = gtk.VBox()
        self.vbox.show()
        self.window.add(self.vbox)

        # create the StripTableau and add the data to it
        self.create_StripTableau()

        # create the toolbar
        self.create_toolbar()

    def create_StripTableau(self):
        # create the striptable widget

        # gtk adjustment (value, lower, upper, step_incr, page_incr, page_size)
        hadj = gtk.Adjustment(0, 0, 1, 1, 1, self.lines)
        sel = gtk.Adjustment(-1)
        self.striptableau = stripchart.StripTableau(hadj, sel)
        self.striptableau.metawidth  = 120
        self.striptableau.gradewidth = 100
        self.vbox.pack_end(self.striptableau.widget, gtk.TRUE, gtk.TRUE)

        # Add the channel for NTP2
        # adjust the size of the graph for NTP2 to allow all the data
        # to fit within the graph
        vadj_ntp2 = gtk.Adjustment(self.ntp2_lower, self.ntp2_lower-0.1, self.ntp2_upper+0.1, 0.1, 0, self.ntp2_upper+0.1)
        ntp2_item = self.striptableau.addChannel(self.ntp2, vadj_ntp2)
        ntp2_item.name = "NTP2"
        ntp2_item.meta = self.create_text("NTP2 Offset Values")

        # add the channel for NTP3
        # adjust the size of the graph for NTP2 to allow all the data
        # to fit within the graph
        vadj_ntp3 = gtk.Adjustment(self.ntp3_lower-0.1, self.ntp3_lower-0.1, self.ntp3_upper+0.1, 0.1, 0, self.ntp3_upper+0.1)
        ntp3_item = self.striptableau.addChannel(self.ntp3, vadj_ntp3)
        ntp3_item.name = "NTP3"
        ntp3_item.meta = self.create_text("NTP3 Offset Values")

    def create_text(self, text):
        # Creates a text widget to contain a description of a channel.
        scrolled_window = gtk.ScrolledWindow()
        scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        scrolled_window.set_shadow_type(gtk.SHADOW_IN)

        textview = gtk.TextView()
        buffer   = gtk.TextBuffer()
        iter     = buffer.get_iter_at_offset(0)
        buffer.insert(iter, text)
        textview.set_buffer(buffer)
        textview.set_editable(gtk.TRUE)
        textview.set_cursor_visible(gtk.TRUE)
        textview.set_wrap_mode(gtk.WRAP_WORD)
        textview.set_left_margin(5)
        textview.set_right_margin(5)
        textview.set_pixels_above_lines(5)
        textview.set_pixels_below_lines(5)

        scrolled_window.add(textview)
        scrolled_window.show()

        return scrolled_window

    def create_toolbar(self):
        # Create the toolbar
        self.toolbar = gtk.Toolbar()
        self.toolbar.show()

        # add buttons for zoom and wire them to the StripTableau widget
        self.toolbar.insert_stock(gtk.STOCK_ZOOM_IN, "Zoom in", None,
                lambda b, w: self.striptableau.zoomIn(), self.window, -1)
        self.toolbar.insert_stock(gtk.STOCK_ZOOM_OUT, "Zoom out", None,
                lambda b, w: self.striptableau.zoomOut(), self.window, -1)
        self.toolbar.insert_stock(gtk.STOCK_ZOOM_FIT, "Zoom fit", None,
                lambda b, w: self.striptableau.zoomSel(), self.window, -1)

        # sit the toolbar inside a HandleBox so that it can be detacjed
        self.handlebox = gtk.HandleBox()
        self.handlebox.show()
        self.handlebox.add(self.toolbar)

        # pack the toolbar into the main window
        self.vbox.pack_start(self.handlebox, gtk.FALSE)

    def get_offset(self, data):
        # get the difference between the clock time and receiver time of day
        return (float(data.split(' ')[3]) - float(data.split(' ')[4]))

    def read_data(self, stream):
        # Reads data from a ntp log file.  Layout is:
        #
        #   - The keyword "sample"
        #   - The NTP unit from which it was collected.
        #   - Collection time of day, expressed in seconds
        #   - Receiver time of day, expressed in seconds
        #   - Clock time of day, expressed in seconds
        #   - Leep-second notification status
        #   - Source precision (log(2) of source jitter)

        self.ntp2       = array.array("d") # ntp2 array
        self.ntp3       = array.array("d") # ntp3 array
        self.lines      = 0                # width of graph - set to the size of the largest array
        self.ntp2_upper = 0                # highest value in ntp2 array
        self.ntp2_lower = 0                # lowest value in ntp2 array
        self.ntp3_upper = 0                # highest value in ntp3 array
        self.ntp3_lower = 0                # lowest value in ntp3 array
        offset          = 0                # offset value to add to the array and to check the upper and lower bounds of the graph

        for line in stream:
            if len(line.split(' ')) > 6:
                if 'NTP2' in line:
                    offset = self.get_offset(line)
                    self.ntp2.append(offset)
                    if offset > self.ntp2_upper:
                        self.ntp2_upper = round(offset, 5)
                    if offset < self.ntp2_lower:
                        self.ntp2_lower = round(offset, 5)
                if 'NTP3' in line:
                    offset = self.get_offset(line)
                    self.ntp3.append(offset)
                    if offset > self.ntp3_upper:
                        self.ntp3_upper = round(offset, 5)
                    if offset < self.ntp3_lower:
                        self.ntp3_lower = round(offset, 5)
        stream.close()

        # Get the line count for the larger of the two arrays.
        # This will set the width of the graph when it is displayed.
        if len(self.ntp2) > len(self.ntp3):
            self.lines = len(self.ntp2)
        else:
            self.lines = len(self.ntp3)

# Run the class
if __name__ == "__main__":
    # instantiate the application
    ntpOffset(sys.stdin)