Commit 167aa381 authored by nimrod's avatar nimrod
Browse files

More consistent logging.

The Nagios plugin check object logs on internal dealings. The main
function logs on handling the result.
parent ea4bd684
Loading
Loading
Loading
Loading
Loading
+0 −14
Original line number Diff line number Diff line
@@ -94,36 +94,22 @@ def main(): # noqa: MC0001

    if check.ExitCode == nagios.NagiosCode.OK:
        logging.info("Check status is OK.")
        logging.info(check.Output)
        logging.info(check.AdditionalOutput)
        logging.info(check.stderr)
    elif check.ExitCode == nagios.NagiosCode.WARNING:
        logging.info("Check status is WARNING.")
        logging.info(check.Output)
        logging.info(check.AdditionalOutput)
        logging.info(check.stderr)
        if args.warn and not args.dry_run:
            notify(f"{check.Output} on {HOSTNAME}.")
    elif check.ExitCode == nagios.NagiosCode.CRITICAL:
        logging.info("Check status is CRITICAL.")
        logging.info(check.Output)
        logging.info(check.AdditionalOutput)
        logging.info(check.stderr)
        if not args.dry_run:
            notify(f"{check.Output} on {HOSTNAME}.")
    elif check.ExitCode == nagios.NagiosCode.UNKNOWN:
        logging.info("Check status is UNKNOWN.")
        logging.info(check.Output)
        logging.info(check.AdditionalOutput)
        logging.info(check.stderr)
        if args.unknown and not args.dry_run:
            notify(f"{check.Output} on {HOSTNAME}.")
    else:
        logging.info(
            f"Check status is invalid for a Nagios plugin ({check.ExitCode})."
        )
        logging.info(check._stdout)
        logging.info(check.stderr)
        if args.errors:
            notify(f"Check {args.command} on {HOSTNAME} status is invalid.")

+21 −14
Original line number Diff line number Diff line
@@ -60,23 +60,23 @@ def parse_perf_data(line): # noqa: MC0001
    # Get the remaining fields, if available.
    try:
        pd["warning"] = float(fields[1])
    except Exception as ex:  # pylint: disable=broad-except
        logging.info(str(ex))
    except (ValueError, IndexError) as ex:
        logging.debug(str(ex))

    try:
        pd["critical"] = float(fields[2])
    except Exception as ex:  # pylint: disable=broad-except
        logging.info(str(ex))
    except (ValueError, IndexError) as ex:
        logging.debug(str(ex))

    try:
        pd["min"] = float(fields[3])
    except Exception as ex:  # pylint: disable=broad-except
        logging.info(str(ex))
    except (ValueError, IndexError) as ex:
        logging.debug(str(ex))

    try:
        pd["max"] = float(fields[4])
    except Exception as ex:  # pylint: disable=broad-except
        logging.info(str(ex))
    except (ValueError, IndexError) as ex:
        logging.debug(str(ex))

    return PerfData(**pd)

@@ -107,12 +107,13 @@ class Check:

        Can only be run after the plugin has run and finished successfully.
        """
        if self.ExitCode is None:
            raise RuntimeError("Check hasn't run yet.")
        if self._stdout is None or self._stdout == "" or self._stdout == "\n":
            logging.info("Empty stdout, skipping parsing.")
            return

        lines = self._stdout.splitlines()
        if not lines:
            logging.info("Emptry stdout.")
            logging.info("Empty stdout, skipping parsing.")
            return

        if "|" in lines[0]:
@@ -164,8 +165,14 @@ class Check:
        self.ExitCode = proc.returncode
        logging.info(f"Exit code is {self.ExitCode}.")
        self._stdout = proc.stdout
        logging.info("stdout is {self.stdout}.")
        logging.info("stdout is {self._stdout}.")
        self.stderr = proc.stderr
        logging.info("stderr is {self.stderr}.")
        self._stdout = proc.stdout
        if self.ExitCode in list(NagiosCode):
            self._parse_output()
            logging.info(f"Output is {self.Output}.")
            logging.info(f"Additional output is {self.AdditionalOutput}.")
        else:
            logging.warning(
                "Invalid plugin return code, skipping output parsing."
            )