Module: check_mk
Branch: master
Commit: e9fd427e531ccdd0af7adee251f77a113d204a0e
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e9fd427e531ccd…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Apr 22 18:30:28 2019 +0200
Cleanup agent related top level functions
CMK-2030
Change-Id: I32abec5279cacaff07aa56b9c34f921221b0141e
---
cmk_base/config.py | 94 +++++++++++++++++++-------------------
cmk_base/data_sources/tcp.py | 10 ++--
tests/unit/cmk_base/test_config.py | 62 +++++++++++++++++++++++++
3 files changed, 114 insertions(+), 52 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 2d08c49..fdb5c31 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -777,53 +777,6 @@ def management_credentials_of(hostname):
#
-# Agent communication
-#
-
-
-def agent_port_of(hostname):
- ports = get_config_cache().host_extra_conf(hostname, agent_ports)
- if len(ports) == 0:
- return agent_port
-
- return ports[0]
-
-
-def tcp_connect_timeout_of(hostname):
- timeouts = get_config_cache().host_extra_conf(hostname, tcp_connect_timeouts)
- if len(timeouts) == 0:
- return tcp_connect_timeout
-
- return timeouts[0]
-
-
-def agent_encryption_of(hostname):
- settings = get_config_cache().host_extra_conf(hostname, agent_encryption)
- if settings:
- return settings[0]
-
- return {'use_regular': 'disable', 'use_realtime':
'enforce'}
-
-
-def agent_target_version(hostname):
- agent_target_versions = get_config_cache().host_extra_conf(hostname,
-
check_mk_agent_target_versions)
- if agent_target_versions:
- spec = agent_target_versions[0]
- if spec == "ignore":
- return None
- elif spec == "site":
- return cmk.__version__
- elif isinstance(spec, str):
- # Compatibility to old value specification format (a single version string)
- return spec
- elif spec[0] == 'specific':
- return spec[1]
-
- return spec # return the whole spec in case of an "at least version"
config
-
-
-#
# Explicit custom variables
#
def get_explicit_service_custom_variables(hostname, description):
@@ -2648,6 +2601,53 @@ class HostConfig(object):
of this site"""
return self.hostname in self._config_cache.all_configured_clusters()
+ @property
+ def agent_port(self):
+ # type: () -> int
+ ports = self._config_cache.host_extra_conf(self.hostname, agent_ports)
+ if not ports:
+ return agent_port
+
+ return ports[0]
+
+ @property
+ def tcp_connect_timeout(self):
+ # type: () -> float
+ timeouts = self._config_cache.host_extra_conf(self.hostname,
tcp_connect_timeouts)
+ if not timeouts:
+ return tcp_connect_timeout
+
+ return timeouts[0]
+
+ @property
+ def agent_encryption(self):
+ # type: () -> Dict[str, str]
+ settings = self._config_cache.host_extra_conf(self.hostname, agent_encryption)
+ if not settings:
+ return {'use_regular': 'disable', 'use_realtime':
'enforce'}
+ return settings[0]
+
+ @property
+ def agent_target_version(self):
+ # type: () -> Union[None, str, Tuple[str, str]]
+ agent_target_versions = self._config_cache.host_extra_conf(self.hostname,
+
check_mk_agent_target_versions)
+ if not agent_target_versions:
+ return None
+
+ spec = agent_target_versions[0]
+ if spec == "ignore":
+ return None
+ elif spec == "site":
+ return cmk.__version__
+ elif isinstance(spec, str):
+ # Compatibility to old value specification format (a single version string)
+ return spec
+ elif spec[0] == 'specific':
+ return spec[1]
+
+ return spec # return the whole spec in case of an "at least version"
config
+
#.
# .--Configuration Cache-------------------------------------------------.
diff --git a/cmk_base/data_sources/tcp.py b/cmk_base/data_sources/tcp.py
index e64218d..676aa22 100644
--- a/cmk_base/data_sources/tcp.py
+++ b/cmk_base/data_sources/tcp.py
@@ -87,7 +87,7 @@ class TCPDataSource(CheckMKAgentDataSource):
if self._port is not None:
return self._port
- return config.agent_port_of(self._hostname)
+ return self._host_config.agent_port
def set_timeout(self, timeout):
self._timeout = timeout
@@ -96,7 +96,7 @@ class TCPDataSource(CheckMKAgentDataSource):
if self._timeout:
return self._timeout
- return config.tcp_connect_timeout_of(self._hostname)
+ return self._host_config.tcp_connect_timeout
def _execute(self):
if self._use_only_cache:
@@ -107,7 +107,7 @@ class TCPDataSource(CheckMKAgentDataSource):
port = self._get_port()
- encryption_settings = config.agent_encryption_of(self._hostname)
+ encryption_settings = self._host_config.agent_encryption
socktype = (socket.AF_INET6 if self._host_config.is_ipv6_primary else
socket.AF_INET)
s = socket.socket(socktype, socket.SOCK_STREAM)
@@ -174,7 +174,7 @@ class TCPDataSource(CheckMKAgentDataSource):
def _sub_result_version(self, agent_info):
agent_version = agent_info["version"]
- expected_version = config.agent_target_version(self._hostname)
+ expected_version = self._host_config.agent_target_version
if expected_version and agent_version \
and not self._is_expected_agent_version(agent_version, expected_version):
@@ -313,7 +313,7 @@ class TCPDataSource(CheckMKAgentDataSource):
def describe(self):
"""Return a short textual description of the
agent"""
- return "TCP: %s:%d" % (self._ipaddress,
config.agent_port_of(self._hostname))
+ return "TCP: %s:%d" % (self._ipaddress, self._host_config.agent_port)
@classmethod
def use_only_cache(cls):
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index d1bed27..0047cb4 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -5,6 +5,7 @@ import pytest # type: ignore
from testlib.base import Scenario
from cmk.utils.rulesets.ruleset_matcher import RulesetMatchObject
+import cmk
import cmk.utils.paths
import cmk_base.config as config
import cmk_base.piggyback as piggyback
@@ -263,6 +264,67 @@ def test_prepare_check_command_not_existing_password(capsys):
assert "The stored password \"pw-id\" used by service
\"blub\" on host \"bla\"" in stderr
+(a)pytest.mark.parametrize("hostname,result"sult", [
+ ("testhost1", 6556),
+ ("testhost2", 1337),
+])
+def test_host_config_agent_port(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset("agent_ports", [
+ (1337, [], ["testhost2"], {}),
+ ])
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).agent_port == result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
+ ("testhost1", 5.0),
+ ("testhost2", 12.0),
+])
+def test_host_config_tcp_connect_timeout(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset("tcp_connect_timeouts", [
+ (12.0, [], ["testhost2"], {}),
+ ])
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).tcp_connect_timeout == result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
+ ("testhost1", {
+ 'use_regular': 'disable',
+ 'use_realtime': 'enforce'
+ }),
+ ("testhost2", {
+ 'use_regular': 'enforce',
+ 'use_realtime': 'disable'
+ }),
+])
+def test_host_config_agent_encryption(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset("agent_encryption", [
+ ({
+ 'use_regular': 'enforce',
+ 'use_realtime': 'disable'
+ }, [], ["testhost2"], {}),
+ ])
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).agent_encryption == result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
+ ("testhost1", None),
+ ("testhost2", cmk.__version__),
+])
+def test_host_config_agent_target_version(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset("check_mk_agent_target_versions", [
+ ("site", [], ["testhost2"], {}),
+ ])
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).agent_target_version == result
+
+
def test_http_proxies():
assert config.http_proxies == {}