Module: check_mk
Branch: master
Commit: 50998360c19e13932bf29a28c60bba02e56aaf84
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=50998360c19e13…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Apr 24 09:58:24 2019 +0200
Move config.inv_export_hooks processing to HostConfig
CMK-2030
Change-Id: I0d85ec4e06833db4637b8cc97c8d7761409397cc
---
cmk_base/config.py | 10 ++++++++++
cmk_base/inventory.py | 13 ++++---------
tests/unit/cmk_base/test_config.py | 26 ++++++++++++++++++++++++++
3 files changed, 40 insertions(+), 9 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index e844725..1c91ff7 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -2601,6 +2601,16 @@ class HostConfig(object):
inv_parameters.get(section_name,
[]))
@property
+ def inventory_export_hooks(self):
+ # type: () -> List[Tuple[str, Dict]]
+ hooks = [] # type: List[Tuple[str, Dict]]
+ for hookname, ruleset in sorted(inv_exports.items(), key=lambda x: x[0]):
+ entries = self._config_cache.host_extra_conf(self.hostname, ruleset)
+ if entries:
+ hooks.append((hookname, entries[0]))
+ return hooks
+
+ @property
def hostgroups(self):
# type: () -> List[str]
"""Returns the list of hostgroups of this host
diff --git a/cmk_base/inventory.py b/cmk_base/inventory.py
index c89316f..40442f1 100644
--- a/cmk_base/inventory.py
+++ b/cmk_base/inventory.py
@@ -228,7 +228,7 @@ def _do_inv_for(sources, multi_host_sections, host_config, ipaddress,
do_status_
inventory_tree.normalize_nodes()
old_timestamp = _save_inventory_tree(hostname, inventory_tree)
- _run_inventory_export_hooks(hostname, inventory_tree)
+ _run_inventory_export_hooks(host_config, inventory_tree)
success_msg = [
"Found %s%s%d%s inventory entries" % (tty.bold, tty.yellow,
inventory_tree.count_entries(),
@@ -408,14 +408,9 @@ def _save_status_data_tree(hostname, status_data_tree):
status_data_tree.save_to(cmk.utils.paths.status_data_dir, hostname)
-def _run_inventory_export_hooks(hostname, inventory_tree):
+def _run_inventory_export_hooks(host_config, inventory_tree):
import cmk_base.inventory_plugins as inventory_plugins
- hooks = []
- config_cache = config.get_config_cache()
- for hookname, ruleset in config.inv_exports.items():
- entries = config_cache.host_extra_conf(hostname, ruleset)
- if entries:
- hooks.append((hookname, entries[0]))
+ hooks = host_config.inventory_export_hooks
if not hooks:
return
@@ -426,7 +421,7 @@ def _run_inventory_export_hooks(hostname, inventory_tree):
"Execute export hook: %s%s%s%s" % (tty.blue, tty.bold, hookname,
tty.normal))
try:
func = inventory_plugins.inv_export[hookname]["export_function"]
- func(hostname, params, inventory_tree.get_raw_tree())
+ func(host_config.hostname, params, inventory_tree.get_raw_tree())
except Exception as e:
if cmk.utils.debug.enabled():
raise
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index e7a0775..66b5d49 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -518,6 +518,32 @@ def test_host_config_inventory_parameters(monkeypatch, hostname,
result):
@pytest.mark.parametrize("hostname,result", [
+ ("testhost1", []),
+ ("testhost2", [
+ ("abc", {
+ "param1": 1
+ }),
+ ("xyz", {
+ "param2": 1
+ }),
+ ]),
+])
+def test_host_config_inventory_export_hooks(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_option(
+ "inv_exports", {
+ "abc": [({
+ "param1": 1
+ }, [], ["testhost2"], {}),],
+ "xyz": [({
+ "param2": 1
+ }, [], ["testhost2"], {}),],
+ })
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).inventory_export_hooks == result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
("testhost1", ["check_mk"]),
("testhost2", ["dingdong"]),
])