-
Notifications
You must be signed in to change notification settings - Fork 191
/
chrony.py
executable file
·65 lines (47 loc) · 1.78 KB
/
chrony.py
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
#!/usr/bin/env python3
#
# Description: Gather metrics from Chrony NTP.
#
import subprocess
import sys
from prometheus_client import CollectorRegistry, Gauge, generate_latest
def chronyc(*args, check=True):
"""Chrony client wrapper
Returns:
(str) Data piped to stdout by the chrony subprocess.
"""
return subprocess.run(
['chronyc', *args], stdout=subprocess.PIPE, check=check
).stdout.decode('utf-8')
def chronyc_tracking():
return chronyc('-c', 'tracking').split(',')
def main():
registry = CollectorRegistry()
chrony_tracking = chronyc_tracking()
if len(chrony_tracking) != 14:
print("ERROR: Unable to parse chronyc tracking CSV", file=sys.stderr)
sys.exit(1)
g = Gauge('chrony_tracking_reference_info',
'The stratum of the current preferred source',
['ref_id', 'ref_host'],
registry=registry)
g.labels(chrony_tracking[0], chrony_tracking[1]).set(1)
g = Gauge('chrony_tracking_stratum',
'The stratum of the current preferred source',
registry=registry)
g.set(chrony_tracking[2])
g = Gauge('chrony_tracking_system_offset_seconds',
'The current estimated drift of system time from true time',
registry=registry)
g.set(chrony_tracking[4])
g = Gauge('chrony_tracking_last_offset_seconds',
'The estimated local offset on the last clock update.',
registry=registry)
g.set(chrony_tracking[5])
g = Gauge('chrony_tracking_root_dispersion_seconds',
'The absolute bound on the computer’s clock accuracy',
registry=registry)
g.set(chrony_tracking[5])
print(generate_latest(registry).decode("utf-8"), end='')
if __name__ == "__main__":
main()