Module: check_mk
Branch: master
Commit: b05b41626ae07bdd74c3cca87242eb7e091ade3d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=b05b41626ae07b…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu May 2 20:55:49 2019 +0200
Move last host_extra_conf to cmk_base.config
* The agent bakery config computation is a bit special. The rule
matching logic has now been moved to cmk_base.config.
* Added tests for it
* The value merging logic remains in cmk_base.cee.agent_bakery for
the moment because it is based on the bakery plugins which should
be cleaned up in the near future.
CMK-2030
Change-Id: Icf06e4f2e50f403b8253e380bdfdcdb66a4a014b
---
cmk_base/config.py | 66 ++++++++++++++++++++++++++----------
tests/unit/cmk_base/test_rulesets.py | 9 +++--
2 files changed, 55 insertions(+), 20 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 0fa8ef9..2f1344e 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -1204,22 +1204,6 @@ def in_extraconf_hostlist(hostlist, hostname):
return False
-def parse_host_rule(rule):
- rule, rule_options = get_rule_options(rule)
-
- num_elements = len(rule)
- if num_elements == 2:
- item, hostlist = rule
- tags = []
- elif num_elements == 3:
- item, tags, hostlist = rule
- else:
- raise MKGeneralException("Invalid entry '%r' in host configuration
list: must "
- "have 2 or 3 entries" % (rule,))
-
- return item, tags, hostlist, rule_options
-
-
def get_rule_options(entry):
"""Get the options from a rule.
@@ -3376,7 +3360,7 @@ class ConfigCache(object):
console.warning('deprecated entry [ "" ] in host configuration
list')
for rule in ruleset:
- item, tags, hostlist, rule_options = parse_host_rule(rule)
+ item, tags, hostlist, rule_options = self._parse_host_rule(rule)
if rule_options.get("disabled"):
continue
@@ -3386,6 +3370,21 @@ class ConfigCache(object):
return new_rules
+ def _parse_host_rule(self, rule):
+ rule, rule_options = get_rule_options(rule)
+
+ num_elements = len(rule)
+ if num_elements == 2:
+ item, hostlist = rule
+ tags = []
+ elif num_elements == 3:
+ item, tags, hostlist = rule
+ else:
+ raise MKGeneralException("Invalid entry '%r' in host
configuration list: must "
+ "have 2 or 3 entries" % (rule,))
+
+ return item, tags, hostlist, rule_options
+
def service_extra_conf(self, hostname, service, ruleset):
"""Compute outcome of a service rule set that has an
item."""
# When the requested host is part of the local sites configuration,
@@ -3746,6 +3745,39 @@ class CEEConfigCache(ConfigCache):
return None
return entries[0]
+ # TODO: Cleanup the GENERIC_AGENT duplication with
cmk_base.cee.agent_bakyery.GENERIC_AGENT
+ def matched_agent_config_entries(self, hostname):
+ # type: (Union[bool, str]) -> Dict[str, Any]
+ GENERIC_AGENT = True
+ matched = {}
+ for varname, ruleset in agent_config.items() + [("agent_port",
agent_ports),
+ ("agent_encryption",
agent_encryption)]:
+ if hostname is GENERIC_AGENT:
+ matched[varname] = self._generic_host_extra_conf(ruleset)
+ else:
+ matched[varname] = self.host_extra_conf(hostname, ruleset)
+
+ return matched
+
+ def _generic_host_extra_conf(self, ruleset):
+ """Compute ruleset for "generic" host
+
+ This fictious host has no name and no tags. It matches all rules that
+ do not require specific hosts or tags. But it matches rules that e.g.
+ except specific hosts or tags (is not, has not set)
+ """
+ entries = []
+
+ for rule in ruleset:
+ item, tags, hostlist = self._parse_host_rule(rule)[:-1]
+ if tags and not hosttags_match_taglist([], tags):
+ continue
+ if not in_extraconf_hostlist(hostlist, ""):
+ continue
+
+ entries.append(item)
+ return entries
+
# TODO: Find a clean way to move this to cmk_base.cee. This will be possible once the
# configuration settings are not held in cmk_base.config namespace anymore.
diff --git a/tests/unit/cmk_base/test_rulesets.py b/tests/unit/cmk_base/test_rulesets.py
index b3ef5e5..7654c0a 100644
--- a/tests/unit/cmk_base/test_rulesets.py
+++ b/tests/unit/cmk_base/test_rulesets.py
@@ -210,22 +210,25 @@ def test_in_extraconf_hostlist():
def test_parse_host_rule():
+ config_cache = config.get_config_cache()
options = {'description': u'Put all hosts into the contact group
"all"'}
entry = ('all', [], config.ALL_HOSTS, options)
- assert config.parse_host_rule(entry) == ('all', [], config.ALL_HOSTS,
options)
+ assert config_cache._parse_host_rule(entry) == ('all', [], config.ALL_HOSTS,
options)
def test_parse_host_rule_without_tags():
+ config_cache = config.get_config_cache()
options = {'description': u'Put all hosts into the contact group
"all"'}
entry = ('all', config.ALL_HOSTS, options)
- assert config.parse_host_rule(entry) == ('all', [], config.ALL_HOSTS,
options)
+ assert config_cache._parse_host_rule(entry) == ('all', [], config.ALL_HOSTS,
options)
def test_parse_host_rule_invalid_length():
+ config_cache = config.get_config_cache()
options = {'description': u'Put all hosts into the contact group
"all"'}
entry = (None, None, 'all', config.ALL_HOSTS, options)
with pytest.raises(MKGeneralException):
- assert config.parse_host_rule(entry)
+ assert config_cache._parse_host_rule(entry)
def test_get_rule_options_regular_rule():