Module: check_mk
Branch: master
Commit: 2a5d9baa1991701d1776267f555382db5728fdfd
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=2a5d9baa199170…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Apr 24 07:34:30 2019 +0200
Move config.special_agents processing to HostConfig
CMK-2030
Change-Id: Ia20e039b1cbf977e82af5c3e74e880cdf02a9cd1
---
cmk_base/config.py | 16 ++++++++++++++++
cmk_base/data_sources/__init__.py | 21 ++++++---------------
tests/unit/cmk_base/test_config.py | 26 ++++++++++++++++++++++++++
3 files changed, 48 insertions(+), 15 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 06cb8cd..7d0feb0 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -2523,6 +2523,22 @@ class HostConfig(object):
return programs[0]
@property
+ def special_agents(self):
+ # type: () -> List[Tuple[str, Dict]]
+ matched = [] # type: List[Tuple[str, Dict]]
+ # Previous to 1.5.0 it was not defined in which order the special agent
+ # rules overwrite each other. When multiple special agents were configured
+ # for a single host a "random" one was picked (depending on the
iteration
+ # over config.special_agents.
+ # We now sort the matching special agents by their name to at least get
+ # a deterministic order of the special agents.
+ for agentname, ruleset in sorted(special_agents.items()):
+ params = self._config_cache.host_extra_conf(self.hostname, ruleset)
+ if params:
+ matched.append((agentname, params[0]))
+ return matched
+
+ @property
def hostgroups(self):
# type: () -> List[str]
"""Returns the list of hostgroups of this host
diff --git a/cmk_base/data_sources/__init__.py b/cmk_base/data_sources/__init__.py
index 7aa1803..8e20f3f 100644
--- a/cmk_base/data_sources/__init__.py
+++ b/cmk_base/data_sources/__init__.py
@@ -41,6 +41,7 @@ import socket
import subprocess
import sys
import time
+from typing import List # pylint: disable=unused-import
import cmk.utils.paths
import cmk.utils.debug
@@ -186,21 +187,11 @@ class DataSources(object):
return TCPDataSource(self._hostname, self._ipaddress)
def _get_special_agent_data_sources(self):
- special_agents = []
-
- # Previous to 1.5.0 it was not defined in which order the special agent
- # rules overwrite eachother. When multiple special agents were configured
- # for a single host a "random" one was picked (depending on the
iteration
- # over config.special_agents.
- # We now sort the matching special agents by their name to at least get
- # a deterministic order of the special agents.
- for agentname, ruleset in sorted(config.special_agents.items()):
- params = self._config_cache.host_extra_conf(self._hostname, ruleset)
- if params:
- special_agents.append(
- SpecialAgentDataSource(self._hostname, self._ipaddress, agentname,
params[0]))
-
- return special_agents
+ # type: () -> List[SpecialAgentDataSource]
+ return [
+ SpecialAgentDataSource(self._hostname, self._ipaddress, agentname, params)
+ for agentname, params in self._host_config.special_agents
+ ]
def get_check_plugin_names(self):
"""Returns the list of check types the caller may execute on the
sections produced
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 5b76c12..c1f2b0e 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -356,6 +356,32 @@ def test_host_config_datasource_program(monkeypatch, hostname,
result):
@pytest.mark.parametrize("hostname,result", [
+ ("testhost1", []),
+ ("testhost2", [
+ ("abc", {
+ "param1": 1
+ }),
+ ("xyz", {
+ "param2": 1
+ }),
+ ]),
+])
+def test_host_config_special_agents(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_option(
+ "special_agents", {
+ "abc": [({
+ "param1": 1
+ }, [], ["testhost2"], {}),],
+ "xyz": [({
+ "param2": 1
+ }, [], ["testhost2"], {}),],
+ })
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).special_agents == result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
("testhost1", ["check_mk"]),
("testhost2", ["dingdong"]),
])