From e655f672c55c016194c0cf368df62c9958fd2d75 Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Sat, 31 Jul 2021 19:15:21 +0300 Subject: [PATCH] Parse output. The tests aren't the greatest and binary is broken but I think the parsing works. --- mnpw/nagios.py | 36 ++++++++++++++++++++++++++++++++---- tests/test_nagios.py | 3 ++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/mnpw/nagios.py b/mnpw/nagios.py index 06b4e09..c1e33c4 100644 --- a/mnpw/nagios.py +++ b/mnpw/nagios.py @@ -93,8 +93,7 @@ class Check: ExitCode = None Output = None PerfData = [] - LongOutput = None - AdditionalOutput = None + AdditionalOutput = [] stderr = None _stdout = None @@ -107,11 +106,40 @@ class Check: def _parse_output(self): """Parses the plugin output to get the output, perf data and long - output. Can be run after the plugin has run and finished - successfully.""" + output. + + Can only be run after the plugin has run and finished successfully. + """ if self.ExitCode is None: raise RuntimeError("Check hasn't run yet.") + lines = self._stdout.splitlines() + if not lines: + logging.info("Emptry stdout.") + return + + if "|" in lines[0]: + p1, p2 = lines[0].split("|") + self.Output = p1.strip() + self.PerfData.append(PerfData(p2)) + else: + self.Output = lines[0].strip() + + if len(lines) == 1: + return + + processing_perfdata = False + for line in lines[1:]: + if processing_perfdata: + self.PerfData.append(PerfData(line)) + else: + if "|" in line: + self.AdditionalOutput.append(line.split("|")[0].strip()) + processing_perfdata = True + self.PerfData.append(PerfData(line.split("|")[1])) + else: + self.AdditionalOutput.append(line) + def run(self, timeout=DEFAULT_TIMEOUT): """Run the check (if it's hasn't run yet).""" if self.ExitCode is not None: diff --git a/tests/test_nagios.py b/tests/test_nagios.py index ccffcc1..49b47ff 100644 --- a/tests/test_nagios.py +++ b/tests/test_nagios.py @@ -107,7 +107,8 @@ def test_output_parsing(output): ) with _mock_run(return_value=proc): check.run() - # TODO: validate something. + assert isinstance(check.Output, str) + assert len(check.Output.splitlines()) == 1 @pytest.mark.parametrize("line", PERF_DATA) -- GitLab