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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
# -*- coding: utf-8 -*-
# Description: ntpd netdata python.d module
# Author: Sven Mäder (rda0)
# Author: Ilya Mashchenko (ilyam8)
# SPDX-License-Identifier: GPL-3.0-or-later
import re
import struct
from bases.FrameworkServices.SocketService import SocketService
# NTP Control Message Protocol constants
MODE = 6
HEADER_FORMAT = '!BBHHHHH'
HEADER_LEN = 12
OPCODES = {
'readstat': 1,
'readvar': 2
}
# Maximal dimension precision
PRECISION = 1000000
# Static charts
ORDER = [
'sys_offset',
'sys_jitter',
'sys_frequency',
'sys_wander',
'sys_rootdelay',
'sys_rootdisp',
'sys_stratum',
'sys_tc',
'sys_precision',
'peer_offset',
'peer_delay',
'peer_dispersion',
'peer_jitter',
'peer_xleave',
'peer_rootdelay',
'peer_rootdisp',
'peer_stratum',
'peer_hmode',
'peer_pmode',
'peer_hpoll',
'peer_ppoll',
'peer_precision'
]
CHARTS = {
'sys_offset': {
'options': [None, 'Combined offset of server relative to this host', 'milliseconds',
'system', 'ntpd.sys_offset', 'area'],
'lines': [
['offset', 'offset', 'absolute', 1, PRECISION]
]
},
'sys_jitter': {
'options': [None, 'Combined system jitter and clock jitter', 'milliseconds',
'system', 'ntpd.sys_jitter', 'line'],
'lines': [
['sys_jitter', 'system', 'absolute', 1, PRECISION],
['clk_jitter', 'clock', 'absolute', 1, PRECISION]
]
},
'sys_frequency': {
'options': [None, 'Frequency offset relative to hardware clock', 'ppm', 'system', 'ntpd.sys_frequency', 'area'],
'lines': [
['frequency', 'frequency', 'absolute', 1, PRECISION]
]
},
'sys_wander': {
'options': [None, 'Clock frequency wander', 'ppm', 'system', 'ntpd.sys_wander', 'area'],
'lines': [
['clk_wander', 'clock', 'absolute', 1, PRECISION]
]
},
'sys_rootdelay': {
'options': [None, 'Total roundtrip delay to the primary reference clock', 'milliseconds', 'system',
'ntpd.sys_rootdelay', 'area'],
'lines': [
['rootdelay', 'delay', 'absolute', 1, PRECISION]
]
},
'sys_rootdisp': {
'options': [None, 'Total root dispersion to the primary reference clock', 'milliseconds', 'system',
'ntpd.sys_rootdisp', 'area'],
'lines': [
['rootdisp', 'dispersion', 'absolute', 1, PRECISION]
]
},
'sys_stratum': {
'options': [None, 'Stratum (1-15)', 'stratum', 'system', 'ntpd.sys_stratum', 'line'],
'lines': [
['stratum', 'stratum', 'absolute', 1, PRECISION]
]
},
'sys_tc': {
'options': [None, 'Time constant and poll exponent (3-17)', 'log2 s', 'system', 'ntpd.sys_tc', 'line'],
'lines': [
['tc', 'current', 'absolute', 1, PRECISION],
['mintc', 'minimum', 'absolute', 1, PRECISION]
]
},
'sys_precision': {
'options': [None, 'Precision', 'log2 s', 'system', 'ntpd.sys_precision', 'line'],
'lines': [
['precision', 'precision', 'absolute', 1, PRECISION]
]
}
}
PEER_CHARTS = {
'peer_offset': {
'options': [None, 'Filter offset', 'milliseconds', 'peers', 'ntpd.peer_offset', 'line'],
'lines': []
},
'peer_delay': {
'options': [None, 'Filter delay', 'milliseconds', 'peers', 'ntpd.peer_delay', 'line'],
'lines': []
},
'peer_dispersion': {
'options': [None, 'Filter dispersion', 'milliseconds', 'peers', 'ntpd.peer_dispersion', 'line'],
'lines': []
},
'peer_jitter': {
'options': [None, 'Filter jitter', 'milliseconds', 'peers', 'ntpd.peer_jitter', 'line'],
'lines': []
},
'peer_xleave': {
'options': [None, 'Interleave delay', 'milliseconds', 'peers', 'ntpd.peer_xleave', 'line'],
'lines': []
},
'peer_rootdelay': {
'options': [None, 'Total roundtrip delay to the primary reference clock', 'milliseconds', 'peers',
'ntpd.peer_rootdelay', 'line'],
'lines': []
},
'peer_rootdisp': {
'options': [None, 'Total root dispersion to the primary reference clock', 'ms', 'peers',
'ntpd.peer_rootdisp', 'line'],
'lines': []
},
'peer_stratum': {
'options': [None, 'Stratum (1-15)', 'stratum', 'peers', 'ntpd.peer_stratum', 'line'],
'lines': []
},
'peer_hmode': {
'options': [None, 'Host mode (1-6)', 'hmode', 'peers', 'ntpd.peer_hmode', 'line'],
'lines': []
},
'peer_pmode': {
'options': [None, 'Peer mode (1-5)', 'pmode', 'peers', 'ntpd.peer_pmode', 'line'],
'lines': []
},
'peer_hpoll': {
'options': [None, 'Host poll exponent', 'log2 s', 'peers', 'ntpd.peer_hpoll', 'line'],
'lines': []
},
'peer_ppoll': {
'options': [None, 'Peer poll exponent', 'log2 s', 'peers', 'ntpd.peer_ppoll', 'line'],
'lines': []
},
'peer_precision': {
'options': [None, 'Precision', 'log2 s', 'peers', 'ntpd.peer_precision', 'line'],
'lines': []
}
}
class Base:
regex = re.compile(r'([a-z_]+)=((?:-)?[0-9]+(?:\.[0-9]+)?)')
@staticmethod
def get_header(associd=0, operation='readvar'):
"""
Constructs the NTP Control Message header:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|LI | VN |Mode |R|E|M| OpCode | Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Status | Association ID |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Offset | Count |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
"""
version = 2
sequence = 1
status = 0
offset = 0
count = 0
header = struct.pack(HEADER_FORMAT, (version << 3 | MODE), OPCODES[operation],
sequence, status, associd, offset, count)
return header
class System(Base):
def __init__(self):
self.request = self.get_header()
def get_data(self, raw):
"""
Extracts key=value pairs with float/integer from ntp response packet data.
"""
data = dict()
for key, value in self.regex.findall(raw):
data[key] = float(value) * PRECISION
return data
class Peer(Base):
def __init__(self, idx, name):
self.id = idx
self.real_name = name
self.name = name.replace('.', '_')
self.request = self.get_header(self.id)
def get_data(self, raw):
"""
Extracts key=value pairs with float/integer from ntp response packet data.
"""
data = dict()
for key, value in self.regex.findall(raw):
dimension = '_'.join([self.name, key])
data[dimension] = float(value) * PRECISION
return data
class Service(SocketService):
def __init__(self, configuration=None, name=None):
SocketService.__init__(self, configuration=configuration, name=name)
self.order = list(ORDER)
self.definitions = dict(CHARTS)
self.port = 'ntp'
self.dgram_socket = True
self.system = System()
self.peers = dict()
self.request = str()
self.retries = 0
self.show_peers = self.configuration.get('show_peers', False)
self.peer_rescan = self.configuration.get('peer_rescan', 60)
if self.show_peers:
self.definitions.update(PEER_CHARTS)
def check(self):
"""
Checks if we can get valid systemvars.
If not, returns None to disable module.
"""
self._parse_config()
peer_filter = self.configuration.get('peer_filter', r'127\..*')
try:
self.peer_filter = re.compile(r'^((0\.0\.0\.0)|({0}))$'.format(peer_filter))
except re.error as error:
self.error('Compile pattern error (peer_filter) : {0}'.format(error))
return None
self.request = self.system.request
raw_systemvars = self._get_raw_data()
if not self.system.get_data(raw_systemvars):
return None
return True
def get_data(self):
"""
Gets systemvars data on each update.
Gets peervars data for all peers on each update.
"""
data = dict()
self.request = self.system.request
raw = self._get_raw_data()
if not raw:
return None
data.update(self.system.get_data(raw))
if not self.show_peers:
return data
if not self.peers or self.runs_counter % self.peer_rescan == 0 or self.retries > 8:
self.find_new_peers()
for peer in self.peers.values():
self.request = peer.request
peer_data = peer.get_data(self._get_raw_data())
if peer_data:
data.update(peer_data)
else:
self.retries += 1
return data
def find_new_peers(self):
new_peers = dict((p.real_name, p) for p in self.get_peers())
if new_peers:
peers_to_remove = set(self.peers) - set(new_peers)
peers_to_add = set(new_peers) - set(self.peers)
for peer_name in peers_to_remove:
self.hide_old_peer_from_charts(self.peers[peer_name])
del self.peers[peer_name]
for peer_name in peers_to_add:
self.add_new_peer_to_charts(new_peers[peer_name])
self.peers.update(new_peers)
self.retries = 0
def add_new_peer_to_charts(self, peer):
for chart_id in set(self.charts.charts) & set(PEER_CHARTS):
dim_id = peer.name + chart_id[4:]
if dim_id not in self.charts[chart_id]:
self.charts[chart_id].add_dimension([dim_id, peer.real_name, 'absolute', 1, PRECISION])
else:
self.charts[chart_id].hide_dimension(dim_id, reverse=True)
def hide_old_peer_from_charts(self, peer):
for chart_id in set(self.charts.charts) & set(PEER_CHARTS):
dim_id = peer.name + chart_id[4:]
self.charts[chart_id].hide_dimension(dim_id)
def get_peers(self):
self.request = Base.get_header(operation='readstat')
raw_data = self._get_raw_data(raw=True)
if not raw_data:
return list()
peer_ids = self.get_peer_ids(raw_data)
if not peer_ids:
return list()
new_peers = list()
for peer_id in peer_ids:
self.request = Base.get_header(peer_id)
raw_peer_data = self._get_raw_data()
if not raw_peer_data:
continue
srcadr = re.search(r'(srcadr)=([^,]+)', raw_peer_data)
if not srcadr:
continue
srcadr = srcadr.group(2)
if self.peer_filter.search(srcadr):
continue
stratum = re.search(r'(stratum)=([^,]+)', raw_peer_data)
if not stratum:
continue
if int(stratum.group(2)) > 15:
continue
new_peer = Peer(idx=peer_id, name=srcadr)
new_peers.append(new_peer)
return new_peers
def get_peer_ids(self, res):
"""
Unpack the NTP Control Message header
Get data length from header
Get list of association ids returned in the readstat response
"""
try:
count = struct.unpack(HEADER_FORMAT, res[:HEADER_LEN])[6]
except struct.error as error:
self.error('error unpacking header: {0}'.format(error))
return None
if not count:
self.error('empty data field in NTP control packet')
return None
data_end = HEADER_LEN + count
data = res[HEADER_LEN:data_end]
data_format = ''.join(['!', 'H' * int(count / 2)])
try:
peer_ids = list(struct.unpack(data_format, data))[::2]
except struct.error as error:
self.error('error unpacking data: {0}'.format(error))
return None
return peer_ids
|