Skip to content

Commit cc0a432

Browse files
author
root
committed
service_type 7 && monitor all states !0
Service type 7 (program) collects the output of the service in a list of xml elements. Check if this list is empty, concatenate all list values or set 'no command output available' as message for non existing output. 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. unittests for service_type 7 program/output for empty element and for the unlikely case of more than one list elements.
1 parent 951e7c8 commit cc0a432

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

check_monit.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,14 @@ def get_service_output(service_type, element):
109109

110110
# Service Type Program
111111
if service_type == 7:
112-
return element.findall('program/output')[0].text
112+
return_value = None
113+
for output_item in element.findall('program/output'):
114+
# type( output_item ) is <class 'xml.etree.ElementTree.Element'>
115+
return_value = output_item.text if return_value is None else f"{return_value}; {output_item.text}"
116+
if return_value is None:
117+
return_value = 'no command output available'
118+
119+
return return_value
113120

114121
return 'Service (type={0}) not implemented'.format(service_type)
115122

@@ -121,8 +128,8 @@ def get_service_states(services):
121128
for service in services:
122129
# Get the monitor state for the service (0: Not, 1: Yes, 2: Init, 4: Waiting)
123130
monitor = int(service.find('monitor').text)
124-
# if the monitor is yes or initialize, check its status
125-
if monitor in (1, 2):
131+
# ignore 'Monitor_not' (0)
132+
if monitor != 0:
126133
status = int(service.find('status').text)
127134
if status == 0:
128135
count_ok += 1

test_check_monit.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,18 @@ def test_service_output(self):
4040
actual = get_service_output(3, input_element)
4141
self.assertEqual(actual, 'unittest')
4242

43+
input_element = ET.ElementTree(ET.fromstring("""<doc><program><output></output></program></doc>"""))
44+
actual = get_service_output(7, input_element)
45+
self.assertEqual(actual, 'no command output available')
46+
4347
input_element = ET.ElementTree(ET.fromstring("""<doc><program><output>foobar</output></program></doc>"""))
4448
actual = get_service_output(7, input_element)
4549
self.assertEqual(actual, 'foobar')
4650

51+
input_element = ET.ElementTree(ET.fromstring("""<doc><program><output>foot</output><output>bath</output></program></doc>"""))
52+
actual = get_service_output(7, input_element)
53+
self.assertEqual(actual, 'foot; bath')
54+
4755
class UtilTesting(unittest.TestCase):
4856

4957
@mock.patch('builtins.print')

0 commit comments

Comments
 (0)