From 58c10628d86fa5ce6f2e57f59d7d3cbe401a5bf9 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Tue, 14 Feb 2017 16:20:53 +0100 Subject: [PATCH] Add ntpd metrics from ntpq rv Add some metrics using to the ntpd helper script using the "request value"[0] command. [0]: https://www.eecis.udel.edu/~mills/ntp/html/ntpq.html#system --- text_collector_examples/ntpd_metrics.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/text_collector_examples/ntpd_metrics.py b/text_collector_examples/ntpd_metrics.py index 1da4edb8..ab55a130 100755 --- a/text_collector_examples/ntpd_metrics.py +++ b/text_collector_examples/ntpd_metrics.py @@ -9,6 +9,7 @@ import sys # NTP peers status, with no DNS lookups. ntpq_cmd = ['ntpq', '-np'] +ntpq_rv_cmd = ['ntpq', '-c', 'rv 0 offset,sys_jitter,rootdisp,rootdelay'] # Regex to match all of the fields in the output of ntpq -np metrics_fields = [ @@ -50,12 +51,12 @@ status_types = { # Run the ntpq command. -def get_ntpq(): +def get_output(command): try: - ntpq = subprocess.check_output(ntpq_cmd, stderr=subprocess.DEVNULL) + output = subprocess.check_output(command, stderr=subprocess.DEVNULL) except subprocess.CalledProcessError as e: return None - return ntpq.decode() + return output.decode() # Print metrics in Prometheus format. @@ -63,7 +64,10 @@ def print_prometheus(metric, values): print("# HELP ntpd_%s NTPd metric for %s" % (metric, metric)) print("# TYPE ntpd_%s gauge" % (metric)) for labels in values: - print("ntpd_%s{%s} %f" % (metric, labels, values[labels])) + if labels is None: + print("ntpd_%s %f" % (metric, values[labels])) + else: + print("ntpd_%s{%s} %f" % (metric, labels, values[labels])) # Parse raw ntpq lines. @@ -81,7 +85,7 @@ def parse_line(line): # Main function def main(argv): - ntpq = get_ntpq() + ntpq = get_output(ntpq_cmd) peer_status_metrics = {} delay_metrics = {} offset_metrics = {} @@ -107,6 +111,11 @@ def main(argv): print_prometheus('offset_milliseconds', offset_metrics) print_prometheus('jitter_milliseconds', jitter_metrics) + ntpq_rv = get_output(ntpq_rv_cmd) + for metric in ntpq_rv.split(','): + metric_name, metric_value = metric.strip().split('=') + print_prometheus(metric_name, {None: float(metric_value)}) + # Go go go! if __name__ == "__main__":