Switch graylog input metrics plugin to support <= instead of <

This commit is contained in:
Marsell Kukuljevic 2026-02-06 12:45:40 +01:00
parent 0e59597d5d
commit 387b103dcc
8 changed files with 75 additions and 2 deletions

Binary file not shown.

View File

@ -21,7 +21,7 @@ mappings={
"im": "messages",
"rs": "bytes",
"upper": "above",
"lower": "below",
"lower": "below or equal",
}

View File

@ -7,7 +7,7 @@
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
import json
from cmk.base.check_api import check_levels, LegacyCheckDefinition
from cmk.base.check_api import LegacyCheckDefinition
from cmk.base.config import check_info
from cmk.agent_based.v2 import Service
@ -74,6 +74,79 @@ def check_graylog_input_metrics(item, params, parsed):
infoname=infotext
)
# A customer wanted us to support <= for "below", not <. This meant copying
# check_levels() and child functions wholesale out of
# lib/python3/cmk/base/check_api.py just to change < to <= in this function.
# Sometimes a sledgehammer is what it takes... D:
# Start copy ===================================================================
from cmk.agent_based import v1 as _v1
def _do_check_levels(value, levels, human_readable_func):
warn_upper, crit_upper, warn_lower, crit_lower = levels
# Critical cases
if crit_upper is not None and value >= crit_upper:
return 2, _levelsinfo_ty("at", warn_upper, crit_upper, human_readable_func)
if crit_lower is not None and value <= crit_lower:
return 2, _levelsinfo_ty("below", warn_lower, crit_lower, human_readable_func)
# Warning cases
if warn_upper is not None and value >= warn_upper:
return 1, _levelsinfo_ty("at", warn_upper, crit_upper, human_readable_func)
if warn_lower is not None and value <= warn_lower:
return 1, _levelsinfo_ty("below", warn_lower, crit_lower, human_readable_func)
return 0, ""
def _levelsinfo_ty(ty, warn, crit, human_readable_func):
warn_str = "never" if warn is None else f"{human_readable_func(warn)}"
crit_str = "never" if crit is None else f"{human_readable_func(crit)}"
return f" (warn/crit {ty} {warn_str}/{crit_str})"
def _build_perfdata(dsname, value, levels, boundaries):
used_boundaries = boundaries if isinstance(boundaries, tuple) and len(boundaries) == 2 else ()
return [(dsname, value, levels[0], levels[1], *used_boundaries)]
def check_levels(value, dsname, params, unit, human_readable_func, infoname, boundaries=None):
def render_func(x):
return "%s%s" % (human_readable_func(x), unit)
if params and isinstance(params, dict):
result, *metrics = _v1.check_levels_predictive(
value,
levels=params,
metric_name=dsname,
render_func=render_func,
label=infoname,
boundaries=boundaries,
)
assert isinstance(result, _v1.Result)
return (
int(result.state),
result.summary,
[
(m.name, m.value, *m.levels, *m.boundaries)
for m in metrics
if isinstance(m, _v1.Metric)
],
)
infotext = f"{render_func(value)}"
if infoname:
infotext = f"{infoname}: {infotext}"
levels = params
state, levelstext = _do_check_levels(value, levels, render_func)
state, levelstext = _do_check_levels(value, levels, render_func)
return (
state,
infotext + levelstext,
_build_perfdata(dsname, value, levels, boundaries),
)
# End copy ====================================================================
check_info["graylog_input_metrics"] = LegacyCheckDefinition(
parse_function = parse_graylog_input_metrics,
check_function = check_graylog_input_metrics,