From 167aa3811e3c50cee24fdac0a52378662e631e6c Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Sat, 31 Jul 2021 21:25:05 +0300
Subject: [PATCH] More consistent logging.

The Nagios plugin check object logs on internal dealings. The main
function logs on handling the result.
---
 mnpw/__init__.py | 14 --------------
 mnpw/nagios.py   | 35 +++++++++++++++++++++--------------
 2 files changed, 21 insertions(+), 28 deletions(-)

diff --git a/mnpw/__init__.py b/mnpw/__init__.py
index a2ca8d5..9b67b4f 100644
--- a/mnpw/__init__.py
+++ b/mnpw/__init__.py
@@ -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.")
 
diff --git a/mnpw/nagios.py b/mnpw/nagios.py
index 30f0edc..74f3400 100644
--- a/mnpw/nagios.py
+++ b/mnpw/nagios.py
@@ -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
-        self._parse_output()
+        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."
+            )
-- 
GitLab