Module: check_mk
Branch: master
Commit: 88e74f5c6c4cfcff9485853792f2a6325c7d9b35
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=88e74f5c6c4cfc…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Apr 25 08:14:46 2019 +0200
Move config.periodic_discovery processing to HostConfig
CMK-2030
Change-Id: I16013dfe014483cb510a5e62bd33824d1b10d554
---
cmk_base/config.py | 23 +++++++++++++++++++++++
cmk_base/core_nagios.py | 5 +----
cmk_base/discovery.py | 29 ++---------------------------
tests/unit/cmk_base/test_config.py | 28 ++++++++++++++++++++++++++++
4 files changed, 54 insertions(+), 31 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 80469cc..abd9951 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -2602,6 +2602,29 @@ class HostConfig(object):
attrs[key] = values[0]
return attrs
+ @property
+ def discovery_check_parameters(self):
+ # type: () -> Dict
+ """Compute the parameters for the discovery check for a host
+
+ Note: if the discovery check is disabled for that host, default parameters
+ will be returned. A "check_interval" of None means the check should not
be added.
+ """
+ entries = self._config_cache.host_extra_conf(self.hostname, periodic_discovery)
+ if not entries:
+ return self._default_discovery_check_parameters()
+
+ return entries[0]
+
+ def _default_discovery_check_parameters(self):
+ """Support legacy single value global configurations. Otherwise
return the defaults"""
+ return {
+ "check_interval": inventory_check_interval,
+ "severity_unmonitored": inventory_check_severity,
+ "severity_vanished": 0,
+ "inventory_check_do_scan": inventory_check_do_scan,
+ }
+
def inventory_parameters(self, section_name):
# type: (str) -> Dict
return self._config_cache.host_extra_conf_merged(self.hostname,
diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py
index feb2442..7676802 100644
--- a/cmk_base/core_nagios.py
+++ b/cmk_base/core_nagios.py
@@ -503,11 +503,8 @@ def _create_nagios_servicedefs(cfg, config_cache, hostname,
host_attrs):
if 'cmk-inventory' in config.use_new_descriptions_for:
service_discovery_name = 'Check_MK Discovery'
- import cmk_base.discovery as discovery
- params = discovery.discovery_check_parameters(hostname) or \
- discovery.default_discovery_check_parameters()
-
# Inventory checks - if user has configured them.
+ params = host_config.discovery_check_parameters
if params["check_interval"] \
and not config.service_ignored(hostname, None, service_discovery_name) \
and not host_config.is_ping_host:
diff --git a/cmk_base/discovery.py b/cmk_base/discovery.py
index 460abc6..f915759 100644
--- a/cmk_base/discovery.py
+++ b/cmk_base/discovery.py
@@ -326,8 +326,7 @@ def check_discovery(hostname, ipaddress):
config_cache = config.get_config_cache()
host_config = config_cache.get_host_config(hostname)
- params = discovery_check_parameters(hostname) or \
- default_discovery_check_parameters()
+ params = host_config.discovery_check_parameters
status = 0
infotexts = []
@@ -433,30 +432,6 @@ def check_discovery(hostname, ipaddress):
return status, infotexts, long_infotexts, perfdata
-# Compute the parameters for the discovery check for a host. Note:
-# if the discovery check is disabled for that host, default parameters
-# will be returned.
-def discovery_check_parameters(hostname):
- entries = config.get_config_cache().host_extra_conf(hostname,
config.periodic_discovery)
- if entries:
- return entries[0]
-
- elif config.inventory_check_interval:
- # Support legacy global configurations
- return default_discovery_check_parameters()
-
- return None
-
-
-def default_discovery_check_parameters():
- return {
- "check_interval": config.inventory_check_interval,
- "severity_unmonitored": config.inventory_check_severity,
- "severity_vanished": 0,
- "inventory_check_do_scan": config.inventory_check_do_scan,
- }
-
-
def _set_rediscovery_flag(hostname):
def touch(filename):
if not os.path.exists(filename):
@@ -582,7 +557,7 @@ def _discover_marked_host(config_cache, host_config, now_ts,
oldest_queued):
console.verbose("%s%s%s:\n" % (tty.bold, hostname, tty.normal))
host_flag_path = os.path.join(_get_autodiscovery_dir(), hostname)
- params = discovery_check_parameters(hostname) or
default_discovery_check_parameters()
+ params = host_config.discovery_check_parameters
params_rediscovery = params.get("inventory_rediscovery", {})
if "service_blacklist" in params_rediscovery or
"service_whitelist" in params_rediscovery:
# whitelist. if none is specified, this matches everything
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 210087a..8044206 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -518,6 +518,34 @@ def test_host_config_inventory_parameters(monkeypatch, hostname,
result):
@pytest.mark.parametrize("hostname,result", [
+ ("testhost1", {
+ 'check_interval': None,
+ 'inventory_check_do_scan': True,
+ 'severity_unmonitored': 1,
+ 'severity_vanished': 0,
+ }),
+ ("testhost2", {
+ "check_interval": 1,
+ }),
+])
+def test_host_config_discovery_check_parameters(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_option(
+ "periodic_discovery",
+ [
+ ({
+ "check_interval": 1,
+ }, [], ["testhost2"], {}),
+ ({
+ "check_interval": 2,
+ }, [], ["testhost2"], {}),
+ ],
+ )
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).discovery_check_parameters == result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
("testhost1", []),
("testhost2", [
("abc", {