Skip to content

Commit 32bada9

Browse files
Modernize graylog_failures
Migrate the check to the agent_based.v2 API with check_levels and TypedDict parameters, and migrate the check parameters from the legacy GUI valuespec to a cmk.rulesets.v1 form spec living in the plugin package. CMK-34762 Change-Id: Ic0ffcf9f9e3b43ca29dd09c16a06d6841400ed60
1 parent d29b0dd commit 32bada9

4 files changed

Lines changed: 78 additions & 70 deletions

File tree

cmk/gui/plugins/wato/check_parameters/graylog_failures.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

packages/cmk-plugins/cmk/plugins/graylog/agent_based/graylog_failures.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
# mypy: disable-error-code="exhaustive-match"
77

88
import json
9-
from collections.abc import Collection, Iterable, Mapping, Sequence
9+
from collections.abc import Collection, Iterable, Sequence
10+
from typing import TypedDict
1011

1112
from pydantic import BaseModel, Field
1213

13-
from cmk.agent_based.v1 import check_levels as check_levels_v1
1414
from cmk.agent_based.v2 import (
1515
AgentSection,
16+
check_levels,
1617
CheckPlugin,
1718
CheckResult,
1819
DiscoveryResult,
20+
LevelsT,
1921
render,
2022
Result,
2123
Service,
@@ -25,6 +27,11 @@
2527
from cmk.plugins.graylog.lib import deserialize_and_merge_json
2628

2729

30+
class FailuresParams(TypedDict):
31+
failures: LevelsT[int]
32+
failures_last: LevelsT[int]
33+
34+
2835
class FailureMessage(BaseModel):
2936
type: str | None
3037
reason: str | None
@@ -121,15 +128,15 @@ def _failure_results(failures: Collection[Failure]) -> CheckResult:
121128

122129

123130
def check(
124-
params: Mapping[str, tuple[int, int] | None],
131+
params: FailuresParams,
125132
section: Section,
126133
) -> CheckResult:
127134
if section.failures is None or section.total is None:
128135
return
129136

130-
yield from check_levels_v1(
137+
yield from check_levels(
131138
value=section.total,
132-
levels_upper=params.get("failures"),
139+
levels_upper=params["failures"],
133140
metric_name="failures",
134141
render_func=str,
135142
label="Total number of failures",
@@ -138,9 +145,9 @@ def check(
138145
if section.count is None:
139146
return
140147

141-
yield from check_levels_v1(
148+
yield from check_levels(
142149
value=section.count,
143-
levels_upper=params.get("failures_last"),
150+
levels_upper=params["failures_last"],
144151
metric_name=None,
145152
render_func=str,
146153
label=f"Failures in last {render.timespan(section.ds_param_since)}",
@@ -157,6 +164,9 @@ def check(
157164
service_name="Graylog Index Failures",
158165
discovery_function=discover,
159166
check_function=check,
160-
check_default_parameters={},
167+
check_default_parameters={
168+
"failures": ("no_levels", None),
169+
"failures_last": ("no_levels", None),
170+
},
161171
check_ruleset_name="graylog_failures",
162172
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
3+
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
4+
# conditions defined in the file COPYING, which is part of this source code package.
5+
6+
from cmk.rulesets.v1 import Help, Title
7+
from cmk.rulesets.v1.form_specs import (
8+
DictElement,
9+
Dictionary,
10+
InputHint,
11+
Integer,
12+
LevelDirection,
13+
migrate_to_integer_simple_levels,
14+
SimpleLevels,
15+
)
16+
from cmk.rulesets.v1.rule_specs import CheckParameters, HostCondition, Topic
17+
18+
19+
def _parameter_valuespec_graylog_failures() -> Dictionary:
20+
return Dictionary(
21+
elements={
22+
"failures": DictElement(
23+
parameter_form=SimpleLevels[int](
24+
title=Title("Total index failure count upper levels"),
25+
level_direction=LevelDirection.UPPER,
26+
form_spec_template=Integer(),
27+
prefill_fixed_levels=InputHint((0, 0)),
28+
migrate=migrate_to_integer_simple_levels,
29+
),
30+
),
31+
"failures_last": DictElement(
32+
parameter_form=SimpleLevels[int](
33+
title=Title("Index failure in the defined time range upper levels"),
34+
help_text=Help(
35+
"Here, you can set levels on failures in the time range "
36+
"specified in the data source (default: 30 min)."
37+
),
38+
level_direction=LevelDirection.UPPER,
39+
form_spec_template=Integer(),
40+
prefill_fixed_levels=InputHint((0, 0)),
41+
migrate=migrate_to_integer_simple_levels,
42+
),
43+
),
44+
},
45+
)
46+
47+
48+
rule_spec_graylog_failures = CheckParameters(
49+
name="graylog_failures",
50+
title=Title("Graylog index failures"),
51+
topic=Topic.APPLICATIONS,
52+
parameter_form=_parameter_valuespec_graylog_failures,
53+
condition=HostCondition(),
54+
)

packages/cmk-plugins/tests/cmk/plugins/graylog/agent_based/test_graylog_failures.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
44
# conditions defined in the file COPYING, which is part of this source code package.
55

6-
from collections.abc import Mapping
7-
86
import pytest
97

108
from cmk.agent_based.v2 import (
@@ -92,7 +90,7 @@ def test_discover(
9290
[
9391
pytest.param(
9492
_STRING_TABLE_ZERO_FAILURES,
95-
{},
93+
{"failures": ("no_levels", None), "failures_last": ("no_levels", None)},
9694
[
9795
Result(state=State.OK, summary="Total number of failures: 198508"),
9896
Metric("failures", 198508.0),
@@ -102,7 +100,7 @@ def test_discover(
102100
),
103101
pytest.param(
104102
_STRING_TABLE_MSG_DICT,
105-
{},
103+
{"failures": ("no_levels", None), "failures_last": ("no_levels", None)},
106104
[
107105
Result(state=State.OK, summary="Total number of failures: 198508"),
108106
Metric("failures", 198508.0),
@@ -121,8 +119,8 @@ def test_discover(
121119
pytest.param(
122120
_STRING_TABLE_MSG_DICT,
123121
{
124-
"failures": (5000, 2000),
125-
"failures_last": (1, 10),
122+
"failures": ("fixed", (5000, 2000)),
123+
"failures_last": ("fixed", (1, 10)),
126124
},
127125
[
128126
Result(
@@ -147,7 +145,7 @@ def test_discover(
147145
),
148146
pytest.param(
149147
_STRING_TABLE_MSG_STR,
150-
{},
148+
{"failures": ("no_levels", None), "failures_last": ("no_levels", None)},
151149
[
152150
Result(state=State.OK, summary="Total number of failures: 131346"),
153151
Metric("failures", 131346.0),
@@ -167,7 +165,7 @@ def test_discover(
167165
)
168166
def test_check(
169167
string_table: StringTable,
170-
params: Mapping[str, tuple[int, int] | None],
168+
params: graylog_failures.FailuresParams,
171169
expected_result: CheckResult,
172170
) -> None:
173171
section = graylog_failures.parse(string_table)

0 commit comments

Comments
 (0)