Skip to content

Commit 515a2db

Browse files
committed
bi: re-organize output
Use the check API as it is intended to avoid accessing the `state_marker`. Note that the `notice` fields will only be shown in the summary, if the state is not OK. Change-Id: Ic006c9505dfdad1cf2ee8c46df775b822e04c648
1 parent e3692ad commit 515a2db

File tree

2 files changed

+58
-34
lines changed

2 files changed

+58
-34
lines changed

cmk/plugins/collection/agent_based/bi_aggregation.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from dataclasses import dataclass, field
88
from typing import Any, TypedDict
99

10-
from cmk.checkengine.checkresults import state_markers # pylint: disable=cmk-module-layer-violation
11-
1210
from cmk.agent_based.v2 import (
1311
AgentSection,
1412
CheckPlugin,
@@ -50,7 +48,9 @@ class Aggregation:
5048

5149
@dataclass
5250
class AggregationError:
53-
output: str
51+
state: State
52+
notice: str
53+
details: str
5454
affects_state: bool
5555

5656

@@ -90,22 +90,35 @@ def get_aggregation_errors(
9090

9191
if aggr.error_state is not None:
9292
yield AggregationError(
93-
output=f"{state_markers[aggr.error_state]} {aggr.error_output}",
93+
state=State(aggr.error_state),
94+
notice=aggr.error_output or "",
95+
details=aggr.error_output or "",
9496
affects_state=affects_state,
9597
)
9698

9799
if aggr.custom_output is not None:
98-
yield AggregationError(output=aggr.custom_output, affects_state=affects_state)
100+
yield AggregationError(
101+
state=State.OK,
102+
notice=aggr.custom_output,
103+
details=aggr.custom_output,
104+
affects_state=affects_state,
105+
)
99106

100107
for child in aggr.children:
101108
if errors := list(get_aggregation_errors(child, affects_state)):
102109
yield AggregationError(
103-
output=f"+-- {errors[0].output}", affects_state=errors[0].affects_state
110+
state=errors[0].state,
111+
notice=errors[0].notice,
112+
details=f"+-- {errors[0].details}",
113+
affects_state=errors[0].affects_state,
104114
)
105115

106116
for error in errors[1:]:
107117
yield AggregationError(
108-
output=f"| {error.output}", affects_state=error.affects_state
118+
state=error.state,
119+
notice=error.notice,
120+
details=f"| {error.details}",
121+
affects_state=error.affects_state,
109122
)
110123

111124

@@ -136,23 +149,22 @@ def check_bi_aggregation(item: str, section: Section) -> CheckResult:
136149
summary="Acknowledged: %s" % ("yes" if bi_data["acknowledged"] else "no"),
137150
)
138151

139-
if bi_data["infos"]:
140-
aggregations = get_aggregations(bi_data["infos"])
141-
errors = list(get_aggregation_errors(aggregations, bool(overall_state)))
142-
143-
errors_affecting_state = [error.output for error in errors if error.affects_state]
144-
other_errors = [error.output for error in errors if not error.affects_state]
152+
if not bi_data["infos"]:
153+
return
145154

146-
infos = []
147-
if errors_affecting_state:
148-
infos.extend(["", "Aggregation problems affecting the state:"])
149-
infos.extend(errors_affecting_state)
155+
aggregations = get_aggregations(bi_data["infos"])
156+
errors = list(get_aggregation_errors(aggregations, bool(overall_state)))
150157

151-
if other_errors:
152-
infos.extend(["", "Aggregation problems not affecting the state:"])
153-
infos.extend(other_errors)
158+
if errors_affecting_state := [e for e in errors if e.affects_state]:
159+
yield Result(state=State.OK, notice="Aggregation problems affecting the state:")
160+
yield from (
161+
Result(state=e.state, notice=e.notice, details=e.details)
162+
for e in errors_affecting_state
163+
)
154164

155-
yield Result(state=State.OK, notice="\n".join(infos))
165+
if other_errors := [e for e in errors if not e.affects_state]:
166+
yield Result(state=State.OK, notice="Aggregation problems not affecting the state:")
167+
yield from (Result(state=e.state, notice=e.notice, details=e.details) for e in other_errors)
156168

157169

158170
check_plugin_bi_aggregation = CheckPlugin(

tests/unit/cmk/plugins/collection/agent_based/test_bi_aggregation.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,33 @@
7676

7777

7878
def test_check_bi_aggregation() -> None:
79-
expected_notice = (
80-
"\n"
81-
"Aggregation problems affecting the state:\n"
82-
"(!) Host test\n+-- (!) General State\n"
83-
"| +-- (!) Check_MK\n"
84-
"| | +-- (!) Check_MK Discovery, Services unmonitored: 1 (cpu_loads: 1)(!), Host labels: all up to date\n"
85-
"\n"
86-
"Aggregation problems not affecting the state:\n"
87-
"+-- +-- (!) Aggr Host test, Aggregation state: Warning(!), In downtime: no, Acknowledged: no\n"
88-
"| +-- (!!) OMD test_gestern Notification Spooler, Version: 2.3.0-2024.02.01, Status last updated 42 days 1 hour ago, spooler seems crashed or busy(!!)\n"
89-
"| +-- (!!) OMD stable Notification Spooler, Version: 2.2.0-2024.03.13, Status last updated 22 hours 17 minutes ago, spooler seems crashed or busy(!!)"
90-
)
9179
assert list(check_bi_aggregation("Host test", TEST_INFO)) == [
9280
Result(state=State.WARN, summary="Aggregation state: Warning"),
9381
Result(state=State.OK, summary="In downtime: no"),
9482
Result(state=State.OK, summary="Acknowledged: no"),
95-
Result(state=State.OK, notice=expected_notice),
83+
Result(state=State.OK, notice="Aggregation problems affecting the state:"),
84+
Result(state=State.WARN, notice="Host test"),
85+
Result(state=State.WARN, notice="General State", details="+-- General State"),
86+
Result(state=State.WARN, notice="Check_MK", details="| +-- Check_MK"),
87+
Result(
88+
state=State.WARN,
89+
notice="Check_MK Discovery, Services unmonitored: 1 (cpu_loads: 1)(!), Host labels: all up to date",
90+
details="| | +-- Check_MK Discovery, Services unmonitored: 1 (cpu_loads: 1)(!), Host labels: all up to date",
91+
),
92+
Result(state=State.OK, notice="Aggregation problems not affecting the state:"),
93+
Result(
94+
state=State.WARN,
95+
notice="Aggr Host test, Aggregation state: Warning(!), In downtime: no, Acknowledged: no",
96+
details="+-- +-- Aggr Host test, Aggregation state: Warning(!), In downtime: no, Acknowledged: no",
97+
),
98+
Result(
99+
state=State.CRIT,
100+
notice="OMD test_gestern Notification Spooler, Version: 2.3.0-2024.02.01, Status last updated 42 days 1 hour ago, spooler seems crashed or busy(!!)",
101+
details="| +-- OMD test_gestern Notification Spooler, Version: 2.3.0-2024.02.01, Status last updated 42 days 1 hour ago, spooler seems crashed or busy(!!)",
102+
),
103+
Result(
104+
state=State.CRIT,
105+
notice="OMD stable Notification Spooler, Version: 2.2.0-2024.03.13, Status last updated 22 hours 17 minutes ago, spooler seems crashed or busy(!!)",
106+
details="| +-- OMD stable Notification Spooler, Version: 2.2.0-2024.03.13, Status last updated 22 hours 17 minutes ago, spooler seems crashed or busy(!!)",
107+
),
96108
]

0 commit comments

Comments
 (0)