From b49e1b09b49d332b222bd14b440b289f9bd965ef Mon Sep 17 00:00:00 2001 From: Dein Vor- und Nachname Date: Thu, 27 Aug 2026 17:53:41 +0000 Subject: [PATCH] Add toggable hiding of cache information in summary for local checks & MRPEs \n\n Add service montoring rules for cached local and MRPE checks that enable the user to toggle whether the caching information is visible in the summary and the details or only the details. --- .../plugins/wato/check_parameters/local.py | 28 ++++++++++++- cmk/gui/plugins/wato/check_parameters/mrpe.py | 39 +++++++++++++++++++ cmk/plugins/checkmk/agent_based/local.py | 11 +++++- cmk/plugins/checkmk/agent_based/mrpe.py | 22 +++++++++-- .../plugins/checkmk/agent_based/test_mrpe.py | 10 ++--- 5 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 cmk/gui/plugins/wato/check_parameters/mrpe.py diff --git a/cmk/gui/plugins/wato/check_parameters/local.py b/cmk/gui/plugins/wato/check_parameters/local.py index d274d5f0be1..6c255aa9406 100644 --- a/cmk/gui/plugins/wato/check_parameters/local.py +++ b/cmk/gui/plugins/wato/check_parameters/local.py @@ -11,7 +11,7 @@ RulespecGroupCheckParametersApplications, RulespecGroupEnforcedServicesApplications, ) -from cmk.gui.valuespec import Dictionary, DropdownChoice, TextInput +from cmk.gui.valuespec import Dictionary, DropdownChoice, FixedValue, TextInput rulespec_registry.register( ManualCheckParameterRulespec( @@ -55,6 +55,21 @@ def _parameter_valuespec_local() -> Dictionary: ) +def _parameter_valuespec_show_cache_info_local() -> Dictionary: + return Dictionary( + elements=[ + ( + "hide_cache_info_from_summary", + FixedValue( + True, + title=_("Hide caching information from service summary"), + totext="", + ), + ), + ] + ) + + rulespec_registry.register( CheckParameterRulespecWithItem( check_group_name="local", @@ -66,3 +81,14 @@ def _parameter_valuespec_local() -> Dictionary: is_deprecated=True, ) ) + +rulespec_registry.register( + CheckParameterRulespecWithItem( + check_group_name="hide_cache_info_local", + group=RulespecGroupCheckParametersApplications, + item_spec=lambda: TextInput(title=_("Name of local item")), + match_type="dict", + parameter_valuespec=_parameter_valuespec_show_cache_info_local, + title=lambda: _("Local checks: Hide cache information"), + ) +) diff --git a/cmk/gui/plugins/wato/check_parameters/mrpe.py b/cmk/gui/plugins/wato/check_parameters/mrpe.py new file mode 100644 index 00000000000..b067474092c --- /dev/null +++ b/cmk/gui/plugins/wato/check_parameters/mrpe.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2 +# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and +# conditions defined in the file COPYING, which is part of this source code package. + +from cmk.gui.i18n import _ +from cmk.gui.plugins.wato.utils import ( + CheckParameterRulespecWithItem, + rulespec_registry, + RulespecGroupCheckParametersApplications, +) +from cmk.gui.valuespec import Dictionary, FixedValue, TextInput + + +def _parameter_valuespec_hide_cache_info_mrpe() -> Dictionary: + return Dictionary( + elements=[ + ( + "hide_cache_info_from_summary", + FixedValue( + True, + title=_("Hide caching information from service summary"), + totext="", + ), + ), + ] + ) + + +rulespec_registry.register( + CheckParameterRulespecWithItem( + check_group_name="hide_cache_info_mrpe", + group=RulespecGroupCheckParametersApplications, + item_spec=lambda: TextInput(title=_("Name of MRPE check")), + match_type="dict", + parameter_valuespec=_parameter_valuespec_hide_cache_info_mrpe, + title=lambda: _("MRPE checks: Hide cache information"), + ) +) diff --git a/cmk/plugins/checkmk/agent_based/local.py b/cmk/plugins/checkmk/agent_based/local.py index 51227a73880..ac3aa73e2ca 100644 --- a/cmk/plugins/checkmk/agent_based/local.py +++ b/cmk/plugins/checkmk/agent_based/local.py @@ -440,6 +440,11 @@ def check_local(item: str, params: Mapping[str, Any], section: LocalSection) -> except ValueError: summary, details = local_result.text, "" + hide = params.get("hide_cache_info_from_summary", False) + + if local_result.cache_info is not None and hide: + summary = f"{summary} ◷" if summary else "◷" + if local_result.text: yield Result( state=local_result.state, @@ -448,7 +453,9 @@ def check_local(item: str, params: Mapping[str, Any], section: LocalSection) -> ) yield from _local_make_metrics(local_result) - if local_result.cache_info is not None: + if local_result.cache_info is not None and hide: + yield Result(state=State.OK, notice=render_cache_info(local_result.cache_info)) + elif local_result.cache_info is not None and not hide: yield Result(state=State.OK, summary=render_cache_info(local_result.cache_info)) @@ -457,6 +464,6 @@ def check_local(item: str, params: Mapping[str, Any], section: LocalSection) -> service_name="%s", discovery_function=discover_local, check_default_parameters={}, - check_ruleset_name="local", + check_ruleset_name="hide_cache_info_local", check_function=check_local, ) diff --git a/cmk/plugins/checkmk/agent_based/mrpe.py b/cmk/plugins/checkmk/agent_based/mrpe.py index c59e1a59728..b105c45035e 100644 --- a/cmk/plugins/checkmk/agent_based/mrpe.py +++ b/cmk/plugins/checkmk/agent_based/mrpe.py @@ -6,7 +6,7 @@ import time import urllib.parse from collections.abc import Mapping, Sequence -from typing import NamedTuple +from typing import NamedTuple, TypedDict from cmk.agent_based.v2 import ( AgentSection, @@ -32,6 +32,10 @@ class PluginData(NamedTuple): MRPESection = Mapping[str, PluginData] +class MRPEParams(TypedDict, total=False): + hide_cache_info_from_summary: bool + + def parse_mrpe(string_table: StringTable) -> MRPESection: parsed = {} for line in string_table: @@ -143,7 +147,7 @@ def _parse_nagios_perfstring(perfinfo: str) -> Metric: ) -def check_mrpe(item: str, section: MRPESection) -> CheckResult: +def check_mrpe(item: str, params: MRPEParams, section: MRPESection) -> CheckResult: dataset = section.get(item) if dataset is None: return @@ -165,15 +169,23 @@ def check_mrpe(item: str, section: MRPESection) -> CheckResult: perfdata += parts[1].strip().split() now_comes_perfdata = True + hide = params.get("hide_cache_info_from_summary", False) + + summary = output[0] if output[0] else "No further information available" + if dataset.cache_info is not None and hide: + summary = f"{summary} ◷" + yield Result( state=dataset.state, - summary=output[0] if output[0] else "No further information available", + summary=summary, details="\n".join(output) if output[0] else None, ) yield from _output_metrics(perfdata) # This is at the end of the summary, to be consistent with local checks. - if dataset.cache_info is not None: + if dataset.cache_info is not None and hide: + yield Result(state=State.OK, notice=cache_helper.render_cache_info(dataset.cache_info)) + elif dataset.cache_info is not None and not hide: yield Result(state=State.OK, summary=cache_helper.render_cache_info(dataset.cache_info)) # name of check command needed for PNP to choose the correct template @@ -185,5 +197,7 @@ def check_mrpe(item: str, section: MRPESection) -> CheckResult: name="mrpe", discovery_function=discover_mrpe, check_function=check_mrpe, + check_default_parameters={}, + check_ruleset_name="hide_cache_info_mrpe", service_name="%s", ) diff --git a/tests/unit/cmk/plugins/checkmk/agent_based/test_mrpe.py b/tests/unit/cmk/plugins/checkmk/agent_based/test_mrpe.py index 947f54e999a..2876b3a3c01 100644 --- a/tests/unit/cmk/plugins/checkmk/agent_based/test_mrpe.py +++ b/tests/unit/cmk/plugins/checkmk/agent_based/test_mrpe.py @@ -92,7 +92,7 @@ def test_discovery() -> None: def test_check_mrpe() -> None: - assert list(check_mrpe("Bar_Extender", SECTION)) == [ + assert list(check_mrpe("Bar_Extender", {}, SECTION)) == [ Result( state=State.WARN, summary="Bar extender overload 6.012", @@ -100,14 +100,14 @@ def test_check_mrpe() -> None: Metric("bar_load", 6.012), ] - assert list(check_mrpe("Foo_Application", SECTION)) == [ + assert list(check_mrpe("Foo_Application", {}, SECTION)) == [ Result( state=State.OK, summary="Foo server up and running", ), ] - assert list(check_mrpe("Mutliliner", SECTION)) == [ + assert list(check_mrpe("Mutliliner", {}, SECTION)) == [ Result( state=State.UNKNOWN, summary="Invalid plug-in status '§$%'. Output is: Output1", @@ -124,7 +124,7 @@ def test_check_mrpe() -> None: def test_check_invalid_metric() -> None: - assert list(check_mrpe("Invalid_Metric", SECTION)) == [ + assert list(check_mrpe("Invalid_Metric", {}, SECTION)) == [ Result( state=State.OK, summary="I would be ok, if it wasn't for the metric", @@ -146,7 +146,7 @@ def test_check_metric_name_with_invalid_character() -> None: cache_info=None, ) } - assert list(check_mrpe("Disk", section)) == [ + assert list(check_mrpe("Disk", {}, section)) == [ Result( state=State.OK, summary="DISK OK - free space: / 12483 MB",