Skip to content

Commit e04daef

Browse files
committed
Add a Python script to parse Chrony NTP clients
Use the output of `chronyc -c tracking` to provide some basic NTP client status metrics. Signed-off-by: SuperQ <[email protected]>
1 parent 414fb44 commit e04daef

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

Diff for: chrony.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Description: Gather metrics from Chrony NTP.
4+
#
5+
6+
import subprocess
7+
import sys
8+
9+
from prometheus_client import CollectorRegistry, Gauge, generate_latest
10+
11+
12+
def chronyc(*args, check=True):
13+
"""Chrony client wrapper
14+
15+
Returns:
16+
(str) Data piped to stdout by the chrony subprocess.
17+
"""
18+
return subprocess.run(
19+
['chronyc', *args], stdout=subprocess.PIPE, check=check
20+
).stdout.decode('utf-8')
21+
22+
23+
def chronyc_tracking():
24+
return chronyc('-c', 'tracking').split(',')
25+
26+
27+
def main():
28+
registry = CollectorRegistry()
29+
chrony_tracking = chronyc_tracking()
30+
31+
if len(chrony_tracking) != 14:
32+
print("ERROR: Unable to parse chronyc tracking CSV", file=sys.stderr)
33+
sys.exit(1)
34+
35+
g = Gauge('chrony_tracking_reference_info',
36+
'The stratum of the current preferred source',
37+
['ref_id', 'ref_host'],
38+
registry=registry)
39+
g.labels(chrony_tracking[0], chrony_tracking[1]).set(1)
40+
41+
g = Gauge('chrony_tracking_stratum',
42+
'The stratum of the current preferred source',
43+
registry=registry)
44+
g.set(chrony_tracking[2])
45+
46+
g = Gauge('chrony_tracking_system_offset_seconds',
47+
'The current estimated drift of system time from true time',
48+
registry=registry)
49+
g.set(chrony_tracking[4])
50+
51+
g = Gauge('chrony_tracking_last_offset_seconds',
52+
'The estimated local offset on the last clock update.',
53+
registry=registry)
54+
g.set(chrony_tracking[5])
55+
56+
g = Gauge('chrony_tracking_root_dispersion_seconds',
57+
'The absolute bound on the computer’s clock accuracy',
58+
registry=registry)
59+
g.set(chrony_tracking[5])
60+
61+
print(generate_latest(registry).decode("utf-8"), end='')
62+
63+
64+
if __name__ == "__main__":
65+
main()

0 commit comments

Comments
 (0)