Module: check_mk
Branch: master
Commit: e7ce93b73b76a9fc61623a343bcc7a2ab69a8dc9
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e7ce93b73b76a9…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Sun Apr 7 20:52:14 2019 +0200
Show effective labels of hosts / service on WATO "Parameters of X" page
The page is used to show effective settings of the hosts and services,
most of them are based on ruleset configurations, but the pages are not
limited to this.
The page now also shows the effective labels for hosts and services
of all three possible sources (explicit host attribute, ruleset,
discovered).
Change-Id: I5d5e181b78a0c80b6e24a808504dbf7f684aa873
---
cmk/gui/wato/pages/object_parameters.py | 36 ++++++++++++++++--
cmk_base/automations/check_mk.py | 51 +++++++++++++++++++++++--
tests/unit/cmk_base/test_unit_automations.py | 56 ++++++++++++++++++++++++++++
3 files changed, 135 insertions(+), 8 deletions(-)
diff --git a/cmk/gui/wato/pages/object_parameters.py
b/cmk/gui/wato/pages/object_parameters.py
index e27e4aa..de8252d 100644
--- a/cmk/gui/wato/pages/object_parameters.py
+++ b/cmk/gui/wato/pages/object_parameters.py
@@ -32,6 +32,7 @@ import cmk
import cmk.gui.config as config
import cmk.gui.watolib as watolib
import cmk.gui.forms as forms
+import cmk.gui.view_utils
from cmk.gui.i18n import _
from cmk.gui.globals import html
from cmk.gui.exceptions import MKUserError
@@ -109,10 +110,11 @@ class ModeObjectParameters(WatoMode):
all_rulesets = watolib.AllRulesets()
all_rulesets.load()
- # For services we make a special handling the for origin and parameters
- # of that service!
+ # Object type specific detail information
if self._service:
- self._show_service_rules(all_rulesets)
+ self._show_service_info(all_rulesets)
+ else:
+ self._show_host_info()
last_maingroup = None
for groupname in
sorted(rulespec_group_registry.get_host_rulespec_group_names()):
@@ -137,7 +139,16 @@ class ModeObjectParameters(WatoMode):
forms.end()
- def _show_service_rules(self, all_rulesets):
+ def _show_host_info(self):
+ host_info = watolib.check_mk_automation(self._host.site_id(),
"analyse-host",
+ [self._hostname])
+ if not host_info:
+ return
+
+ forms.header(_("Host information"), isopen=True, narrow=True,
css="rulesettings")
+ self._show_labels(host_info["labels"], "host")
+
+ def _show_service_info(self, all_rulesets):
serviceinfo = watolib.check_mk_automation(self._host.site_id(),
"analyse-service",
[self._hostname, self._service])
if not serviceinfo:
@@ -261,6 +272,23 @@ class ModeObjectParameters(WatoMode):
html.close_tr()
html.close_table()
+ self._show_labels(serviceinfo.get("labels", []), "service")
+
+ def _show_labels(self, labels, object_type):
+ forms.section(_("Effective labels"))
+ html.open_table(class_="setting")
+ html.open_tr()
+
+ html.open_td(class_="reason")
+ html.i(_("Explicit, ruleset, discovered"))
+ html.close_td()
+ html.open_td(class_=["settingvalue", "used"])
+ html.write(cmk.gui.view_utils.render_labels(labels, object_type,
with_links=False))
+ html.close_td()
+
+ html.close_tr()
+ html.close_table()
+
def _render_rule_reason(self, title, title_url, reason, reason_url, is_default,
setting):
if title_url:
title = html.render_a(title, href=title_url)
diff --git a/cmk_base/automations/check_mk.py b/cmk_base/automations/check_mk.py
index 2d13555..7520a73 100644
--- a/cmk_base/automations/check_mk.py
+++ b/cmk_base/automations/check_mk.py
@@ -569,15 +569,21 @@ class AutomationAnalyseServices(Automation):
needs_config = True
needs_checks = True # TODO: Can we change this?
+ def execute(self, args):
+ config_cache = config.get_config_cache()
+ hostname = args[0]
+ servicedesc = args[1].decode("utf-8")
+
+ service_info = self._get_service_info(config_cache, hostname, servicedesc)
+ service_info["labels"] = config_cache.labels_of_service(hostname,
servicedesc)
+ return service_info
+
# Determine the type of the check, and how the parameters are being
# constructed
# TODO: Refactor this huge function
# TODO: Was ist mit Clustern???
# TODO: Klappt das mit automatischen verschatten von SNMP-Checks (bei dual
Monitoring)
- def execute(self, args):
- config_cache = config.get_config_cache()
- hostname = args[0]
- servicedesc = args[1].decode("utf-8")
+ def _get_service_info(self, config_cache, hostname, servicedesc):
check_api_utils.set_hostname(hostname)
# We just consider types of checks that are managed via WATO.
@@ -703,6 +709,20 @@ class AutomationAnalyseServices(Automation):
automations.register(AutomationAnalyseServices())
+class AutomationAnalyseHost(Automation):
+ cmd = "analyse-host"
+ needs_config = True
+ needs_checks = False
+
+ def execute(self, args):
+ host_name = args[0]
+ config_cache = config.get_config_cache()
+ return {"labels": config_cache.get_host_config(host_name).labels}
+
+
+automations.register(AutomationAnalyseHost())
+
+
class AutomationDeleteHosts(Automation):
cmd = "delete-hosts"
needs_config = True
@@ -1497,3 +1517,26 @@ class AutomationGetServiceConfigurations(Automation):
automations.register(AutomationGetServiceConfigurations())
+
+
+class AutomationGetLabelsOf(Automation):
+ cmd = "get-labels-of"
+ needs_config = True
+ needs_checks = False
+
+ def execute(self, args):
+ object_type, host_name = args[:2]
+
+ config_cache = config.get_config_cache()
+
+ if object_type == "host":
+ return {"labels": config_cache.get_host_config(host_name).labels}
+
+ if object_type == "service":
+ service_description = args[2].decode("utf-8")
+ return {"labels": config_cache.labels_of_service(host_name,
service_description)}
+
+ raise NotImplementedError()
+
+
+automations.register(AutomationGetLabelsOf())
diff --git a/tests/unit/cmk_base/test_unit_automations.py
b/tests/unit/cmk_base/test_unit_automations.py
index 1a6ca0f..1789606 100644
--- a/tests/unit/cmk_base/test_unit_automations.py
+++ b/tests/unit/cmk_base/test_unit_automations.py
@@ -28,3 +28,59 @@ def test_static_check_rules_of_host(monkeypatch):
'param1': 1
}),
]
+
+
+def test_get_labels_of_host(monkeypatch):
+ automation = automations.AutomationGetLabelsOf()
+
+ monkeypatch.setattr(config, "all_hosts", ["test-host"])
+ monkeypatch.setattr(config, "host_paths", {"test-host":
"/"})
+ monkeypatch.setattr(config, "host_labels", {
+ "test-host": {
+ "explicit": "ding",
+ },
+ })
+ config.get_config_cache().initialize()
+
+ assert automation.execute(["host", "test-host"]) ==
{"labels": {"explicit": "ding"}}
+
+
+def test_get_labels_of_service(monkeypatch):
+ automation = automations.AutomationGetLabelsOf()
+
+ monkeypatch.setattr(config, "all_hosts", ["test-host"])
+ monkeypatch.setattr(config, "host_paths", {"test-host":
"/"})
+
+ ruleset = [
+ ({
+ "label1": "val1"
+ }, [], config.ALL_HOSTS, ["CPU load$"], {}),
+ ({
+ "label2": "val2"
+ }, [], config.ALL_HOSTS, ["CPU load$"], {}),
+ ]
+ monkeypatch.setattr(config, "service_label_rules", ruleset)
+
+ config.get_config_cache().initialize()
+
+ assert automation.execute(["service", "test-host", "CPU
load"]) == {
+ "labels": {
+ "label1": "val1",
+ "label2": "val2"
+ }
+ }
+
+
+def test_analyse_host(monkeypatch):
+ automation = automations.AutomationAnalyseHost()
+
+ monkeypatch.setattr(config, "all_hosts", ["test-host"])
+ monkeypatch.setattr(config, "host_paths", {"test-host":
"/"})
+ monkeypatch.setattr(config, "host_labels", {
+ "test-host": {
+ "explicit": "ding",
+ },
+ })
+ config.get_config_cache().initialize()
+
+ assert automation.execute(["test-host"]) == {"labels":
{"explicit": "ding"}}