Loading mnpw/__init__.py +44 −2 Original line number Diff line number Diff line Loading @@ -45,6 +45,24 @@ def main(): help="No output, except for errors.", action="store_true", ) parser.add_argument( "-w", "--warn", help="Notify when the check status is WARNING.", action="store_true", ) parser.add_argument( "-u", "--unknown", help="Notify when the check status is UNKNOWN.", action="store_true", ) parser.add_argument( "-e", "--errors", help="Notify on check errors.", action="store_true", ) parser.add_argument( "-t", "--timeout", Loading @@ -53,18 +71,42 @@ def main(): default=nagios.DEFAULT_TIMEOUT, ) args = parser.parse_args() log_level = logging.WARNING # Default level. if args.verbose and args.quiet: parser.error("Can't specify verbose and quiet output together.") if args.verbose: logging.basicConfig(level=logging.INFO) log_level = logging.INFO if args.quiet: logging.basicConfig(level=logging.CRITICAL) log_level = logging.CRITICAL logging.basicConfig(format="%(message)s", level=log_level) check = nagios.Check(args.command, args.arguments) try: check.run(args.timeout) except Exception as ex: # pylint: disable=broad-except parser.error(str(ex)) if args.errors: notify(f"") if check.ExitCode == nagios.NagiosCode.OK: logging.info("Check status is OK.") elif check.ExitCode == nagios.NagiosCode.WARNING: logging.info("Check status is WARNING.") if args.warning: notify(f"") elif check.ExitCode == nagios.NagiosCode.CRITICAL: logging.info("Check status is CRITICAL.") notify(f"") elif check.ExitCode == nagios.NagiosCode.UNKNOWN: logging.info("Check status is UNKNOWN.") if args.unknown: notify(f"") else: logging.info( f"Check status is invalid for a Nagios plugin ({check.ExitCode})." ) if args.errors: notify(f"") if __name__ == "__main__": Loading mnpw/nagios.py +14 −2 Original line number Diff line number Diff line Loading @@ -4,7 +4,8 @@ Based on the specification from https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/3/en/pluginapi.html""" import enum import subprocess # nosemgrep: rules.bandit.B404 import logging import subprocess DEFAULT_TIMEOUT = 10 # In seconds. Loading @@ -25,10 +26,11 @@ class Check: Arguments = [] ExitCode = None Output = None PerfData = None PerfData = [] LongOutput = None AdditionalOutput = None stderr = None _stdout = None def __init__(self, command, args): if not command: Loading @@ -48,6 +50,10 @@ class Check: """Run the check (if it's hasn't run yet).""" if self.ExitCode is not None: raise Exception("Check has already run.") _cmd = [ self.Command, ] + self.Arguments logging.info(f"Running command {_cmd}.") try: proc = subprocess.run( [ Loading @@ -64,4 +70,10 @@ class Check: except subprocess.TimeoutExpired: raise RuntimeError("Timeout exceeded.") self.ExitCode = proc.returncode logging.info(f"Exit code is {self.ExitCode}.") self._stdout = proc.stdout logging.info("stdout is {self.stdout}.") self.stderr = proc.stderr logging.info("stderr is {self.stderr}.") self._stdout = proc.stdout self._parse_output() Loading
mnpw/__init__.py +44 −2 Original line number Diff line number Diff line Loading @@ -45,6 +45,24 @@ def main(): help="No output, except for errors.", action="store_true", ) parser.add_argument( "-w", "--warn", help="Notify when the check status is WARNING.", action="store_true", ) parser.add_argument( "-u", "--unknown", help="Notify when the check status is UNKNOWN.", action="store_true", ) parser.add_argument( "-e", "--errors", help="Notify on check errors.", action="store_true", ) parser.add_argument( "-t", "--timeout", Loading @@ -53,18 +71,42 @@ def main(): default=nagios.DEFAULT_TIMEOUT, ) args = parser.parse_args() log_level = logging.WARNING # Default level. if args.verbose and args.quiet: parser.error("Can't specify verbose and quiet output together.") if args.verbose: logging.basicConfig(level=logging.INFO) log_level = logging.INFO if args.quiet: logging.basicConfig(level=logging.CRITICAL) log_level = logging.CRITICAL logging.basicConfig(format="%(message)s", level=log_level) check = nagios.Check(args.command, args.arguments) try: check.run(args.timeout) except Exception as ex: # pylint: disable=broad-except parser.error(str(ex)) if args.errors: notify(f"") if check.ExitCode == nagios.NagiosCode.OK: logging.info("Check status is OK.") elif check.ExitCode == nagios.NagiosCode.WARNING: logging.info("Check status is WARNING.") if args.warning: notify(f"") elif check.ExitCode == nagios.NagiosCode.CRITICAL: logging.info("Check status is CRITICAL.") notify(f"") elif check.ExitCode == nagios.NagiosCode.UNKNOWN: logging.info("Check status is UNKNOWN.") if args.unknown: notify(f"") else: logging.info( f"Check status is invalid for a Nagios plugin ({check.ExitCode})." ) if args.errors: notify(f"") if __name__ == "__main__": Loading
mnpw/nagios.py +14 −2 Original line number Diff line number Diff line Loading @@ -4,7 +4,8 @@ Based on the specification from https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/3/en/pluginapi.html""" import enum import subprocess # nosemgrep: rules.bandit.B404 import logging import subprocess DEFAULT_TIMEOUT = 10 # In seconds. Loading @@ -25,10 +26,11 @@ class Check: Arguments = [] ExitCode = None Output = None PerfData = None PerfData = [] LongOutput = None AdditionalOutput = None stderr = None _stdout = None def __init__(self, command, args): if not command: Loading @@ -48,6 +50,10 @@ class Check: """Run the check (if it's hasn't run yet).""" if self.ExitCode is not None: raise Exception("Check has already run.") _cmd = [ self.Command, ] + self.Arguments logging.info(f"Running command {_cmd}.") try: proc = subprocess.run( [ Loading @@ -64,4 +70,10 @@ class Check: except subprocess.TimeoutExpired: raise RuntimeError("Timeout exceeded.") self.ExitCode = proc.returncode logging.info(f"Exit code is {self.ExitCode}.") self._stdout = proc.stdout logging.info("stdout is {self.stdout}.") self.stderr = proc.stderr logging.info("stderr is {self.stderr}.") self._stdout = proc.stdout self._parse_output()