Module: check_mk
Branch: master
Commit: f157de5ee31779e83d6ad3b41d37f6bd86584895
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f157de5ee31779…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Mar 26 17:11:42 2019 +0100
Cleanup some ruleset processing
The analyse services automation call uses some hand crafted
ruleset processing code which can be cleaned up to use generic code
This commit separates ruleset logic from value handling and adds
a test for the extracted functionality. The next step will be to
switch this ruleset matching to generic code.
Change-Id: Icae3912f0dbd2c6bb8391f802abcc2d86bb7ba25
---
cmk_base/automations/check_mk.py | 63 +++++++++++++++++++--------------
tests/unit/cmk_base/test_automations.py | 30 ++++++++++++++++
2 files changed, 66 insertions(+), 27 deletions(-)
diff --git a/cmk_base/automations/check_mk.py b/cmk_base/automations/check_mk.py
index ae5a316..7ab20a4 100644
--- a/cmk_base/automations/check_mk.py
+++ b/cmk_base/automations/check_mk.py
@@ -591,36 +591,24 @@ class AutomationAnalyseServices(Automation):
table = check_table.get_check_table(hostname, remove_duplicates=True)
# 1. Manual checks
- for nr, (checkgroup, entries) in enumerate(config.static_checks.items()):
- for entry in entries:
- entry, rule_options = config.get_rule_options(entry)
- if rule_options.get("disabled"):
- continue
-
+ for checkgroup_name in config.static_checks:
+ for value in self.static_check_rules_of(checkgroup_name, hostname):
# Parameters are optional
- if len(entry[0]) == 2:
- checktype, item = entry[0]
+ if len(value) == 2:
+ checktype, item = value
params = None
else:
- checktype, item, params = entry[0]
- if len(entry) == 3:
- taglist, hostlist = entry[1:3]
- else:
- hostlist = entry[1]
- taglist = []
-
- if config.hosttags_match_taglist(config_cache.tags_of_host(hostname),
taglist) and \
- config.in_extraconf_hostlist(hostlist, hostname):
- descr = config.service_description(hostname, checktype, item)
- if descr == servicedesc:
- return {
- "origin": "static",
- "checkgroup": checkgroup,
- "checktype": checktype,
- "item": item,
- "rule_nr": nr,
- "parameters": params,
- }
+ 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,
+ }
# TODO: There is a lot of duplicated logic with discovery.py/check_table.py.
Clean this
# whole function up.
@@ -707,6 +695,27 @@ class AutomationAnalyseServices(Automation):
return {} # not found
+ def static_check_rules_of(self, checkgroup_name, hostname):
+ config_cache = config.get_config_cache()
+
+ rules = []
+ for entry in config.static_checks.get(checkgroup_name, []):
+ entry, rule_options = config.get_rule_options(entry)
+ if rule_options.get("disabled"):
+ continue
+
+ if len(entry) == 3:
+ taglist, hostlist = entry[1:3]
+ else:
+ hostlist = entry[1]
+ taglist = []
+
+ if config.hosttags_match_taglist(config_cache.tags_of_host(hostname),
taglist) and \
+ config.in_extraconf_hostlist(hostlist, hostname):
+ rules.append(entry[0])
+
+ return rules
+
automations.register(AutomationAnalyseServices())
diff --git a/tests/unit/cmk_base/test_automations.py
b/tests/unit/cmk_base/test_automations.py
new file mode 100644
index 0000000..1a6ca0f
--- /dev/null
+++ b/tests/unit/cmk_base/test_automations.py
@@ -0,0 +1,30 @@
+import cmk_base.automations.check_mk as automations
+import cmk_base.config as config
+
+
+def test_static_check_rules_of_host(monkeypatch):
+ as_automation = automations.AutomationAnalyseServices()
+ assert as_automation.static_check_rules_of("checkgroup_ding",
"test-host") == []
+
+ monkeypatch.setattr(config, "all_hosts", ["test-host"])
+ monkeypatch.setattr(config, "host_paths", {"test-host":
"/"})
+ monkeypatch.setattr(
+ config, "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, {}),
+ ],
+ })
+ config.get_config_cache().initialize()
+
+ assert as_automation.static_check_rules_of("checkgroup_ding",
"test-host") == [
+ ('ding-check', 'item'),
+ ('dong-check', 'item2', {
+ 'param1': 1
+ }),
+ ]