From 5050fb19f6418ea3697e89a64bdcd5cb756790b6 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 29 Aug 2019 14:15:35 -0700 Subject: [PATCH] =?UTF-8?q?nagios:=20Don=E2=80=99t=20crash=20on=20missing?= =?UTF-8?q?=20cron=20file.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders Kaseorg --- scripts/nagios/cron_file_helper.py | 34 ++++++++++++++++++------------ 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/scripts/nagios/cron_file_helper.py b/scripts/nagios/cron_file_helper.py index ca92ece168..2e14ec5184 100644 --- a/scripts/nagios/cron_file_helper.py +++ b/scripts/nagios/cron_file_helper.py @@ -11,25 +11,31 @@ def nagios_from_file(results_file: str) -> Tuple[int, str]: This file is created by various nagios checking cron jobs such as check-rabbitmq-queues and check-rabbitmq-consumers""" - with open(results_file) as f: - data = f.read().strip() - pieces = data.split('|') - - if not len(pieces) == 4: + try: + with open(results_file) as f: + data = f.read().strip() + except FileNotFoundError: state = 'UNKNOWN' ret = 3 - data = "Results file malformed" + data = "Results file is missing" else: - timestamp = int(pieces[0]) + pieces = data.split('|') - time_diff = time.time() - timestamp - if time_diff > 60 * 2: - ret = 3 + if not len(pieces) == 4: state = 'UNKNOWN' - data = "Results file is stale" + ret = 3 + data = "Results file malformed" else: - ret = int(pieces[1]) - state = pieces[2] - data = pieces[3] + 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, f"{state}: {data}")