Module: check_mk
Branch: master
Commit: 52b4c1e29734760faf7735ed662bf87b6b9053b2
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=52b4c1e2973476…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Apr 22 18:34:50 2019 +0200
Cleanup config.hostgroups_of
Centralized fallback to the default hostgroup "check_mk" on the
way and added tests for HostConfig.hostgroups.
CMK-2030
Change-Id: I2420d215e95f685a13171fa7a2c5674aec54a93e
---
cmk_base/config.py | 16 ++++++++++++----
cmk_base/core_nagios.py | 15 ++++-----------
cmk_base/dump_host.py | 2 +-
cmk_base/modes/check_mk.py | 3 ++-
tests/unit/cmk_base/test_config.py | 13 +++++++++++++
5 files changed, 32 insertions(+), 17 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index fdb5c31..ce049a9 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -791,10 +791,6 @@ def get_explicit_service_custom_variables(hostname, description):
#
-def hostgroups_of(hostname):
- return get_config_cache().host_extra_conf(hostname, host_groups)
-
-
def contactgroups_of(hostname):
cgrs = []
@@ -2648,6 +2644,18 @@ class HostConfig(object):
return spec # return the whole spec in case of an "at least version"
config
+ @property
+ def hostgroups(self):
+ # type: () -> List[str]
+ """Returns the list of hostgroups of this host
+
+ If the host has no hostgroups it will be added to the default hostgroup
+ (Nagios requires each host to be member of at least on group."""
+ groups = self._config_cache.host_extra_conf(self.hostname, host_groups)
+ if not groups:
+ return [default_host_group]
+ return groups
+
#.
# .--Configuration Cache-------------------------------------------------.
diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py
index adfc8a9..b2ab1b4 100644
--- a/cmk_base/core_nagios.py
+++ b/cmk_base/core_nagios.py
@@ -209,17 +209,10 @@ def _create_nagios_host_spec(cfg, config_cache, hostname, attrs):
if command:
host_spec["check_command"] = command
- # Host groups: If the host has no hostgroups it gets the default
- # hostgroup (Nagios requires each host to be member of at least on
- # group.
- hgs = config.hostgroups_of(hostname)
- hostgroups = ",".join(hgs)
- if len(hgs) == 0:
- hostgroups = config.default_host_group
- cfg.hostgroups_to_define.add(config.default_host_group)
- elif config.define_hostgroups:
- cfg.hostgroups_to_define.update(hgs)
- host_spec["hostgroups"] = hostgroups
+ hostgroups = host_config.hostgroups
+ if config.define_hostgroups:
+ cfg.hostgroups_to_define.update(hostgroups)
+ host_spec["hostgroups"] = ",".join(hostgroups)
# Contact groups
cgrs = config.contactgroups_of(hostname)
diff --git a/cmk_base/dump_host.py b/cmk_base/dump_host.py
index 4cb81f3..1c4980e 100644
--- a/cmk_base/dump_host.py
+++ b/cmk_base/dump_host.py
@@ -90,7 +90,7 @@ def dump_host(hostname):
console.output(tty.yellow + "Parents: " + tty.normal +
", ".join(parents_list) + "\n")
console.output(tty.yellow + "Host groups: " + tty.normal +
- cmk_base.utils.make_utf8(",
".join(config.hostgroups_of(hostname))) + "\n")
+ cmk_base.utils.make_utf8(", ".join(host_config.hostgroups))
+ "\n")
console.output(tty.yellow + "Contact groups: " + tty.normal +
cmk_base.utils.make_utf8(",
".join(config.contactgroups_of(hostname))) + "\n")
diff --git a/cmk_base/modes/check_mk.py b/cmk_base/modes/check_mk.py
index 0c521d4..8c0052e 100644
--- a/cmk_base/modes/check_mk.py
+++ b/cmk_base/modes/check_mk.py
@@ -233,7 +233,8 @@ def _list_all_hosts(hostgroups, options):
hostlist = []
for hn in hostnames:
- for hg in config.hostgroups_of(hn):
+ host_config = config_cache.get_host_config(hn)
+ for hg in host_config.hostgroups:
if hg in hostgroups:
hostlist.append(hn)
break
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 0047cb4..472b03b 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -325,6 +325,19 @@ def test_host_config_agent_target_version(monkeypatch, hostname,
result):
assert config_cache.get_host_config(hostname).agent_target_version == result
+(a)pytest.mark.parametrize("hostname,result"sult", [
+ ("testhost1", ["check_mk"]),
+ ("testhost2", ["dingdong"]),
+])
+def test_host_config_hostgroups(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset("host_groups", [
+ ("dingdong", [], ["testhost2"], {}),
+ ])
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).hostgroups == result
+
+
def test_http_proxies():
assert config.http_proxies == {}