Module: check_mk
Branch: master
Commit: 81151bf1258061289282819600f0847200820c3c
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=81151bf1258061…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Apr 23 19:35:15 2019 +0200
Cleanup config.exit_code_spec
CMK-2030
Change-Id: I8db2ede87168f6a90c0c5f75ec8586156e1982d2
---
cmk_base/checking.py | 3 +--
cmk_base/config.py | 49 +++++++++++++++++++-------------------
cmk_base/data_sources/abstract.py | 9 ++++---
cmk_base/data_sources/programs.py | 3 ---
cmk_base/data_sources/tcp.py | 4 ++--
cmk_base/decorator.py | 3 ++-
tests/unit/cmk_base/test_config.py | 48 +++++++++++++++++++++++++++++++++++++
7 files changed, 82 insertions(+), 37 deletions(-)
diff --git a/cmk_base/checking.py b/cmk_base/checking.py
index 2bc025d..3956293 100644
--- a/cmk_base/checking.py
+++ b/cmk_base/checking.py
@@ -92,8 +92,7 @@ def do_check(hostname, ipaddress, only_check_plugin_names=None):
config_cache = config.get_config_cache()
host_config = config_cache.get_host_config(hostname)
- # Exit state in various situations is configurable since 1.2.3i1
- exit_spec = config.exit_code_spec(hostname)
+ exit_spec = host_config.exit_code_spec()
status, infotexts, long_infotexts, perfdata = 0, [], [], []
try:
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 16de0f8..e19294d 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -751,30 +751,6 @@ def get_explicit_service_custom_variables(hostname, description):
#
-def exit_code_spec(hostname, data_source_id=None):
- spec = {}
- specs = get_config_cache().host_extra_conf(hostname, check_mk_exit_status)
- for entry in specs[::-1]:
- spec.update(entry)
- return _get_exit_code_spec(spec, data_source_id)
-
-
-def _get_exit_code_spec(spec, data_source_id):
- if data_source_id is not None:
- try:
- return spec["individual"][data_source_id]
- except KeyError:
- pass
-
- try:
- return spec["overall"]
- except KeyError:
- pass
-
- # Old configuration format
- return spec
-
-
def check_period_of(hostname, service):
periods = get_config_cache().service_extra_conf(hostname, service, check_periods)
if periods:
@@ -2648,6 +2624,31 @@ class HostConfig(object):
return default_value
+ def exit_code_spec(self, data_source_id=None):
+ # type: (Optional[str]) -> Dict[str, int]
+ spec = {} # type: Dict[str, int]
+ # TODO: Can we use host_extra_conf_merged?
+ specs = self._config_cache.host_extra_conf(self.hostname, check_mk_exit_status)
+ for entry in specs[::-1]:
+ spec.update(entry)
+ return self._merge_with_data_source_exit_code_spec(spec, data_source_id)
+
+ def _merge_with_data_source_exit_code_spec(self, spec, data_source_id):
+ # type: (Dict, Optional[str]) -> Dict[str, int]
+ if data_source_id is not None:
+ try:
+ return spec["individual"][data_source_id]
+ except KeyError:
+ pass
+
+ try:
+ return spec["overall"]
+ except KeyError:
+ pass
+
+ # Old configuration format
+ return spec
+
#.
# .--Configuration Cache-------------------------------------------------.
diff --git a/cmk_base/data_sources/abstract.py b/cmk_base/data_sources/abstract.py
index 178daa3..805428c 100644
--- a/cmk_base/data_sources/abstract.py
+++ b/cmk_base/data_sources/abstract.py
@@ -85,7 +85,6 @@ class DataSource(object):
# Runtime data (managed by self.run()) - Meant for self.get_summary_result()
self._exception = None
self._host_sections = None
- self._exit_code_spec = config.exit_code_spec(hostname, data_source_id=self.id())
self._persisted_sections = None
self._config_cache = config.get_config_cache()
@@ -388,16 +387,16 @@ class DataSource(object):
exc_msg = "%s" % self._exception
if isinstance(self._exception, MKEmptyAgentData):
- status = self._exit_code_spec.get("empty_output", 2)
+ status = self._host_config.exit_code_spec().get("empty_output", 2)
elif isinstance(self._exception, (MKAgentError, MKIPAddressLookupError,
MKSNMPError)):
- status = self._exit_code_spec.get("connection", 2)
+ status = self._host_config.exit_code_spec().get("connection", 2)
elif isinstance(self._exception, MKTimeout):
- status = self._exit_code_spec.get("timeout", 2)
+ status = self._host_config.exit_code_spec().get("timeout", 2)
else:
- status = self._exit_code_spec.get("exception", 3)
+ status = self._host_config.exit_code_spec().get("exception", 3)
return status, exc_msg + check_api_utils.state_markers[status], []
diff --git a/cmk_base/data_sources/programs.py b/cmk_base/data_sources/programs.py
index 50ff366..659805f 100644
--- a/cmk_base/data_sources/programs.py
+++ b/cmk_base/data_sources/programs.py
@@ -180,9 +180,6 @@ class SpecialAgentDataSource(ProgramDataSource):
def special_agent_plugin_file_name(self):
return "agent_%s" % self._special_agent_id
- def _get_individual_exit_code_spec(self, exit_code_spec):
- return exit_code_spec["individual"]["special"]
-
# TODO: Can't we make this more specific in case of special agents?
def _gather_check_plugin_names(self):
return config.discoverable_tcp_checks()
diff --git a/cmk_base/data_sources/tcp.py b/cmk_base/data_sources/tcp.py
index 676aa22..58a80a7 100644
--- a/cmk_base/data_sources/tcp.py
+++ b/cmk_base/data_sources/tcp.py
@@ -192,12 +192,12 @@ class TCPDataSource(CheckMKAgentDataSource):
expected += ' release %s' %
expected_version[1]['release']
else:
expected = expected_version
- status = self._exit_code_spec.get("wrong_version", 1)
+ status = self._host_config.exit_code_spec().get("wrong_version",
1)
output = ", unexpected agent version %s (should be %s)%s" \
% (agent_version, expected, state_markers[status])
elif config.agent_min_version and agent_version < config.agent_min_version:
- status = self._exit_code_spec.get("wrong_version", 1)
+ status = self._host_config.exit_code_spec().get("wrong_version",
1)
output = ", old plugin version %s (should be at least %s)%s" \
% (agent_version, config.agent_min_version, state_markers[status])
diff --git a/cmk_base/decorator.py b/cmk_base/decorator.py
index 580be96..53e7591 100644
--- a/cmk_base/decorator.py
+++ b/cmk_base/decorator.py
@@ -46,7 +46,8 @@ def handle_check_mk_check_result(check_plugin_name, description):
def wrap(check_func):
def wrapped_check_func(hostname, *args, **kwargs):
- exit_spec = config.exit_code_spec(hostname)
+ host_config = config.get_config_cache().get_host_config(hostname)
+ exit_spec = host_config.exit_code_spec()
status, infotexts, long_infotexts, perfdata = 0, [], [], []
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index da7c5ec..116175f 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -351,6 +351,54 @@ def test_host_config_contactgroups(monkeypatch, hostname, result):
assert config_cache.get_host_config(hostname).contactgroups == result
+(a)pytest.mark.parametrize("hostname,result"sult", [
+ ("testhost1", {}),
+ ("testhost2", {
+ 'empty_output': 1
+ }),
+])
+def test_host_config_exit_code_spec_overall(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset("check_mk_exit_status", [
+ ({
+ "overall": {
+ "empty_output": 1
+ },
+ "individual": {
+ "snmp": {
+ "empty_output": 4
+ }
+ },
+ }, [], ["testhost2"], {}),
+ ])
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).exit_code_spec() == result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
+ ("testhost1", {}),
+ ("testhost2", {
+ 'empty_output': 4
+ }),
+])
+def test_host_config_exit_code_spec_individual(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset("check_mk_exit_status", [
+ ({
+ "overall": {
+ "empty_output": 1
+ },
+ "individual": {
+ "snmp": {
+ "empty_output": 4
+ }
+ },
+ }, [], ["testhost2"], {}),
+ ])
+ config_cache = ts.apply(monkeypatch)
+ assert
config_cache.get_host_config(hostname).exit_code_spec(data_source_id="snmp") ==
result
+
+
def test_http_proxies():
assert config.http_proxies == {}