Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
No related merge requests found
Pipeline #1971 passed
...@@ -94,36 +94,22 @@ def main(): # noqa: MC0001 ...@@ -94,36 +94,22 @@ def main(): # noqa: MC0001
if check.ExitCode == nagios.NagiosCode.OK: if check.ExitCode == nagios.NagiosCode.OK:
logging.info("Check status is 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: elif check.ExitCode == nagios.NagiosCode.WARNING:
logging.info("Check status is 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: if args.warn and not args.dry_run:
notify(f"{check.Output} on {HOSTNAME}.") notify(f"{check.Output} on {HOSTNAME}.")
elif check.ExitCode == nagios.NagiosCode.CRITICAL: elif check.ExitCode == nagios.NagiosCode.CRITICAL:
logging.info("Check status is CRITICAL.") logging.info("Check status is CRITICAL.")
logging.info(check.Output)
logging.info(check.AdditionalOutput)
logging.info(check.stderr)
if not args.dry_run: if not args.dry_run:
notify(f"{check.Output} on {HOSTNAME}.") notify(f"{check.Output} on {HOSTNAME}.")
elif check.ExitCode == nagios.NagiosCode.UNKNOWN: elif check.ExitCode == nagios.NagiosCode.UNKNOWN:
logging.info("Check status is 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: if args.unknown and not args.dry_run:
notify(f"{check.Output} on {HOSTNAME}.") notify(f"{check.Output} on {HOSTNAME}.")
else: else:
logging.info( logging.info(
f"Check status is invalid for a Nagios plugin ({check.ExitCode})." f"Check status is invalid for a Nagios plugin ({check.ExitCode})."
) )
logging.info(check._stdout)
logging.info(check.stderr)
if args.errors: if args.errors:
notify(f"Check {args.command} on {HOSTNAME} status is invalid.") notify(f"Check {args.command} on {HOSTNAME} status is invalid.")
......
...@@ -60,23 +60,23 @@ def parse_perf_data(line): # noqa: MC0001 ...@@ -60,23 +60,23 @@ def parse_perf_data(line): # noqa: MC0001
# Get the remaining fields, if available. # Get the remaining fields, if available.
try: try:
pd["warning"] = float(fields[1]) pd["warning"] = float(fields[1])
except Exception as ex: # pylint: disable=broad-except except (ValueError, IndexError) as ex:
logging.info(str(ex)) logging.debug(str(ex))
try: try:
pd["critical"] = float(fields[2]) pd["critical"] = float(fields[2])
except Exception as ex: # pylint: disable=broad-except except (ValueError, IndexError) as ex:
logging.info(str(ex)) logging.debug(str(ex))
try: try:
pd["min"] = float(fields[3]) pd["min"] = float(fields[3])
except Exception as ex: # pylint: disable=broad-except except (ValueError, IndexError) as ex:
logging.info(str(ex)) logging.debug(str(ex))
try: try:
pd["max"] = float(fields[4]) pd["max"] = float(fields[4])
except Exception as ex: # pylint: disable=broad-except except (ValueError, IndexError) as ex:
logging.info(str(ex)) logging.debug(str(ex))
return PerfData(**pd) return PerfData(**pd)
...@@ -107,12 +107,13 @@ class Check: ...@@ -107,12 +107,13 @@ class Check:
Can only be run after the plugin has run and finished successfully. Can only be run after the plugin has run and finished successfully.
""" """
if self.ExitCode is None: if self._stdout is None or self._stdout == "" or self._stdout == "\n":
raise RuntimeError("Check hasn't run yet.") logging.info("Empty stdout, skipping parsing.")
return
lines = self._stdout.splitlines() lines = self._stdout.splitlines()
if not lines: if not lines:
logging.info("Emptry stdout.") logging.info("Empty stdout, skipping parsing.")
return return
if "|" in lines[0]: if "|" in lines[0]:
...@@ -164,8 +165,14 @@ class Check: ...@@ -164,8 +165,14 @@ class Check:
self.ExitCode = proc.returncode self.ExitCode = proc.returncode
logging.info(f"Exit code is {self.ExitCode}.") logging.info(f"Exit code is {self.ExitCode}.")
self._stdout = proc.stdout self._stdout = proc.stdout
logging.info("stdout is {self.stdout}.") logging.info("stdout is {self._stdout}.")
self.stderr = proc.stderr self.stderr = proc.stderr
logging.info("stderr is {self.stderr}.") logging.info("stderr is {self.stderr}.")
self._stdout = proc.stdout if self.ExitCode in list(NagiosCode):
self._parse_output() 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."
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment