Skip to content

Commit

Permalink
Sharpen typing on possible enabled states
Browse files Browse the repository at this point in the history
Change-Id: I8fe0749123dc5306d9d655a252b10c31cd4c2974
JIRA-Ref: SUP-21093
  • Loading branch information
TimotheusBachinger committed Nov 20, 2024
1 parent a0dd1ea commit 2ed0a07
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
43 changes: 35 additions & 8 deletions cmk/plugins/collection/agent_based/systemd_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from collections.abc import Iterable, Iterator, Mapping, Sequence
from dataclasses import dataclass
from datetime import timedelta
from enum import Enum
from typing import Any, NamedTuple, Self
from enum import Enum, StrEnum
from typing import Any, NamedTuple, Optional, Self

from cmk.agent_based.v1 import check_levels as check_levels_v1
from cmk.agent_based.v2 import (
Expand Down Expand Up @@ -209,6 +209,33 @@ def parse_raw(cls, raw: str) -> Self:
)


# https://www.freedesktop.org/software/systemd/man/latest/systemctl.html#is-enabled%20UNIT%E2%80%A6
class IsEnabled(StrEnum):
enabled = "enabled"
enabled_runtime = "enabled-runtime"
linked = "linked"
linked_runtime = "linked-runtime"
alias = "alias"
masked = "masked"
masked_runtime = "masked-runtime"
static = "static"
indirect = "indirect"
disabled = "disabled"
generated = "generated"
transient = "transient"
bad = "bad"
not_found = "not-found"

@staticmethod
def from_raw(raw: str | None) -> Optional["IsEnabled"]:
if raw is None:
return None
try:
return IsEnabled(raw)
except ValueError:
return None


# See: https://www.freedesktop.org/software/systemd/man/systemd.unit.html
class UnitTypes(Enum):
# When adding new systemd units, keep in mind to extend the gathering of the data via
Expand All @@ -233,7 +260,7 @@ def plural(self):
class UnitStatus:
name: str
status: str
enabled_status: str | None
enabled_status: IsEnabled | None
time_since_change: timedelta | None
cpu: CpuTimeSeconds | None = None
memory: Memory | None = None
Expand All @@ -242,7 +269,7 @@ class UnitStatus:
@classmethod
def from_entry(cls, entry: Sequence[Sequence[str]]) -> "UnitStatus":
name = entry[0][1].split(".", 1)[0]
enabled_status = entry[1][3].rstrip(";)") if len(entry[1]) >= 4 else None
enabled_status = IsEnabled(entry[1][3].rstrip(";)")) if len(entry[1]) >= 4 else None

timestr = " ".join(entry[2]).split(";", 1)[-1]
if "ago" in timestr:
Expand Down Expand Up @@ -283,7 +310,7 @@ class UnitEntry:
# both add and remove values. See systemctl --state=help
description: str
enabled_status: (
str | None
IsEnabled | None
) # see "Available unit file states:" in `systemctl --state=help` or _SYSTEMD_UNIT_FILE_STATES
time_since_change: timedelta | None = None
cpu_seconds: CpuTimeSeconds | None = None
Expand Down Expand Up @@ -319,9 +346,9 @@ def try_parse(
# Prefer enabled state from the status section over the list-unit-files, it is the actual instantiation of a service
# The status section is generated by the agent since a6a979ce and may not be present
enabled = (
status_details[name].enabled_status
if name in status_details
else enabled_status.get(f"{temp}{unit_type.suffix}")
IsEnabled.from_raw(enabled_status.get(f"{temp}{unit_type.suffix}"))
if name not in status_details
else status_details[name].enabled_status
)
remains = (" ".join(row[1:])).split(" ", 3)
if len(remains) == 3:
Expand Down
47 changes: 24 additions & 23 deletions tests/unit/cmk/plugins/collection/agent_based/test_systemd_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
discovery_systemd_units_services,
discovery_systemd_units_services_summary,
discovery_systemd_units_sockets,
IsEnabled,
Memory,
parse,
Section,
Expand All @@ -46,15 +47,15 @@
active_status="active",
current_state="running",
description="System Logging Service",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
),
UnitEntry(
name="alsa-state",
loaded_status="loaded",
active_status="inactive",
current_state="dead",
description="Manage Sound Card State (restore and store)",
enabled_status="disabled",
enabled_status=IsEnabled.disabled,
),
],
[],
Expand All @@ -74,7 +75,7 @@
active_status="active",
current_state="running",
description="System Logging Service",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
),
],
"excluded": [],
Expand All @@ -85,10 +86,10 @@
active_status="inactive",
current_state="dead",
description="Manage Sound Card State (restore and store)",
enabled_status="disabled",
enabled_status=IsEnabled.disabled,
)
],
"static": [],
IsEnabled.static: [],
"activating": [],
"deactivating": [],
"reloading": [],
Expand All @@ -110,7 +111,7 @@
active_status="active",
current_state="running",
description="System Logging Service",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
),
UnitEntry(
name="alsa-state",
Expand All @@ -130,7 +131,7 @@
active_status="active",
current_state="running",
description="System Logging Service",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
),
],
"excluded": [
Expand All @@ -150,7 +151,7 @@
active_status="inactive",
current_state="dead",
description="Manage Sound Card State (restore and store)",
enabled_status="indirect",
enabled_status=IsEnabled.indirect,
)
],
"static": [],
Expand Down Expand Up @@ -269,7 +270,7 @@ def test_services_split(
active_status="active",
current_state="exited",
description="LSB: VirtualBox Linux kernel module",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
)
},
),
Expand Down Expand Up @@ -298,7 +299,7 @@ def test_services_split(
)
},
),
id="Systemd unit status not available in list-unit-files mapping, use unknown instead",
id="Systemd unit status not available in list-unit-files mapping, use IsEnabled.unknown instead",
),
pytest.param(
[
Expand Down Expand Up @@ -349,7 +350,7 @@ def test_services_split(
active_status="active",
current_state="running",
description="NOT FROM SYSTEMD",
enabled_status="disabled",
enabled_status=IsEnabled.disabled,
time_since_change=timedelta(minutes=33),
cpu_seconds=CpuTimeSeconds(value=0.000815),
number_of_tasks=1,
Expand Down Expand Up @@ -382,7 +383,7 @@ def test_services_split(
active_status="active",
current_state="running",
description="SSSD NOT FROM SYSTEMD ONLY FOR TEST",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
time_since_change=None,
),
},
Expand Down Expand Up @@ -521,7 +522,7 @@ def test_services_split(
active_status="failed",
current_state="failed",
description="The WAS Server blablu",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
time_since_change=timedelta(days=13),
cpu_seconds=CpuTimeSeconds(value=180.0),
)
Expand Down Expand Up @@ -577,7 +578,7 @@ def test_services_split(
active_status="active",
current_state="running",
description="Checkmk agent (PID 1849349/UID 997)",
enabled_status="static",
enabled_status=IsEnabled.static,
time_since_change=timedelta(seconds=1),
cpu_seconds=CpuTimeSeconds(value=0.134),
memory=Memory(bytes=4089446),
Expand All @@ -591,7 +592,7 @@ def test_services_split(
active_status="active",
current_state="listening",
description="Local Checkmk agent socket",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
time_since_change=None,
cpu_seconds=None,
memory=None,
Expand Down Expand Up @@ -697,7 +698,7 @@ def test_parse_time_since_state_change(time: str, expected: timedelta) -> None:
active_status="active",
current_state="running",
description="SSSD NOT FROM SYSTEMD ONLY FOR TEST",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
time_since_change=expected,
),
},
Expand Down Expand Up @@ -730,7 +731,7 @@ def test_all_possible_service_states_in_status_section(icon: str) -> None:
active_status="active",
current_state="running",
description="SSSD NOT FROM SYSTEMD ONLY FOR TEST",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
time_since_change=timedelta(seconds=3),
),
},
Expand Down Expand Up @@ -763,7 +764,7 @@ def test_all_possible_service_states_in_all_section(icon: str) -> None:
active_status="active",
current_state="running",
description="SSSD NOT FROM SYSTEMD ONLY FOR TEST",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
time_since_change=timedelta(seconds=3),
),
},
Expand Down Expand Up @@ -818,15 +819,15 @@ def test_all_possible_service_states_in_all_section(icon: str) -> None:
active_status="active",
current_state="running",
description="Checkmk agent (127.0.0.1:51542)",
enabled_status="static",
enabled_status=IsEnabled.static,
),
"check-mk-enterprise-2021.09.07": UnitEntry(
name="check-mk-enterprise-2021.09.07",
loaded_status="loaded",
active_status="active",
current_state="exited",
description="LSB: OMD sites",
enabled_status="generated",
enabled_status=IsEnabled.generated,
),
},
)
Expand Down Expand Up @@ -1170,7 +1171,7 @@ def test_check_systemd_units_sockets(
active_status="activating",
current_state="exited",
description="LSB: VirtualBox Linux kernel module",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
time_since_change=timedelta(seconds=2),
),
},
Expand Down Expand Up @@ -1199,7 +1200,7 @@ def test_check_systemd_units_sockets(
active_status="reloading",
current_state="reload",
description="LSB: VirtualBox Linux kernel module",
enabled_status="enabled",
enabled_status=IsEnabled.enabled,
time_since_change=timedelta(seconds=2),
),
},
Expand Down Expand Up @@ -1228,7 +1229,7 @@ def test_check_systemd_units_sockets(
active_status="active",
current_state="exited",
description="LSB: VirtualBox Linux kernel module",
enabled_status="indirect",
enabled_status=IsEnabled.indirect,
),
},
),
Expand Down

0 comments on commit 2ed0a07

Please sign in to comment.