Module: check_mk
Branch: master
Commit: d51550095ac3551fbae5d98a49317406f73deed0
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=d51550095ac355…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Apr 23 20:24:52 2019 +0200
Cleanup config.do_host_label_discovery
CMK-2030
Change-Id: I6e961b7756b3624195287eac3514f5411d5b473d
---
cmk_base/config.py | 33 +++++++++++++++++----------------
cmk_base/inventory.py | 14 ++++----------
tests/unit/cmk_base/test_config.py | 6 +++---
3 files changed, 24 insertions(+), 29 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 82abafb..0cb4879 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -2091,22 +2091,6 @@ def _get_checkgroup_parameters(config_cache, host, checktype,
item):
raise MKGeneralException(str(e) + " (on host %s, checktype %s)" %
(host, checktype))
-def do_host_label_discovery_for(hostname):
- rules = active_checks.get('cmk_inv')
- if rules is None:
- return True
-
- entries = get_config_cache().host_extra_conf(hostname, rules)
-
- if not entries:
- return True # No matching rule -> disable
-
- # Convert legacy rules to current dict format (just like the valuespec)
- params = {} if entries[0] is None else entries[0]
-
- return params.get("host_label_inventory", True)
-
-
def filter_by_management_board(hostname,
found_check_plugin_names,
for_mgmt_board,
@@ -2659,6 +2643,23 @@ class HostConfig(object):
return params.get('status_data_inventory', False)
+ @property
+ def do_host_label_discovery(self):
+ # type: () -> bool
+ rules = active_checks.get('cmk_inv')
+ if rules is None:
+ return True
+
+ entries = self._config_cache.host_extra_conf(self.hostname, rules)
+
+ if not entries:
+ return True # No matching rule -> disable
+
+ # Convert legacy rules to current dict format (just like the valuespec)
+ params = {} if entries[0] is None else entries[0]
+
+ return params.get("host_label_inventory", True)
+
#.
# .--Configuration Cache-------------------------------------------------.
diff --git a/cmk_base/inventory.py b/cmk_base/inventory.py
index 1e9afa5..b0fae6c 100644
--- a/cmk_base/inventory.py
+++ b/cmk_base/inventory.py
@@ -87,7 +87,6 @@ def do_inv(hostnames):
host_config=host_config,
ipaddress=ipaddress,
do_status_data_inv=host_config.do_status_data_inventory,
- do_host_label_discovery=config.do_host_label_discovery_for(hostname),
)
except Exception as e:
if cmk.utils.debug.enabled():
@@ -124,7 +123,6 @@ def do_inv_check(hostname, options):
host_config=host_config,
ipaddress=ipaddress,
do_status_data_inv=host_config.do_status_data_inventory,
- do_host_label_discovery=config.do_host_label_discovery_for(hostname),
)
if (inventory_tree.is_empty() and status_data_tree.is_empty() and
@@ -179,12 +177,10 @@ def do_inventory_actions_during_checking_for(sources,
multi_host_sections, host_
hostname = host_config.hostname
do_status_data_inventory = not host_config.is_cluster and
host_config.do_status_data_inventory
- do_host_label_discovery = config.do_host_label_discovery_for(hostname)
-
if not do_status_data_inventory:
_cleanup_status_data(hostname)
- if not do_status_data_inventory and not do_host_label_discovery:
+ if not do_status_data_inventory and not host_config.do_host_label_discovery:
return # nothing to do here
# This is called during checking, but the inventory plugins are not loaded yet
@@ -200,7 +196,6 @@ def do_inventory_actions_during_checking_for(sources,
multi_host_sections, host_
host_config=host_config,
ipaddress=ipaddress,
do_status_data_inv=do_status_data_inventory,
- do_host_label_discovery=do_host_label_discovery,
)
@@ -213,9 +208,8 @@ def _cleanup_status_data(hostname):
os.remove(filepath + ".gz")
-def _do_inv_for(sources, multi_host_sections, host_config, ipaddress,
do_status_data_inv,
- do_host_label_discovery):
- # type: (data_sources.DataSources, data_sources.MultiHostSections, config.HostConfig,
Optional[str], bool, bool) -> Tuple[Optional[float], StructuredDataTree,
StructuredDataTree, DiscoveredHostLabels]
+def _do_inv_for(sources, multi_host_sections, host_config, ipaddress,
do_status_data_inv):
+ # type: (data_sources.DataSources, data_sources.MultiHostSections, config.HostConfig,
Optional[str], bool) -> Tuple[Optional[float], StructuredDataTree, StructuredDataTree,
DiscoveredHostLabels]
hostname = host_config.hostname
_initialize_inventory_tree()
@@ -241,7 +235,7 @@ def _do_inv_for(sources, multi_host_sections, host_config, ipaddress,
do_status_
tty.normal)
]
- if do_host_label_discovery:
+ if host_config.do_host_label_discovery:
DiscoveredHostLabelsStore(hostname).save(discovered_host_labels.to_dict())
success_msg.append("and %s%s%d%s host labels" % (tty.bold, tty.yellow,
len(discovered_host_labels),
tty.normal))
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 832d679..f99fd00 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -757,11 +757,11 @@ def test_host_config_do_status_data_inventory(monkeypatch, result,
ruleset):
"host_label_inventory": False
}, [], config.ALL_HOSTS, {})]),
])
-def test_do_host_label_discovery_for(monkeypatch, result, ruleset):
+def test_host_config_do_host_label_discovery_for(monkeypatch, result, ruleset):
ts = Scenario().add_host("abc", [])
ts.set_option("active_checks", {
"cmk_inv": ruleset,
})
- ts.apply(monkeypatch)
+ config_cache = ts.apply(monkeypatch)
- assert config.do_host_label_discovery_for("abc") == result
+ assert config_cache.get_host_config("abc").do_host_label_discovery ==
result