Module: check_mk
Branch: master
Commit: be3f94bbd803d6742e03638c27895674eb2b20e7
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=be3f94bbd803d6…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu May 2 16:57:45 2019 +0200
Centralize computation of passive check interval computation
The logic for computing the check interval of passive checks
that base on the Check_MK service has been centralized.
Added a test for the computation on the way.
CMK-2030
Change-Id: I3727f4d25b48ff1cfd91af1cec29587193d693df
---
cmk_base/core_config.py | 14 ++++++++++++++
cmk_base/core_nagios.py | 14 ++------------
tests/unit/cmk_base/test_core_config.py | 32 ++++++++++++++++++++++++++++++++
tests/unit/cmk_base/test_core_nagios.py | 4 ++--
4 files changed, 50 insertions(+), 14 deletions(-)
diff --git a/cmk_base/core_config.py b/cmk_base/core_config.py
index 7ce2fda..ec5d159 100644
--- a/cmk_base/core_config.py
+++ b/cmk_base/core_config.py
@@ -310,6 +310,20 @@ def active_check_arguments(hostname, description, args):
# '----------------------------------------------------------------------'
+def get_cmk_passive_service_attributes(config_cache, host_config, description, checkname,
params,
+ check_mk_attrs):
+ attrs = get_service_attributes(host_config.hostname, description, config_cache,
checkname,
+ params)
+
+ value = host_config.snmp_check_interval(config_cache.section_name_of(checkname))
+ if value is not None:
+ attrs["check_interval"] = value
+ else:
+ attrs["check_interval"] = check_mk_attrs["check_interval"]
+
+ return attrs
+
+
def get_service_attributes(hostname, description, config_cache, checkname=None,
params=None):
attrs = _extra_service_attributes(hostname, description, config_cache, checkname,
params)
attrs.update(_get_tag_attributes(config_cache.tags_of_service(hostname, description),
"TAG"))
diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py
index 012d787..22783ab 100644
--- a/cmk_base/core_nagios.py
+++ b/cmk_base/core_nagios.py
@@ -316,26 +316,16 @@ def _create_nagios_servicedefs(cfg, config_cache, hostname,
host_attrs):
"dependent_service_description": description,
}).encode("utf-8"))
- # TODO: Find a generic (CMC/Nagios) place for this logic
- # Add the check interval of either the Check_MK service or
- # (if configured) the snmp_check_interval for snmp based checks
- value =
host_config.snmp_check_interval(cmk_base.check_utils.section_name_of(checkname))
- if value is not None:
- check_interval = value
- else:
- check_interval = check_mk_attrs["check_interval"]
-
service_spec = {
"use": template,
"host_name": hostname,
"service_description": description,
- "check_interval": check_interval,
"check_command": "check_mk-%s" % checkname,
}
service_spec.update(
- core_config.get_service_attributes(
- hostname, description, config_cache, checkname=checkname,
params=params))
+ core_config.get_cmk_passive_service_attributes(config_cache, host_config,
description,
+ checkname, params,
check_mk_attrs))
service_spec.update(_extra_service_conf_of(cfg, config_cache, hostname,
description))
outfile.write(_format_nagios_object("service",
service_spec).encode("utf-8"))
diff --git a/tests/unit/cmk_base/test_core_config.py
b/tests/unit/cmk_base/test_core_config.py
index 1031ef4..ff5c1f6 100644
--- a/tests/unit/cmk_base/test_core_config.py
+++ b/tests/unit/cmk_base/test_core_config.py
@@ -6,6 +6,7 @@ from testlib.base import Scenario
from cmk.utils.exceptions import MKGeneralException
import cmk_base.config as config
import cmk_base.core_config as core_config
+import cmk_base.check_api as check_api
def test_active_check_arguments(mocker):
@@ -49,6 +50,37 @@ def test_get_host_attributes(fixup_ip_lookup, monkeypatch):
}
+(a)pytest.mark.parametrize("hostname,result"sult", [
+ ("localhost", {
+ 'check_interval': 1.0,
+ 'contact_groups': u'ding',
+ }),
+ ("blub", {
+ 'check_interval': 40.0
+ }),
+])
+def test_get_cmk_passive_service_attributes(monkeypatch, hostname, result):
+ config.load_checks(check_api.get_check_api_context, ["checks/cpu"])
+
+ ts = Scenario().add_host("localhost")
+ ts.add_host("blub")
+ ts.set_option(
+ "extra_service_conf", {
+ "contact_groups": [(u'ding', ['localhost'],
["CPU load$"]),],
+ "check_interval": [
+ (40.0, ['blub'], ["Check_MK$"]),
+ (33.0, ['localhost'], ["CPU load$"]),
+ ],
+ })
+ config_cache = ts.apply(monkeypatch)
+ host_config = config_cache.get_host_config(hostname)
+ check_mk_attrs = core_config.get_service_attributes(hostname, "Check_MK",
config_cache)
+
+ service_spec = core_config.get_cmk_passive_service_attributes(
+ config_cache, host_config, "CPU load", "cpu.loads", {},
check_mk_attrs)
+ assert service_spec == result
+
+
@pytest.mark.parametrize("tag_groups,result", [({
"tg1": "val1",
"tg2": "val1",
diff --git a/tests/unit/cmk_base/test_core_nagios.py
b/tests/unit/cmk_base/test_core_nagios.py
index d60cb9b..495add3 100644
--- a/tests/unit/cmk_base/test_core_nagios.py
+++ b/tests/unit/cmk_base/test_core_nagios.py
@@ -29,7 +29,7 @@ def test_format_nagios_object():
""" % tuple(itertools.chain(*sorted(spec.items(), key=lambda x: x[0])))
-def ts():
+def ts_test_create_nagios_host_spec():
ts1 = Scenario().add_host("localhost")
result1 = {
'_ADDRESS_4': '127.0.0.1',
@@ -123,7 +123,7 @@ def ts():
yield ts4, result4
-(a)pytest.mark.parametrize("ts,result"sult", ts())
+(a)pytest.mark.parametrize("ts,result"sult", ts_test_create_nagios_host_spec())
def test_create_nagios_host_spec(ts, result, monkeypatch):
outfile = StringIO()
cfg = core_nagios.NagiosConfig(outfile, ["localhost"])