diff --git a/check_monit.py b/check_monit.py index 96dc452..e31c32a 100755 --- a/check_monit.py +++ b/check_monit.py @@ -109,7 +109,14 @@ 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'): + # 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' + + return return_value return 'Service (type={0}) not implemented'.format(service_type) @@ -121,8 +128,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 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')