Module: check_mk
Branch: master
Commit: deb1f70aa538259641b0becf298e5351ed6b657a
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=deb1f70aa53825…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu May 2 19:57:55 2019 +0200
Moved service check_periods processing to ConfigCache
CMK-2030
Change-Id: Ieb901607987c09030470d64470ed8cc71562bbbf
---
cmk_base/checking.py | 21 ++++++++++-----------
cmk_base/config.py | 29 ++++++++++++-----------------
tests/unit/cmk_base/test_config.py | 19 +++++++++++++++++++
3 files changed, 41 insertions(+), 28 deletions(-)
diff --git a/cmk_base/checking.py b/cmk_base/checking.py
index 3956293..7becbb1 100644
--- a/cmk_base/checking.py
+++ b/cmk_base/checking.py
@@ -240,8 +240,8 @@ def _do_all_checks_on_host(sources, host_config, ipaddress,
only_check_plugin_na
hostname, description):
continue
- success = execute_check(multi_host_sections, hostname, ipaddress,
check_plugin_name, item,
- params, description)
+ success = execute_check(config_cache, multi_host_sections, hostname, ipaddress,
+ check_plugin_name, item, params, description)
if success:
num_success += 1
elif success is None:
@@ -260,21 +260,20 @@ def _do_all_checks_on_host(sources, host_config, ipaddress,
only_check_plugin_na
return num_success, missing_section_list
-def execute_check(multi_host_sections, hostname, ipaddress, check_plugin_name, item,
params,
- description):
+def execute_check(config_cache, multi_host_sections, hostname, ipaddress,
check_plugin_name, item,
+ params, description):
# Make a bit of context information globally available, so that functions
# called by checks now this context
check_api_utils.set_service(check_plugin_name, description)
item_state.set_item_state_prefix(check_plugin_name, item)
# Skip checks that are not in their check period
- period = config.check_period_of(hostname, description)
- if period and not cmk_base.core.check_timeperiod(period):
- console.verbose(
- "Skipping service %s: currently not in timeperiod %s.\n" %
(description, period))
- return None
-
- elif period:
+ period = config_cache.check_period_of_service(hostname, description)
+ if period is not None:
+ if not cmk_base.core.check_timeperiod(period):
+ console.verbose(
+ "Skipping service %s: currently not in timeperiod %s.\n" %
(description, period))
+ return None
console.vverbose("Service %s: timeperiod %s is currently active.\n" %
(description, period))
section_name = cmk_base.check_utils.section_name_of(check_plugin_name)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index ecf56fe..0fa8ef9 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -735,23 +735,6 @@ def _host_is_member_of_site(config_cache, hostname, site):
return True
-#
-# Misc
-#
-
-
-def check_period_of(hostname, service):
- periods = get_config_cache().service_extra_conf(hostname, service, check_periods)
- if periods:
- period = periods[0]
- if period == "24X7":
- return None
-
- return period
-
- return None
-
-
#.
# .--Services------------------------------------------------------------.
# | ____ _ |
@@ -3115,6 +3098,18 @@ class ConfigCache(object):
return None
return entries[0]
+ def check_period_of_service(self, hostname, description):
+ # type: (str, Text) -> Optional[str]
+ entries = self.service_extra_conf(hostname, description, check_periods)
+ if not entries:
+ return None
+
+ entry = entries[0]
+ if entry == "24X7":
+ return None
+
+ return entry
+
def get_explicit_service_custom_variables(self, hostname, description):
# type: (str, Text) -> Dict[str, str]
try:
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 8c0777e..783fcc1 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -1153,6 +1153,25 @@ def test_config_cache_service_level_of_service(monkeypatch,
hostname, result):
assert config_cache.service_level_of_service(hostname, "CPU load") ==
result
+(a)pytest.mark.parametrize("hostname,result"sult", [
+ ("testhost1", None),
+ ("testhost2", None),
+ ("testhost3", "xyz"),
+])
+def test_config_cache_check_period_of_service(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset(
+ "check_periods",
+ [
+ ("24X7", [], ["testhost2"], ["CPU load$"],
{}),
+ ("xyz", [], ["testhost3"], ["CPU load$"], {}),
+ ("zzz", [], ["testhost3"], ["CPU load$"], {}),
+ ],
+ )
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.check_period_of_service(hostname, "CPU load") ==
result
+
+
@pytest.mark.parametrize("edition_short,expected_cache_class_name,expected_host_class_name",
[
("cme", "CEEConfigCache", "CEEHostConfig"),
("cee", "CEEConfigCache", "CEEHostConfig"),