Module: check_mk
Branch: master
Commit: 47c26b5b4f51ba93f3d10130f94bc5f4c62a5215
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=47c26b5b4f51ba…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Apr 25 07:43:32 2019 +0200
Move config.custom_checks processing to HostConfig
CMK-2030
Change-Id: Id3c8f1f49df555b276a5ad4a2330721a8bec071d
---
cmk_base/automations/check_mk.py | 52 +++++++++++++++-----------------------
cmk_base/config.py | 12 ++++++++-
cmk_base/core_nagios.py | 2 +-
cmk_base/discovery.py | 5 +---
tests/unit/cmk_base/test_config.py | 22 ++++++++++++++++
5 files changed, 56 insertions(+), 37 deletions(-)
diff --git a/cmk_base/automations/check_mk.py b/cmk_base/automations/check_mk.py
index 084288a..94e416a 100644
--- a/cmk_base/automations/check_mk.py
+++ b/cmk_base/automations/check_mk.py
@@ -682,25 +682,16 @@ class AutomationAnalyseServices(Automation):
}
# 3. Classical checks
- for nr, entry in enumerate(config.custom_checks):
- if len(entry) == 4:
- rule, tags, hosts, options = entry
- if options.get("disabled"):
- continue
- else:
- rule, tags, hosts = entry
-
- matching_hosts = config.all_matching_hosts(tags, hosts,
with_foreign_hosts=True)
- if hostname in matching_hosts:
- desc = rule["service_description"]
- if desc == servicedesc:
- result = {
- "origin": "classic",
- "rule_nr": nr,
- }
- if "command_line" in rule: # Only active checks have a
command line
- result["command_line"] =
rule["command_line"]
- return result
+ for nr, entry in enumerate(host_config.custom_checks):
+ desc = entry["service_description"]
+ if desc == servicedesc:
+ result = {
+ "origin": "classic",
+ "rule_nr": nr,
+ }
+ if "command_line" in entry: # Only active checks have a
command line
+ result["command_line"] = entry["command_line"]
+ return result
# 4. Active checks
for plugin_name, entries in host_config.active_checks:
@@ -1316,20 +1307,19 @@ class AutomationActiveCheck(Automation):
hostname, plugin, item = args
item = item.decode("utf-8")
- config_cache = config.get_config_cache()
- host_config = config_cache.get_host_config(hostname)
+ host_config = config.get_config_cache().get_host_config(hostname)
if plugin == "custom":
- custchecks = config_cache.host_extra_conf(hostname, config.custom_checks)
- for entry in custchecks:
- if entry["service_description"] == item:
- command_line = self._replace_core_macros(hostname, entry.get(
- "command_line", ""))
- if command_line:
- command_line = core_config.autodetect_plugin(command_line)
- return self._execute_check_plugin(command_line)
-
- return -1, "Passive check - cannot be executed"
+ for entry in host_config.custom_checks:
+ if entry["service_description"] != item:
+ continue
+
+ command_line = self._replace_core_macros(hostname,
entry.get("command_line", ""))
+ if command_line:
+ command_line = core_config.autodetect_plugin(command_line)
+ return self._execute_check_plugin(command_line)
+
+ return -1, "Passive check - cannot be executed"
else:
act_info = config.active_check_info[plugin]
# Set host name for host_name()-function (part of the Check API)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index e103ef2..4c225c8 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -2625,7 +2625,11 @@ class HostConfig(object):
@property
def active_checks(self):
# type: () -> List[Tuple[str, List[Any]]]
- """Returns the list of active checks configured for this
host"""
+ """Returns the list of active checks configured for this host
+
+ These are configured using the active check formalization of WATO
+ where the whole parameter set is configured using valuespecs.
+ """
configured_checks = [] # type: List[Tuple[str, List[Any]]]
for plugin_name, ruleset in sorted(active_checks.items(), key=lambda x: x[0]):
# Skip Check_MK HW/SW Inventory for all ping hosts, even when the
@@ -2642,6 +2646,12 @@ class HostConfig(object):
return configured_checks
@property
+ def custom_checks(self):
+ # type: () -> List[Dict]
+ """Return the free form configured custom checks without
formalization"""
+ return self._config_cache.host_extra_conf(self.hostname, custom_checks)
+
+ @property
def hostgroups(self):
# type: () -> List[str]
"""Returns the list of hostgroups of this host
diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py
index 9b3f5fd..feb2442 100644
--- a/cmk_base/core_nagios.py
+++ b/cmk_base/core_nagios.py
@@ -426,7 +426,7 @@ def _create_nagios_servicedefs(cfg, config_cache, hostname,
host_attrs):
outfile.write(get_dependencies(hostname,
description).encode("utf-8"))
# Legacy checks via custom_checks
- custchecks = config_cache.host_extra_conf(hostname, config.custom_checks)
+ custchecks = host_config.custom_checks
if custchecks:
outfile.write("\n\n# Custom checks\n")
for entry in custchecks:
diff --git a/cmk_base/discovery.py b/cmk_base/discovery.py
index e392eeb..460abc6 100644
--- a/cmk_base/discovery.py
+++ b/cmk_base/discovery.py
@@ -1020,11 +1020,8 @@ def _merge_manual_services(host_config, services, on_error):
for (check_plugin_name, item), (params, descr, _unused_deps) in
manual_items.items():
services[(check_plugin_name, item)] = ('manual', repr(params))
- config_cache = config.get_config_cache()
-
# Add custom checks -> "custom"
- custchecks = config_cache.host_extra_conf(hostname, config.custom_checks)
- for entry in custchecks:
+ for entry in host_config.custom_checks:
services[('custom', entry['service_description'])] =
('custom', 'None')
# Similar for 'active_checks', but here we have parameters
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 3819987..2485dc2 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -607,6 +607,28 @@ def test_host_config_active_checks(monkeypatch, hostname, result):
@pytest.mark.parametrize("hostname,result", [
+ ("testhost1", []),
+ ("testhost2", [{
+ "param1": 1
+ }, {
+ "param2": 2
+ }]),
+])
+def test_host_config_custom_checks(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset("custom_checks", [
+ ({
+ "param1": 1
+ }, [], ["testhost2"], {}),
+ ({
+ "param2": 2
+ }, [], ["testhost2"], {}),
+ ])
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).custom_checks == result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
("testhost1", ["check_mk"]),
("testhost2", ["dingdong"]),
])