Skip to content

Instantly share code, notes, and snippets.

@sbz
Created February 13, 2020 00:06
Show Gist options
  • Save sbz/ccb0f310e5ff76ac64565bc60ddec4de to your computer and use it in GitHub Desktop.
Save sbz/ccb0f310e5ff76ac64565bc60ddec4de to your computer and use it in GitHub Desktop.
tcollector ntp
#!/usr/bin/env python
import sys
import subprocess
import socket
import shlex
NTPQ_BIN='/usr/bin/ntpq'
METRICS=[
'stratum',
'offset',
'rootdelay',
'clk_jitter',
'sys_jitter',
'clk_wander',
]
class NTPError(Exception): pass
class NTPCollector(object):
def __init__(self):
self.hostname = socket.getfqdn()
self.metrics = {}
def collect(self):
ntq_command = shlex.split("{} -c 'rv 0 {}'".format(NTPQ_BIN, ','.join(METRICS)))
print("Executing {}\n".format(ntq_command))
data = subprocess.check_output(ntq_command)
data = "".join([ c.strip() for c in data])
print(data.split(','))
def main():
collector = NTPCollector()
collector.collect()
return 0
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment