Module: check_mk
Branch: master
Commit: 169433a9ae37d59f6fba5574ed5e455b77060b6c
URL: http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=169433a9ae37d5…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Apr 23 20:00:26 2019 +0200
Cleanup config.check_interval_of
CMK-2030
Change-Id: Ic2516a5e990b5d61f10cbbc141b14101179f4453
---
cmk_base/config.py | 33 +++++++++++++++++++++------------
cmk_base/core_nagios.py | 2 +-
cmk_base/data_sources/snmp.py | 2 +-
tests/unit/cmk_base/test_config.py | 17 +++++++++++++++++
4 files changed, 40 insertions(+), 14 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index e19294d..ca28bdc 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -763,18 +763,6 @@ def check_period_of(hostname, service):
return None
-def check_interval_of(hostname, section_name):
- if not cmk_base.cmk_base.check_utils.is_snmp_check(section_name):
- return # no values at all for non snmp checks
-
- # Previous to 1.5 "match" could be a check name (including subchecks) instead of
- # only main check names -> section names. This has been cleaned up, but we still
- # need to be compatible. Strip of the sub check part of "match".
- for match, minutes in get_config_cache().host_extra_conf(hostname, snmp_check_interval):
- if match is None or match.split(".")[0] == section_name:
- return minutes # use first match
-
-
#.
# .--Services------------------------------------------------------------.
# | ____ _ |
@@ -2507,6 +2495,27 @@ class HostConfig(object):
of this site"""
return self.hostname in self._config_cache.all_configured_clusters()
+ def snmp_check_interval(self, section_name):
+ # type: (str) -> Optional[int]
+ """Return the check interval of SNMP check sections
+
+ This has been added to reduce the check interval of single SNMP checks (or
+ more precise: sections) to be executed less frequent that the "Check_MK"
+ service is executed.
+ """
+ if not cmk_base.cmk_base.check_utils.is_snmp_check(section_name):
+ return None # no values at all for non snmp checks
+
+ # Previous to 1.5 "match" could be a check name (including subchecks) instead of
+ # only main check names -> section names. This has been cleaned up, but we still
+ # need to be compatible. Strip of the sub check part of "match".
+ for match, minutes in self._config_cache.host_extra_conf(self.hostname,
+ snmp_check_interval):
+ if match is None or match.split(".")[0] == section_name:
+ return minutes # use first match
+
+ return None
+
@property
def agent_port(self):
# type: () -> int
diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py
index e5f1fbe..fc8e680 100644
--- a/cmk_base/core_nagios.py
+++ b/cmk_base/core_nagios.py
@@ -325,7 +325,7 @@ def _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs):
check_interval = int(values[0])
except:
check_interval = float(values[0])
- value = config.check_interval_of(hostname, cmk_base.check_utils.section_name_of(checkname))
+ value = host_config.snmp_check_interval(cmk_base.check_utils.section_name_of(checkname))
if value is not None:
check_interval = value
diff --git a/cmk_base/data_sources/snmp.py b/cmk_base/data_sources/snmp.py
index f696658..3795852 100644
--- a/cmk_base/data_sources/snmp.py
+++ b/cmk_base/data_sources/snmp.py
@@ -247,7 +247,7 @@ class SNMPDataSource(DataSource):
persisted_sections = {}
for section_name, section_content in raw_data.items():
- check_interval = config.check_interval_of(self._hostname, section_name)
+ check_interval = self._host_config.snmp_check_interval(section_name)
if check_interval is None:
continue
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 116175f..c574970 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -9,6 +9,7 @@ import cmk
import cmk.utils.paths
import cmk_base.config as config
import cmk_base.piggyback as piggyback
+import cmk_base.check_api as check_api
def test_all_configured_realhosts(monkeypatch):
@@ -399,6 +400,22 @@ def test_host_config_exit_code_spec_individual(monkeypatch, hostname, result):
assert config_cache.get_host_config(hostname).exit_code_spec(data_source_id="snmp") == result
+(a)pytest.mark.parametrize("hostname,section_name,result", [
+ ("testhost1", "uptime", None),
+ ("testhost2", "uptime", None),
+ ("testhost1", "snmp_uptime", None),
+ ("testhost2", "snmp_uptime", 4),
+])
+def test_host_config_snmp_check_interval(monkeypatch, hostname, section_name, result):
+ config.load_checks(check_api.get_check_api_context, ["checks/uptime", "checks/snmp_uptime"])
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset("snmp_check_interval", [
+ (("snmp_uptime", 4), [], ["testhost2"], {}),
+ ])
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).snmp_check_interval(section_name) == result
+
+
def test_http_proxies():
assert config.http_proxies == {}
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", [
("testhost1", ["check_mk"]),
("testhost2", ["dingdong"]),
])