From 91da4bd59b30f3d40ef70db91c241daa5a92d4eb Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Tue, 9 Aug 2016 23:11:10 -0700 Subject: [PATCH] puppet: Add check_cron_file generic helper. --- .../zulip_app_frontend/check_cron_file | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 puppet/zulip/files/nagios_plugins/zulip_app_frontend/check_cron_file diff --git a/puppet/zulip/files/nagios_plugins/zulip_app_frontend/check_cron_file b/puppet/zulip/files/nagios_plugins/zulip_app_frontend/check_cron_file new file mode 100644 index 0000000000..5fb5003a7c --- /dev/null +++ b/puppet/zulip/files/nagios_plugins/zulip_app_frontend/check_cron_file @@ -0,0 +1,52 @@ +#!/usr/bin/env python +""" Nagios plugin paired with a cron job. This just verifies that the +file output by the cron job is correct. +""" +from __future__ import print_function + +import sys +import time + +# Avoid requiring the typing module to be installed +if False: from typing import Tuple + +def nagios_from_file(results_file): + # type: (str) -> Tuple[int, str] + """Returns a nagios-appropriate string and return code obtained by + parsing the desired file on disk. The file on disk should be of format + + %s|%s % (timestamp, nagios_string) + + This file is created by various nagios checking cron jobs such as + check-rabbitmq-queues and check-rabbitmq-consumers""" + + data = open(results_file).read().strip() + pieces = data.split('|') + + if not len(pieces) == 4: + state = 'UNKNOWN' + ret = 3 + data = "Results file malformed" + else: + timestamp = int(pieces[0]) + + time_diff = time.time() - timestamp + if time_diff > 60 * 2: + ret = 3 + state = 'UNKNOWN' + data = "Results file is stale" + else: + ret = int(pieces[1]) + state = pieces[2] + data = pieces[3] + + return (ret, "%s: %s" % (state, data)) + +if __name__ == "__main__": + RESULTS_FILE = sys.argv[1] + ret, result = nagios_from_file(RESULTS_FILE) + + print(result) + exit(ret) + +