1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

monit: add support for all monit services when checking process state (#1532)

* add support for all monit service types

* ignore case when performing check

* add changelog

* Escape special characters before matching

Co-authored-by: Felix Fontein <felix@fontein.de>

* escape each element individually

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Graham Herceg 2021-01-03 05:37:56 -05:00 committed by GitHub
parent fd741ed663
commit bed1dc479f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 12 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- monit - add support for all monit service checks (https://github.com/ansible-collections/community.general/pull/1532).

View file

@ -62,6 +62,9 @@ STATE_COMMAND_MAP = {
'restarted': 'restart'
}
MONIT_SERVICES = ['Process', 'File', 'Fifo', 'Filesystem', 'Directory', 'Remote host', 'System', 'Program',
'Network']
@python_2_unicode_compatible
class StatusValue(namedtuple("Status", "value, is_pending")):
@ -151,7 +154,9 @@ class Monit(object):
return self._parse_status(out, err)
def _parse_status(self, output, err):
if "Process '%s'" % self.process_name not in output:
escaped_monit_services = '|'.join([re.escape(x) for x in MONIT_SERVICES])
pattern = "(%s) '%s'" % (escaped_monit_services, re.escape(self.process_name))
if not re.search(pattern, output, re.IGNORECASE):
return Status.MISSING
status_val = re.findall(r"^\s*status\s*([\w\- ]+)", output, re.MULTILINE)

View file

@ -12,7 +12,7 @@ from ansible_collections.community.general.tests.unit.plugins.modules.utils impo
TEST_OUTPUT = """
Process '%s'
%s '%s'
status %s
monitoring status Not monitored
monitoring mode active
@ -106,28 +106,45 @@ def test_status_value(status_name):
BASIC_OUTPUT_CASES = [
(TEST_OUTPUT % ('processX', name), getattr(monit.Status, name.upper()))
(TEST_OUTPUT % ('Process', 'processX', name), getattr(monit.Status, name.upper()))
for name in monit.StatusValue.ALL_STATUS
]
@pytest.mark.parametrize('output, expected', BASIC_OUTPUT_CASES + [
('', monit.Status.MISSING),
(TEST_OUTPUT % ('processY', 'OK'), monit.Status.MISSING),
(TEST_OUTPUT % ('processX', 'Not Monitored - start pending'), monit.Status.OK),
(TEST_OUTPUT % ('processX', 'Monitored - stop pending'), monit.Status.NOT_MONITORED),
(TEST_OUTPUT % ('processX', 'Monitored - restart pending'), monit.Status.OK),
(TEST_OUTPUT % ('processX', 'Not Monitored - monitor pending'), monit.Status.OK),
(TEST_OUTPUT % ('processX', 'Does not exist'), monit.Status.DOES_NOT_EXIST),
(TEST_OUTPUT % ('processX', 'Not monitored'), monit.Status.NOT_MONITORED),
(TEST_OUTPUT % ('processX', 'Running'), monit.Status.OK),
(TEST_OUTPUT % ('processX', 'Execution failed | Does not exist'), monit.Status.EXECUTION_FAILED),
(TEST_OUTPUT % ('Process', 'processY', 'OK'), monit.Status.MISSING),
(TEST_OUTPUT % ('Process', 'processX', 'Not Monitored - start pending'), monit.Status.OK),
(TEST_OUTPUT % ('Process', 'processX', 'Monitored - stop pending'), monit.Status.NOT_MONITORED),
(TEST_OUTPUT % ('Process', 'processX', 'Monitored - restart pending'), monit.Status.OK),
(TEST_OUTPUT % ('Process', 'processX', 'Not Monitored - monitor pending'), monit.Status.OK),
(TEST_OUTPUT % ('Process', 'processX', 'Does not exist'), monit.Status.DOES_NOT_EXIST),
(TEST_OUTPUT % ('Process', 'processX', 'Not monitored'), monit.Status.NOT_MONITORED),
(TEST_OUTPUT % ('Process', 'processX', 'Running'), monit.Status.OK),
(TEST_OUTPUT % ('Process', 'processX', 'Execution failed | Does not exist'), monit.Status.EXECUTION_FAILED),
])
def test_parse_status(output, expected):
status = monit.Monit(None, '', 'processX', 0)._parse_status(output, '')
assert status == expected
@pytest.mark.parametrize('output, expected', BASIC_OUTPUT_CASES + [
(TEST_OUTPUT % ('Process', 'processX', 'OK'), monit.Status.OK),
(TEST_OUTPUT % ('File', 'processX', 'OK'), monit.Status.OK),
(TEST_OUTPUT % ('Fifo', 'processX', 'OK'), monit.Status.OK),
(TEST_OUTPUT % ('Filesystem', 'processX', 'OK'), monit.Status.OK),
(TEST_OUTPUT % ('Directory', 'processX', 'OK'), monit.Status.OK),
(TEST_OUTPUT % ('Remote host', 'processX', 'OK'), monit.Status.OK),
(TEST_OUTPUT % ('System', 'processX', 'OK'), monit.Status.OK),
(TEST_OUTPUT % ('Program', 'processX', 'OK'), monit.Status.OK),
(TEST_OUTPUT % ('Network', 'processX', 'OK'), monit.Status.OK),
(TEST_OUTPUT % ('Unsupported', 'processX', 'OK'), monit.Status.MISSING),
])
def test_parse_status_supports_all_services(output, expected):
status = monit.Monit(None, '', 'processX', 0)._parse_status(output, '')
assert status == expected
@pytest.mark.parametrize('output, expected', [
('This is monit version 5.18.1', '5.18.1'),
('This is monit version 12.18', '12.18'),