diff --git a/mnpw/__init__.py b/mnpw/__init__.py index cadb6fec1cfb39ba083634d184bc48b82317de2c..2b53d2c47351447d6c2612d25df7f552effcedb8 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})."