Module: check_mk
Branch: master
Commit: 6d5eb7842a192680aea8bb1f37f5888439d5909e
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=6d5eb7842a1926…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Apr 25 10:03:05 2019 +0200
Move config.static_checks processing to HostConfig
CMK-2030
Change-Id: Icc4d96c1787552132f27c6397f67d51374b5bf35
---
cmk_base/automations/check_mk.py | 32 +++++++++-------------------
cmk_base/config.py | 18 ++++++++++++++++
tests/unit/cmk_base/test_config.py | 28 ++++++++++++++++++++++++
tests/unit/cmk_base/test_unit_automations.py | 28 ------------------------
4 files changed, 56 insertions(+), 50 deletions(-)
diff --git a/cmk_base/automations/check_mk.py b/cmk_base/automations/check_mk.py
index e76b1c3..ea24628 100644
--- a/cmk_base/automations/check_mk.py
+++ b/cmk_base/automations/check_mk.py
@@ -613,24 +613,16 @@ class AutomationAnalyseServices(Automation):
table = check_table.get_check_table(hostname, remove_duplicates=True)
# 1. Manual checks
- for checkgroup_name in config.static_checks:
- for value in self.static_check_rules_of(checkgroup_name, hostname):
- # Parameters are optional
- if len(value) == 2:
- checktype, item = value
- params = None
- else:
- checktype, item, params = value
-
- descr = config.service_description(hostname, checktype, item)
- if descr == servicedesc:
- return {
- "origin": "static",
- "checkgroup": checkgroup_name,
- "checktype": checktype,
- "item": item,
- "parameters": params,
- }
+ for checkgroup_name, checktype, item, params in host_config.static_checks:
+ descr = config.service_description(hostname, checktype, item)
+ if descr == servicedesc:
+ return {
+ "origin": "static",
+ "checkgroup": checkgroup_name,
+ "checktype": checktype,
+ "item": item,
+ "parameters": params,
+ }
# TODO: There is a lot of duplicated logic with discovery.py/check_table.py.
Clean this
# whole function up.
@@ -706,10 +698,6 @@ class AutomationAnalyseServices(Automation):
return {} # not found
- def static_check_rules_of(self, checkgroup_name, hostname):
- config_cache = config.get_config_cache()
- return config_cache.host_extra_conf(hostname,
config.static_checks.get(checkgroup_name, []))
-
automations.register(AutomationAnalyseServices())
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 4b40161..177701a 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -2680,6 +2680,24 @@ class HostConfig(object):
return self._config_cache.host_extra_conf(self.hostname, custom_checks)
@property
+ def static_checks(self):
+ # type: () -> List[Tuple[str, str, str, Any]]
+ """Returns a table of all "manual checks" configured for
this host"""
+ matched = []
+ for checkgroup_name in static_checks:
+ for entry in self._config_cache.host_extra_conf(self.hostname,
+
static_checks.get(checkgroup_name, [])):
+ if len(entry) == 2:
+ checktype, item = entry
+ params = None
+ else:
+ checktype, item, params = entry
+
+ matched.append((checkgroup_name, checktype, item, params))
+
+ return matched
+
+ @property
def hostgroups(self):
# type: () -> List[str]
"""Returns the list of hostgroups of this host
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 8044206..d8df3f2 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -657,6 +657,34 @@ def test_host_config_custom_checks(monkeypatch, hostname, result):
@pytest.mark.parametrize("hostname,result", [
+ ("testhost1", []),
+ ("testhost2", [
+ ('checkgroup', 'checktype1', 'item1', {
+ 'param1': 1
+ }),
+ ('checkgroup', 'checktype2', 'item2', {
+ 'param2': 2
+ }),
+ ]),
+])
+def test_host_config_static_checks(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_option(
+ "static_checks", {
+ "checkgroup": [
+ (("checktype1", "item1", {
+ "param1": 1
+ }), [], ["testhost2"], {}),
+ (("checktype2", "item2", {
+ "param2": 2
+ }), [], ["testhost2"], {}),
+ ],
+ })
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).static_checks == result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
("testhost1", ["check_mk"]),
("testhost2", ["dingdong"]),
])
diff --git a/tests/unit/cmk_base/test_unit_automations.py
b/tests/unit/cmk_base/test_unit_automations.py
index a959810..1c71f93 100644
--- a/tests/unit/cmk_base/test_unit_automations.py
+++ b/tests/unit/cmk_base/test_unit_automations.py
@@ -52,34 +52,6 @@ def test_registered_automations(site):
assert sorted(needed_automations) == sorted(registered_automations)
-def test_static_check_rules_of_host(monkeypatch):
- as_automation = automations.AutomationAnalyseServices()
- assert as_automation.static_check_rules_of("checkgroup_ding",
"test-host") == []
-
- ts = Scenario()
- ts.add_host("test-host", [])
- ts.set_option(
- "static_checks", {
- "checkgroup_ding": [
- (("ding-check", "item"), [], config.ALL_HOSTS, {}),
- (("ding-check", "item2"), [], config.ALL_HOSTS, {
- "disabled": True
- }),
- (("dong-check", "item2", {
- "param1": 1
- }), [], config.ALL_HOSTS, {}),
- ],
- })
- ts.apply(monkeypatch)
-
- assert as_automation.static_check_rules_of("checkgroup_ding",
"test-host") == [
- ('ding-check', 'item'),
- ('dong-check', 'item2', {
- 'param1': 1
- }),
- ]
-
-
def test_get_labels_of_host(monkeypatch):
automation = automations.AutomationGetLabelsOf()