From 4c067d4cf702d705af92e565add71e32f5ac072e Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Sun, 27 Feb 2022 13:16:36 +0200 Subject: [PATCH] Handle notifications without command output. --- mnpw/__init__.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/mnpw/__init__.py b/mnpw/__init__.py index cadb6fe..2b53d2c 100644 --- a/mnpw/__init__.py +++ b/mnpw/__init__.py @@ -4,6 +4,7 @@ __version__ = "0.1.5" import argparse import logging +import os.path import socket import sys @@ -28,6 +29,20 @@ def notify(message): logging.error("Failed to send notification.") +def notify_wrapper(check): + """mnpw specific notifications. + + The above notify function is generic, this function is more specific to the + notifications mnpw sends. + """ + if check.Output is None or check.Output.strip() == "": + notify( + f"[{HOSTNAME}] {os.path.basename(check.Command)} returned {check.ExitCode}." # noqa: E501 + ) + else: + notify(f"[{HOSTNAME}] {check.Output}") + + def main(): # noqa: MC0001 """Main entrypoint.""" parser = argparse.ArgumentParser(description=__doc__) @@ -112,15 +127,15 @@ def main(): # noqa: MC0001 elif check.ExitCode == nagios.NagiosCode.WARNING: logging.info("Check status is WARNING.") if args.warn and not args.dry_run: - notify(f"[{HOSTNAME}] {check.Output}") + notify_wrapper(check) elif check.ExitCode == nagios.NagiosCode.CRITICAL: logging.info("Check status is CRITICAL.") if not args.dry_run: - notify(f"[{HOSTNAME}] {check.Output}") + notify_wrapper(check) elif check.ExitCode == nagios.NagiosCode.UNKNOWN: logging.info("Check status is UNKNOWN.") if args.unknown and not args.dry_run: - notify(f"[{HOSTNAME}] {check.Output}") + notify_wrapper(check) else: logging.info( f"Check status is invalid for a Nagios plugin ({check.ExitCode})." -- GitLab