From db2bae0b7a7f8d1a3a069b536647a76bd90abec1 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 7 Oct 2025 09:23:46 +0200 Subject: [PATCH 1/2] service_type 7 output & ignore monitor_state 0 The output for service_type 7 didn't work in all cicumstances: `element.findall('program/output')` sometimes is an empty list and its first non-existent entry is of the type None. If the list is not empty get the last xml.etree.ElementTree.Element as text from it as the return value. If it is empty return the message 'no command output available'. Instead of only taking in account monitor_states 1 (Yes) and 2 (Init) take all monitor_states in account that are not 0 (Not). There is no reason not to include information about any monitor that is not disabled. --- check_monit.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/check_monit.py b/check_monit.py index 96dc452..a4d7056 100755 --- a/check_monit.py +++ b/check_monit.py @@ -109,7 +109,13 @@ def get_service_output(service_type, element): # Service Type Program if service_type == 7: - return element.findall('program/output')[0].text + return_value = None + for output_item in element.findall('program/output'): + return_value = output_item.text # + if return_value is None: + return_value = 'no command output available' + + return return_value return 'Service (type={0}) not implemented'.format(service_type) @@ -121,8 +127,8 @@ def get_service_states(services): for service in services: # Get the monitor state for the service (0: Not, 1: Yes, 2: Init, 4: Waiting) monitor = int(service.find('monitor').text) - # if the monitor is yes or initialize, check its status - if monitor in (1, 2): + # ignore 'Monitor_not' (0) + if not monitor == 0: status = int(service.find('status').text) if status == 0: count_ok += 1 From f0088a5ae394a6beefbdad94b6f2b4fe9a08b790 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 10 Oct 2025 10:30:05 +0200 Subject: [PATCH 2/2] join list elements & unittest & make pretty * instead of accepting only the last value of program/output concatenate values in the unlikely case there's more than one * added unittests for service_type 7 for program output: empty, one value, two values * replace `if not ==` by `if !=` (proposed by pylint) --- check_monit.py | 5 +++-- test_check_monit.py | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) mode change 100644 => 100755 test_check_monit.py diff --git a/check_monit.py b/check_monit.py index a4d7056..e31c32a 100755 --- a/check_monit.py +++ b/check_monit.py @@ -111,7 +111,8 @@ def get_service_output(service_type, element): if service_type == 7: return_value = None for output_item in element.findall('program/output'): - return_value = output_item.text # + # type( output_item ) is + return_value = output_item.text if return_value is None else f"{return_value}; {output_item.text}" if return_value is None: return_value = 'no command output available' @@ -128,7 +129,7 @@ def get_service_states(services): # Get the monitor state for the service (0: Not, 1: Yes, 2: Init, 4: Waiting) monitor = int(service.find('monitor').text) # ignore 'Monitor_not' (0) - if not monitor == 0: + if monitor != 0: status = int(service.find('status').text) if status == 0: count_ok += 1 diff --git a/test_check_monit.py b/test_check_monit.py old mode 100644 new mode 100755 index 8034a65..d616615 --- a/test_check_monit.py +++ b/test_check_monit.py @@ -40,10 +40,18 @@ def test_service_output(self): actual = get_service_output(3, input_element) self.assertEqual(actual, 'unittest') + input_element = ET.ElementTree(ET.fromstring("""""")) + actual = get_service_output(7, input_element) + self.assertEqual(actual, 'no command output available') + input_element = ET.ElementTree(ET.fromstring("""foobar""")) actual = get_service_output(7, input_element) self.assertEqual(actual, 'foobar') + input_element = ET.ElementTree(ET.fromstring("""footbath""")) + actual = get_service_output(7, input_element) + self.assertEqual(actual, 'foot; bath') + class UtilTesting(unittest.TestCase): @mock.patch('builtins.print')