Module: check_mk
Branch: master
Commit: 6123e226bbdce7619da8016017989108d58ba781
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=6123e226bbdce7…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Mar 27 08:39:12 2019 +0100
Refactor hand crafted rule matching out of several checks
These checks discover services based on service discovery rulesets, which
are regular host rulesets and don't need specific matching code in the
checks. They can simply use the generic host_extra_conf() function
instead of hand individual matching.
Change-Id: Ia5373ecfc877ed67b9e39e3b0a581b45683e1892
---
checks/ps.include | 19 ++++---------------
checks/sap | 11 ++---------
checks/services | 18 +++---------------
tests/unit/checks/conftest.py | 5 +++++
4 files changed, 14 insertions(+), 39 deletions(-)
diff --git a/checks/ps.include b/checks/ps.include
index b023c77..4b27153 100644
--- a/checks/ps.include
+++ b/checks/ps.include
@@ -89,24 +89,13 @@ def ps_data_configured_inventory_rules(invdata):
def ps_wato_configured_inventory_rules(invrules):
inventory_specs = []
- for rule in invrules:
- taglist, hostlist = rule[1:3]
- if len(rule) >= 4:
- options = rule[3]
- if options.get("disabled"):
- continue
-
- # Filter out entries with do not match our current host
- if not hosttags_match_taglist(tags_of_host(host_name()), taglist) \
- or not in_extraconf_hostlist(hostlist, host_name()):
- continue # TODO Test this jump
-
- v = rule[0]
- default_params = v.get('default_params', v)
+ for value in host_extra_conf(host_name(), invrules):
+ default_params = value.get('default_params', value)
if "cpu_rescale_max" not in default_params:
default_params["cpu_rescale_max"] = None
- inventory_specs.append((v['descr'], v.get('match'),
v.get('user'), default_params))
+ inventory_specs.append((value['descr'], value.get('match'),
value.get('user'),
+ default_params))
return inventory_specs
diff --git a/checks/sap b/checks/sap
index e12f7de..b845912 100644
--- a/checks/sap
+++ b/checks/sap
@@ -118,15 +118,8 @@ def inventory_sap_value(info):
inv = []
patterns = []
- for rule in inventory_sap_values:
- taglist, hostlist = rule[1:3]
- # Filter out entries with do not match our current host
- if not hosttags_match_taglist(tags_of_host(host_name()), taglist) \
- or not in_extraconf_hostlist(hostlist, host_name()):
- continue
-
- v = rule[0]
- patterns.append((v['match'], v.get('limit_item_levels')))
+ for value in host_extra_conf(host_name(), inventory_sap_values):
+ patterns.append((value['match'],
value.get('limit_item_levels')))
for line in info:
for pattern, limit_item_levels in patterns:
diff --git a/checks/services b/checks/services
index 193ed60..26bc7a1 100644
--- a/checks/services
+++ b/checks/services
@@ -114,22 +114,10 @@ def inventory_windows_services(info):
matching += add_matching_services(name, description, state,
start_type, svc)
return matching
- # Filter WATO compatible rules by tags/hostlist
+ # Extract the WATO compatible rules for the current host
rules = []
- for rule in inventory_services_rules:
- if len(rule) >= 4:
- options = rule[3]
- if options.get("disabled"):
- continue
-
- # 1. Get all rules matching the current host
- taglist, hostlist = rule[1:3]
- if not hosttags_match_taglist(tags_of_host(host_name()), taglist) \
- or not in_extraconf_hostlist(hostlist, host_name()):
- continue
-
- # 2. Now extract the list of service regexes
- value = rule[0]
+ for value in host_extra_conf(host_name(), inventory_services_rules):
+ # Now extract the list of service regexes
svcs = value.get('services', [])
state = value.get('state', None)
start_mode = value.get('start_mode', None)
diff --git a/tests/unit/checks/conftest.py b/tests/unit/checks/conftest.py
index 73b8e6c..b559259 100644
--- a/tests/unit/checks/conftest.py
+++ b/tests/unit/checks/conftest.py
@@ -14,5 +14,10 @@ def check_manager():
def clear_config_caches(monkeypatch):
import cmk_base
import cmk_base.caching
+ import cmk_base.config
monkeypatch.setattr(cmk_base, "config_cache",
cmk_base.caching.CacheManager())
monkeypatch.setattr(cmk_base, "runtime_cache",
cmk_base.caching.CacheManager())
+
+ monkeypatch.setattr(cmk_base.config, "all_hosts",
["non-existent-testhost"])
+ monkeypatch.setattr(cmk_base.config, "host_paths",
{"non-existent-testhost": "/"})
+ cmk_base.config.get_config_cache().initialize()