From 592a1f4bb767501b94e5d7a7a11894a69df19713 Mon Sep 17 00:00:00 2001 From: Markus Opolka <7090372+martialblog@users.noreply.github.com> Date: Tue, 24 Mar 2026 13:17:55 +0100 Subject: [PATCH 1/2] More robust output handling --- check_monit.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/check_monit.py b/check_monit.py index e31c32a..ee8f8d5 100755 --- a/check_monit.py +++ b/check_monit.py @@ -70,10 +70,9 @@ def print_output(status, count_ok, count_all, items): if len(items): for item in items: - s = "OK" if item['status'] == 0 else "CRITICAL" - print(' \\_ [{0}]: {1}'.format(s, item['name'])) - print(' ' + item['output']) - + s = "OK" if item.get('status') == 0 else "CRITICAL" + print(' \\_ [{0}]: {1}'.format(s, item.get('name', 'No Name'))) + print(' ' + item.get('output', 'None')) def get_service_output(service_type, element): # Service Type Filesystem From ee4a13f8faf80f4795dd1e16ba123a54e60be9a2 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Fri, 27 Mar 2026 15:02:46 +0100 Subject: [PATCH 2/2] [squash] Add add unittests --- check_monit.py | 2 +- test_check_monit.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/check_monit.py b/check_monit.py index ee8f8d5..6e4cb1f 100755 --- a/check_monit.py +++ b/check_monit.py @@ -72,7 +72,7 @@ def print_output(status, count_ok, count_all, items): for item in items: s = "OK" if item.get('status') == 0 else "CRITICAL" print(' \\_ [{0}]: {1}'.format(s, item.get('name', 'No Name'))) - print(' ' + item.get('output', 'None')) + print(' {}' .format(item.get('output', 'None'))) def get_service_output(service_type, element): # Service Type Filesystem diff --git a/test_check_monit.py b/test_check_monit.py index d616615..45a5e33 100644 --- a/test_check_monit.py +++ b/test_check_monit.py @@ -64,6 +64,24 @@ def test_return_plugin(self, mock_print): mock_print.assert_has_calls(calls) + @mock.patch('builtins.print') + def test_return_plugin_with_no_output(self, mock_print): + actual = print_output(1, 2, 3, [{'name': 'foo', 'output': None, 'status': 1}]) + + calls = [mock.call('[WARNING]: Monit Service Status 2/3'), + mock.call(' \\_ [CRITICAL]: foo'), + mock.call(' None')] + + mock_print.assert_has_calls(calls) + + actual = print_output(1, 2, 3, [{'name': 'foo', 'status': 1}]) + + calls = [mock.call('[WARNING]: Monit Service Status 2/3'), + mock.call(' \\_ [CRITICAL]: foo'), + mock.call(' None')] + + mock_print.assert_has_calls(calls) + class MainTesting(unittest.TestCase): @mock.patch('builtins.print')