Skip to content

Commit f07225f

Browse files
committed
Fixed Graylog alerts
Graylog changed API call for quering alerts and events. Added graph for showing alerts and events. Signed-off-by: Sven Rueß <[email protected]>
1 parent 8b8c1e1 commit f07225f

File tree

14 files changed

+340
-258
lines changed

14 files changed

+340
-258
lines changed

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

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

cmk/plugins/collection/agent_based/graylog_alerts.py

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

cmk/plugins/graylog/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python3
2+
# Copyright (C) 2024 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.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python3
2+
# Copyright (C) 2024 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.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
"""
7+
Kuhn & Rueß GmbH
8+
Consulting and Development
9+
https://kuhn-ruess.de
10+
"""
11+
12+
from collections.abc import Mapping
13+
from json import loads
14+
from typing import Any, NamedTuple
15+
16+
from cmk.agent_based.v2 import (
17+
AgentSection,
18+
check_levels,
19+
CheckPlugin,
20+
CheckResult,
21+
DiscoveryResult,
22+
Service,
23+
StringTable,
24+
)
25+
26+
# <<<graylog_alerts>>>
27+
# {"alerts": {"num_of_events": 547, "num_of_alerts": 4}}
28+
29+
# <<<graylog_alerts>>>
30+
# {"alerts": {"num_of_events": 5, "num_of_alerts": 0}}
31+
32+
33+
class AlertsInfo(NamedTuple):
34+
num_of_events: int
35+
num_of_alerts: int
36+
37+
38+
def parse_graylog_alerts(string_table: StringTable) -> AlertsInfo | None:
39+
"""
40+
Parse JSON data to AlertsInfo
41+
"""
42+
alerts_section = loads(string_table[0][0])
43+
44+
if len(alerts_section) != 1:
45+
return None
46+
47+
alerts_data = alerts_section.get("alerts")
48+
49+
return AlertsInfo(
50+
num_of_events=alerts_data.get("num_of_events"),
51+
num_of_alerts=alerts_data.get("num_of_alerts"),
52+
)
53+
54+
55+
agent_section_graylog_alerts = AgentSection(
56+
name="graylog_alerts",
57+
parse_function=parse_graylog_alerts,
58+
)
59+
60+
61+
def discover_graylog_alerts(section: AlertsInfo) -> DiscoveryResult:
62+
"""
63+
Discover one service
64+
"""
65+
if section:
66+
yield Service(item=None)
67+
68+
69+
def check_graylog_alerts(params: Mapping[str, Any], section: AlertsInfo) -> CheckResult:
70+
for which in ["alerts", "events"]:
71+
yield from check_levels(
72+
value=(section._asdict())[f"num_of_{which}"],
73+
levels_upper=params.get(f"{which}_upper", None),
74+
levels_lower=params.get(f"{which}_lower", None),
75+
metric_name=f"graylog_{which}",
76+
render_func=lambda x: str(int(x)),
77+
label=f"Total number of {which}",
78+
)
79+
80+
81+
check_plugin_graylog_alerts = CheckPlugin(
82+
name="graylog_alerts",
83+
sections=["graylog_alerts"],
84+
service_name="Graylog Cluster Alerts",
85+
discovery_function=discover_graylog_alerts,
86+
check_function=check_graylog_alerts,
87+
check_default_parameters={},
88+
check_ruleset_name="graylog_alerts",
89+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python3
2+
# Copyright (C) 2024 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.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Kuhn & Rueß GmbH
5+
Consulting and Development
6+
https://kuhn-ruess.de
7+
"""
8+
9+
from cmk.graphing.v1 import Title
10+
from cmk.graphing.v1.graphs import Graph
11+
from cmk.graphing.v1.metrics import Color, DecimalNotation, Metric, Unit
12+
13+
UNIT_NUMBER = Unit(DecimalNotation(""))
14+
15+
metric_graylog_alerts = Metric(
16+
name="graylog_alerts",
17+
title=Title("Total amount of alerts"),
18+
unit=UNIT_NUMBER,
19+
color=Color.BLUE,
20+
)
21+
metric_graylog_events = Metric(
22+
name="graylog_events",
23+
title=Title("Total amount of events"),
24+
unit=UNIT_NUMBER,
25+
color=Color.GREEN,
26+
)
27+
28+
graph_graylog_alerts = Graph(
29+
name="gralog_alerts",
30+
title=Title("Graylog alerts and events"),
31+
simple_lines=["graylog_alerts", "graylog_events"],
32+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python3
2+
# Copyright (C) 2024 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.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Kuhn & Rueß GmbH
5+
Consulting and Development
6+
https://kuhn-ruess.de
7+
"""
8+
9+
from cmk.rulesets.v1 import Title
10+
from cmk.rulesets.v1.form_specs import (
11+
DictElement,
12+
Dictionary,
13+
InputHint,
14+
Integer,
15+
LevelDirection,
16+
SimpleLevels,
17+
)
18+
from cmk.rulesets.v1.rule_specs import CheckParameters, HostCondition, Topic
19+
20+
21+
def _parameter_form_graylog_alerts():
22+
return Dictionary(
23+
title=Title("Graylog alerts"),
24+
elements={
25+
"alerts_upper": DictElement(
26+
parameter_form=SimpleLevels(
27+
title=Title("Total alerts count upper levels"),
28+
level_direction=LevelDirection.UPPER,
29+
form_spec_template=Integer(),
30+
prefill_fixed_levels=InputHint((0, 0)),
31+
)
32+
),
33+
"alerts_lower": DictElement(
34+
parameter_form=SimpleLevels(
35+
title=Title("Total alerts count lower levels"),
36+
level_direction=LevelDirection.LOWER,
37+
form_spec_template=Integer(),
38+
prefill_fixed_levels=InputHint((0, 0)),
39+
)
40+
),
41+
"events_upper": DictElement(
42+
parameter_form=SimpleLevels(
43+
title=Title("Total events count upper levels"),
44+
level_direction=LevelDirection.UPPER,
45+
form_spec_template=Integer(),
46+
prefill_fixed_levels=InputHint((0, 0)),
47+
)
48+
),
49+
"events_lower": DictElement(
50+
parameter_form=SimpleLevels(
51+
title=Title("Total events count lower levels"),
52+
level_direction=LevelDirection.LOWER,
53+
form_spec_template=Integer(),
54+
prefill_fixed_levels=InputHint((0, 0)),
55+
)
56+
),
57+
},
58+
)
59+
60+
61+
rule_spec_graylog_alerts = CheckParameters(
62+
name="graylog_alerts",
63+
topic=Topic.APPLICATIONS,
64+
condition=HostCondition(),
65+
parameter_form=_parameter_form_graylog_alerts,
66+
title=Title("Graylog alerts"),
67+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python3
2+
# Copyright (C) 2024 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.

0 commit comments

Comments
 (0)