Skip to content
Permalink
Browse files

7893 FIX systemd_units: filter out disabled services from summary mon…

…itoring

This fix solves the permanent crit status of the systemd summary checkup. Disabled (explicit & implicit) services are now excluded from the summary but still mentioned in the overall message

FEED-3850

Change-Id: Ia49ce955fc1cdac8fa6910b4072b5ed85a392026
  • Loading branch information...
anthonyh209 committed Jul 4, 2019
1 parent 10710af commit d39fd293938bdf7ebd17cb2345330c8679b4bc7a
@@ -0,0 +1,10 @@
Title: systemd_units: filter out disabled services from summary monitoring
Level: 2
Component: checks
Compatible: compat
Edition: cre
Version: 1.7.0i1
Date: 1562245533
Class: fix

This fix solves the permanent crit status of the systemd summary checkup. Disabled (explicit & implicit) services are now excluded from the summary but still mentioned in the overall message
@@ -213,7 +213,10 @@ section_df() {
sections_systemd() {
if inpath systemctl; then
echo '<<<systemd_units>>>'
systemctl --all --no-pager
echo "[list-unit-files]"
systemctl list-unit-files --no-pager
echo "[all]"
systemctl --all --no-pager | sed '/^$/q'
fi
}

@@ -68,26 +68,47 @@ def parse_systemd_units(info):
if not info:
return {}

# second to last line (see above): "X loaded units listed."
count = int(info[-2][0])
if count == 0:
return parsed

UnitEntry = collections.namedtuple("UnitEntry",
['name', 'type', 'load', 'active', 'sub', 'description'])
for row in info[1:count + 1]:
if row[0] == '*': # remove the '● ' (seems to get converted to '*')
row.pop(0)
line = ' '.join(row)
for unit_marker in _SYSTEMD_UNITS:
if unit_marker in line:
utype = unit_marker.strip('. ')
name, remains = line.split(unit_marker, 1)
load, active, sub, descr = remains.split(' ', 3)
unit = UnitEntry(name, utype, load, active, sub, descr)
parsed.setdefault(unit.type, {})[unit.name] = unit
break
iter_info = iter(info)
target_states = {}

line = next(iter_info)

if line[0] == "[list-unit-files]":
for line in iter_info:
if line[0].startswith('['):
break
if len(line) >= 2:
target_states[line[0]] = line[1]

if line[0] == '[all]':
try:
line = next(iter_info)

# no services listed
except StopIteration:
return parsed

UnitEntry = collections.namedtuple(
"UnitEntry", ['name', 'type', 'load', 'active', 'sub', 'description', 'state'])

for row in iter_info:
if row[0] == '*':
row.pop(0)
line = ' '.join(row)
for unit_marker in _SYSTEMD_UNITS:
if unit_marker in line:
utype = unit_marker.strip('. ')
name, remains = line.split(unit_marker, 1)
if "@" in name:
pos = name.find("@")
temp = name[:pos + 1]
else:
temp = name
state = target_states.get("%s.%s" % (temp, utype), "unknown")
load, active, sub, descr = remains.split(' ', 3)
unit = UnitEntry(name, utype, load, active, sub, descr, state)
parsed.setdefault(unit.type, {})[unit.name] = unit
break
return parsed


@@ -177,23 +198,24 @@ def discovery_systemd_units_services_summary(parsed):
def services_split(services, blacklist):
excluded = []
included = []
disabled = []
compiled_patterns = [regex(p) for p in blacklist]
for service in services:
if any(expr.match(service.name) for expr in compiled_patterns):
if service.state in ["disabled", "indirect"]:
disabled.append(service)
elif any(expr.match(service.name) for expr in compiled_patterns):
excluded.append(service)
else:
included.append(service)
return included, excluded
return included, excluded, disabled


def check_systemd_units_services_summary(_no_item, params, parsed):
services = parsed.get('service', {}).values()
blacklist = params.get('ignored', [])
yield 0, "%d services in total" % len(services)

services, excluded = services_split(services, blacklist)
if excluded:
yield 0, "%d ignored services" % len(excluded)
services, excluded, disabled = services_split(services, blacklist)

all_states = sorted(set(s.active for s in services))

@@ -207,6 +229,10 @@ def check_systemd_units_services_summary(_no_item, params, parsed):
service_names = ", ".join(service_names)
yield state, "%d service%s %s (%s)" % (cnt, '' if cnt == 1 else 's', active_state,
service_names)
if excluded:
yield 0, "%d ignored services" % len(excluded)
if disabled:
yield 0, "%d disabled services" % len(disabled)


check_info['systemd_units.services_summary'] = {
@@ -2,6 +2,8 @@
checkname = 'systemd_units'

info = [
['[list-unit-files]'],
['[all]'],
['UNIT', 'LOAD', 'ACTIVE', 'SUB', 'DESCRIPTION'],
[
'proc-sys-fs-binfmt_misc.automount', 'loaded', 'active', 'running', 'Arbitrary',
@@ -135,19 +137,6 @@
'ureadahead-stop.timer', 'loaded', 'active', 'elapsed', 'Stop', 'ureadahead', 'data',
'collection', '45s', 'after', 'completed', 'startup'
],
[],
['LOAD', '=', 'Reflects', 'whether', 'the', 'unit', 'definition', 'was', 'properly', 'loaded.'],
[
'ACTIVE', '=', 'The', 'high-level', 'unit', 'activation', 'state,', 'i.e.',
'generalization', 'of', 'SUB.'
],
[
'SUB', '=', 'The', 'low-level', 'unit', 'activation', 'state,', 'values', 'depend', 'on',
'unit', 'type.'
],
[],
['54', 'loaded', 'units', 'listed.'],
['To', 'show', 'all', 'installed', 'unit', 'files', 'use', "'systemctl", "list-unit-files'."],
]

discovery = {'services': [], 'services_summary': [('Summary', {})]}
@@ -2,14 +2,14 @@
checkname = 'systemd_units'

info = [
['[list-unit-files]'],
['[all]'],
['UNIT', 'LOAD', 'ACTIVE', 'SUB', 'DESCRIPTION'],
[
'foo.service', 'loaded', 'failed', 'failed', 'Arbitrary', 'Executable', 'File', 'Formats',
'File', 'System', 'Automount', 'Point'
],
['bar.service', 'loaded', 'failed', 'failed', 'a', 'bar', 'service'],
['2'],
[],
]

discovery = {'services': [], 'services_summary': [('Summary', {})]}
@@ -1,11 +1,11 @@
import pytest # type:ignore
import pytest
import collections
from cmk_base.check_api import MKGeneralException

pytestmark = pytest.mark.checks

UnitEntry = collections.namedtuple("UnitEntry",
['name', 'type', 'load', 'active', 'sub', 'description'])
UnitEntry = collections.namedtuple(
"UnitEntry", ['name', 'type', 'load', 'active', 'sub', 'description', 'state'])


@pytest.mark.parametrize('services, blacklist, expected', [
@@ -15,78 +15,92 @@
load=u'loaded',
active=u'inactive',
sub=u'dead',
description=u'Detect the available GPUs and deal with any system changes'),
description=u'Detect the available GPUs and deal with any system changes',
state=u'unknown'),
UnitEntry(name=u'rsyslog',
type='service',
load=u'loaded',
active=u'active',
sub=u'running',
description=u'System Logging Service'),
description=u'System Logging Service',
state=u'enabled'),
UnitEntry(name=u'alsa-state',
type='service',
load=u'loaded',
active=u'inactive',
sub=u'dead',
description=u'Manage Sound Card State (restore and store)')
description=u'Manage Sound Card State (restore and store)',
state=u'disabled'),
], [], ([
UnitEntry(name=u'gpu-manager',
type='service',
load=u'loaded',
active=u'inactive',
sub=u'dead',
description=u'Detect the available GPUs and deal with any system changes'),
description=u'Detect the available GPUs and deal with any system changes',
state=u'unknown'),
UnitEntry(name=u'rsyslog',
type='service',
load=u'loaded',
active=u'active',
sub=u'running',
description=u'System Logging Service'),
description=u'System Logging Service',
state=u'enabled'),
], [], [
UnitEntry(name=u'alsa-state',
type='service',
load=u'loaded',
active=u'inactive',
sub=u'dead',
description=u'Manage Sound Card State (restore and store)')
], [])),
description=u'Manage Sound Card State (restore and store)',
state=u'disabled'),
])),
([
UnitEntry(name=u'gpu-manager',
type='service',
load=u'loaded',
active=u'inactive',
sub=u'dead',
description=u'Detect the available GPUs and deal with any system changes'),
description=u'Detect the available GPUs and deal with any system changes',
state=u'unknown'),
UnitEntry(name=u'rsyslog',
type='service',
load=u'loaded',
active=u'active',
sub=u'running',
description=u'System Logging Service'),
description=u'System Logging Service',
state=u'enabled'),
UnitEntry(name=u'alsa-state',
type='service',
load=u'loaded',
active=u'inactive',
sub=u'dead',
description=u'Manage Sound Card State (restore and store)')
description=u'Manage Sound Card State (restore and store)',
state=u'indirect')
], [u'gpu'], ([
UnitEntry(name=u'rsyslog',
type='service',
load=u'loaded',
active=u'active',
sub=u'running',
description=u'System Logging Service'),
UnitEntry(name=u'alsa-state',
description=u'System Logging Service',
state=u'enabled'),
], [
UnitEntry(name=u'gpu-manager',
type='service',
load=u'loaded',
active=u'inactive',
sub=u'dead',
description=u'Manage Sound Card State (restore and store)')
description=u'Detect the available GPUs and deal with any system changes',
state=u'unknown'),
], [
UnitEntry(name=u'gpu-manager',
UnitEntry(name=u'alsa-state',
type='service',
load=u'loaded',
active=u'inactive',
sub=u'dead',
description=u'Detect the available GPUs and deal with any system changes')
description=u'Manage Sound Card State (restore and store)',
state=u'indirect')
])),
])
def test_services_split(check_manager, services, blacklist, expected):

0 comments on commit d39fd29

Please sign in to comment.
You can’t perform that action at this time.