Module: check_mk
Branch: master
Commit: dafa2c64e1fae89f050ed01e9d75014bad649ae7
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=dafa2c64e1fae8…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Apr 22 18:45:10 2019 +0200
Cleanup config.contactgroups_of
CMK-2030
Change-Id: Ica112b6bb0b8af42627fdc71091e49e97165bf97
---
cmk_base/config.py | 48 +++++++++++++++++---------------------
cmk_base/core_nagios.py | 8 +++----
cmk_base/dump_host.py | 2 +-
tests/unit/cmk_base/test_config.py | 13 +++++++++++
4 files changed, 40 insertions(+), 31 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index ce049a9..99c57ad 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -787,32 +787,6 @@ def get_explicit_service_custom_variables(hostname, description):
#
-# Groups
-#
-
-
-def contactgroups_of(hostname):
- cgrs = []
-
- # host_contactgroups may take single values as well as
- # lists as item value. Of all list entries only the first
- # one is used. The single-contact-groups entries are all
- # recognized.
- first_list = True
- for entry in get_config_cache().host_extra_conf(hostname, host_contactgroups):
- if isinstance(entry, list) and first_list:
- cgrs += entry
- first_list = False
- else:
- cgrs.append(entry)
-
- if monitoring_core == "nagios" and enable_rulebased_notifications:
- cgrs.append("check-mk-notify")
-
- return list(set(cgrs))
-
-
-#
# Misc
#
@@ -2656,6 +2630,28 @@ class HostConfig(object):
return [default_host_group]
return groups
+ @property
+ def contactgroups(self):
+ # type: () -> List[str]
+ """Returns the list of contactgroups of this
host"""
+ cgrs = [] # type: List[str]
+
+ # host_contactgroups may take single values as well as lists as item
+ # value. Of all list entries only the first one is used. The
+ # single-contact-groups entries are all recognized.
+ first_list = True
+ for entry in self._config_cache.host_extra_conf(self.hostname,
host_contactgroups):
+ if isinstance(entry, list) and first_list:
+ cgrs += entry
+ first_list = False
+ else:
+ cgrs.append(entry)
+
+ if monitoring_core == "nagios" and enable_rulebased_notifications:
+ cgrs.append("check-mk-notify")
+
+ return list(set(cgrs))
+
#.
# .--Configuration Cache-------------------------------------------------.
diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py
index b2ab1b4..6130c95 100644
--- a/cmk_base/core_nagios.py
+++ b/cmk_base/core_nagios.py
@@ -215,10 +215,10 @@ def _create_nagios_host_spec(cfg, config_cache, hostname, attrs):
host_spec["hostgroups"] = ",".join(hostgroups)
# Contact groups
- cgrs = config.contactgroups_of(hostname)
- if len(cgrs) > 0:
- host_spec["contact_groups"] = ",".join(cgrs)
- cfg.contactgroups_to_define.update(cgrs)
+ contactgroups = host_config.contactgroups
+ if contactgroups:
+ host_spec["contact_groups"] = ",".join(contactgroups)
+ cfg.contactgroups_to_define.update(contactgroups)
if not host_config.is_cluster:
# Parents for non-clusters
diff --git a/cmk_base/dump_host.py b/cmk_base/dump_host.py
index 1c4980e..1df425e 100644
--- a/cmk_base/dump_host.py
+++ b/cmk_base/dump_host.py
@@ -92,7 +92,7 @@ def dump_host(hostname):
console.output(tty.yellow + "Host groups: " + tty.normal +
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")
+ cmk_base.utils.make_utf8(",
".join(host_config.contactgroups)) + "\n")
agenttypes = []
sources = data_sources.DataSources(hostname, ipaddress)
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 472b03b..1bdf6da 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -338,6 +338,19 @@ def test_host_config_hostgroups(monkeypatch, hostname, result):
assert config_cache.get_host_config(hostname).hostgroups == result
+(a)pytest.mark.parametrize("hostname,result"sult", [
+ ("testhost1", []),
+ ("testhost2", ["dingdong"]),
+])
+def test_host_config_contactgroups(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset("host_contactgroups", [
+ ("dingdong", [], ["testhost2"], {}),
+ ])
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).contactgroups == result
+
+
def test_http_proxies():
assert config.http_proxies == {}