Skip to content

Commit c15a3f3

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ß <github@sven-ruess.de>
1 parent 71f7280 commit c15a3f3

16 files changed

Lines changed: 352 additions & 268 deletions

File tree

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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
13+
from collections.abc import Mapping
14+
from json import loads
15+
from typing import Any, NamedTuple
16+
17+
from cmk.agent_based.v2 import (
18+
AgentSection,
19+
check_levels,
20+
CheckPlugin,
21+
CheckResult,
22+
DiscoveryResult,
23+
Service,
24+
StringTable,
25+
)
26+
27+
# <<<graylog_alerts>>>
28+
# {"alerts": {"num_of_events": 547, "num_of_alerts": 4}}
29+
30+
# <<<graylog_alerts>>>
31+
# {"alerts": {"num_of_events": 5, "num_of_alerts": 0}}
32+
33+
34+
class AlertsInfo(NamedTuple):
35+
num_of_events: int
36+
num_of_alerts: int
37+
38+
39+
def parse_graylog_alerts(string_table: StringTable) -> AlertsInfo | None:
40+
"""
41+
Parse JSON data to AlertsInfo
42+
"""
43+
alerts_section = loads(string_table[0][0])
44+
45+
if len(alerts_section) != 1:
46+
return None
47+
48+
alerts_data = alerts_section.get("alerts")
49+
50+
return AlertsInfo(
51+
num_of_events=alerts_data.get("num_of_events"),
52+
num_of_alerts=alerts_data.get("num_of_alerts"),
53+
)
54+
55+
56+
agent_section_graylog_alerts = AgentSection(
57+
name="graylog_alerts",
58+
parse_function=parse_graylog_alerts,
59+
)
60+
61+
62+
def discover_graylog_alerts(section: AlertsInfo) -> DiscoveryResult:
63+
"""
64+
Discover one service
65+
"""
66+
if section:
67+
yield Service(item=None)
68+
69+
70+
def check_graylog_alerts(params: Mapping[str, Any], section: AlertsInfo) -> CheckResult:
71+
for which in ["alerts", "events"]:
72+
yield from check_levels(
73+
value=(section._asdict())[f"num_of_{which}"],
74+
levels_upper=params.get(f"{which}_upper", None),
75+
levels_lower=params.get(f"{which}_lower", None),
76+
metric_name=f"graylog_{which}",
77+
render_func=lambda x: str(int(x)),
78+
label=f"Total number of {which}",
79+
)
80+
81+
82+
check_plugin_graylog_alerts = CheckPlugin(
83+
name="graylog_alerts",
84+
sections=["graylog_alerts"],
85+
service_name="Graylog Cluster Alerts",
86+
discovery_function=discover_graylog_alerts,
87+
check_function=check_graylog_alerts,
88+
check_default_parameters={},
89+
check_ruleset_name="graylog_alerts",
90+
)
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
title: Graylog: Sources
1+
title: Graylog: Alerts
22
agents: graylog
33
catalog: app/graylog
44
license: GPLv2
55
distribution: check_mk
66
description:
7-
This check plug-in monitors the count of alerts a Graylog
8-
instance. It outputs the total number of alerts.
9-
You can also configure the check plug-in to monitor the number of alerts in a given timeframe.
7+
This check plug-in monitors the count of alerts and events of a Graylog
8+
instance. It outputs the total number of alerts and events.
9+
You can also configure the check plug-in to monitor the number of alerts
10+
and events in a given timeframe.
1011
You would do that by configuring the "Time for coverage of alerts" option
1112
when configuring the Graylog special agent.
1213

1314
item:
1415
Does not have an item.
1516

1617
discovery:
17-
One service is created.
18+
One service is created.
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+
)

0 commit comments

Comments
 (0)