Module: check_mk
Branch: master
Commit: b31309d8a2cbba26cba266a89e9692e0f1f93f17
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=b31309d8a2cbba…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Mar 26 14:58:34 2019 +0100
Cleaned up check_table.service_deps function
* Moved service_dependencies ruleset processing to config module
* Renamed function to service_depends_on to be more specific
* Added some basic unit tests for this function
Change-Id: I805b0e1981e61743e4882a9d29156e722cdf83d4
---
cmk_base/check_table.py | 36 +-----------------------------------
cmk_base/config.py | 36 ++++++++++++++++++++++++++++++++++++
cmk_base/core_nagios.py | 2 +-
tests/unit/cmk_base/test_config.py | 22 +++++++++++++++++++++-
4 files changed, 59 insertions(+), 37 deletions(-)
diff --git a/cmk_base/check_table.py b/cmk_base/check_table.py
index 04c6af5..0e2a5f2 100644
--- a/cmk_base/check_table.py
+++ b/cmk_base/check_table.py
@@ -25,7 +25,6 @@
# Boston, MA 02110-1301 USA.
"""Code for computing the table of checks of hosts."""
-from cmk.utils.regex import regex
from cmk.utils.exceptions import MKGeneralException
import cmk_base
@@ -157,7 +156,7 @@ def get_check_table(hostname,
elif filter_mode == "only_clustered" and svc_is_mine:
return
- deps = service_deps(hostname, descr)
+ deps = config.service_depends_on(hostname, descr)
check_table[(checkname, item)] = (params, descr, deps)
# Now process all entries that are specific to the host
@@ -234,39 +233,6 @@ def get_precompiled_check_parameters(hostname, item, params,
check_plugin_name):
return params
-# Return a list of services this services depends upon
-# TODO: Make this use the generic "rulesets" functions
-# TODO: Is this needed here? Investigate for what this is used for
-def service_deps(hostname, servicedesc):
- deps = []
- config_cache = config.get_config_cache()
- for entry in config.service_dependencies:
- entry, rule_options = config.get_rule_options(entry)
- if rule_options.get("disabled"):
- continue
-
- if len(entry) == 3:
- depname, hostlist, patternlist = entry
- tags = []
- elif len(entry) == 4:
- depname, tags, hostlist, patternlist = entry
- else:
- raise MKGeneralException("Invalid entry '%r' in service
dependencies: "
- "must have 3 or 4 entries" % entry)
-
- if config.hosttags_match_taglist(config_cache.tags_of_host(hostname), tags) and
\
- config.in_extraconf_hostlist(hostlist, hostname):
- for pattern in patternlist:
- matchobject = regex(pattern).search(servicedesc)
- if matchobject:
- try:
- item = matchobject.groups()[-1]
- deps.append(depname % item)
- except:
- deps.append(depname)
- return deps
-
-
def remove_duplicate_checks(check_table):
have_with_tcp = {}
have_with_snmp = {}
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 232131c..9aea948 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -1419,6 +1419,42 @@ def _checktype_ignored_for_host(host, checktype):
return False
+# TODO: Make this use the generic "rulesets" functions
+# a) This function has never been configurable via WATO (see
https://mathias-kettner.de/checkmk_service_dependencies.html)
+# b) It only affects the Nagios core - CMC does not implement service dependencies
+# c) This function implements some specific regex replacing match+replace which makes it
incompatible to
+# regular service rulesets. Therefore service_extra_conf() can not easily be used :-/
+def service_depends_on(hostname, servicedesc):
+ """Return a list of services this services depends
upon"""
+ deps = []
+ config_cache = get_config_cache()
+ for entry in service_dependencies:
+ entry, rule_options = get_rule_options(entry)
+ if rule_options.get("disabled"):
+ continue
+
+ if len(entry) == 3:
+ depname, hostlist, patternlist = entry
+ tags = []
+ elif len(entry) == 4:
+ depname, tags, hostlist, patternlist = entry
+ else:
+ raise MKGeneralException("Invalid entry '%r' in service
dependencies: "
+ "must have 3 or 4 entries" % entry)
+
+ if hosttags_match_taglist(config_cache.tags_of_host(hostname), tags) and \
+ in_extraconf_hostlist(hostlist, hostname):
+ for pattern in patternlist:
+ matchobject = regex(pattern).search(servicedesc)
+ if matchobject:
+ try:
+ item = matchobject.groups()[-1]
+ deps.append(depname % item)
+ except:
+ deps.append(depname)
+ return deps
+
+
#.
# .--Misc Helpers--------------------------------------------------------.
# | __ __ _ _ _ _ |
diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py
index 3a21e55..b5a080e 100644
--- a/cmk_base/core_nagios.py
+++ b/cmk_base/core_nagios.py
@@ -272,7 +272,7 @@ def _create_nagios_servicedefs(cfg, hostname, host_attrs):
def get_dependencies(hostname, servicedesc):
result = ""
- for dep in check_table.service_deps(hostname, servicedesc):
+ for dep in config.service_depends_on(hostname, servicedesc):
result += _format_nagios_object(
"servicedependency", {
"use": config.service_dependency_template,
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 6a05865..9a07119 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -1,6 +1,6 @@
# encoding: utf-8
-import pytest
+import pytest # type: ignore
import cmk_base.config as config
@@ -205,3 +205,23 @@ def test_http_proxy(http_proxy, result, monkeypatch):
def _setup_host(monkeypatch, hostname, tags):
monkeypatch.setattr(config, "all_hosts", ["%s|%s" % (hostname,
"|".join(tags))])
monkeypatch.setattr(config, "host_paths", {hostname: "/"})
+
+
+def test_service_depends_on(monkeypatch):
+ assert config.service_depends_on("test-host", "svc") == []
+
+ monkeypatch.setattr(config, "all_hosts", ["test-host"])
+ monkeypatch.setattr(config, "host_paths", {"test-host":
"/"})
+ monkeypatch.setattr(config, "service_dependencies", [
+ ("dep1", [], config.ALL_HOSTS, ["svc1"], {}),
+ ("dep2-%s", [], config.ALL_HOSTS, ["svc1-(.*)"], {}),
+ ("dep-disabled", [], config.ALL_HOSTS, ["svc1"], {
+ "disabled": True
+ }),
+ ])
+
+ config.get_config_cache().initialize()
+
+ assert config.service_depends_on("test-host", "svc2") == []
+ assert config.service_depends_on("test-host", "svc1") ==
["dep1"]
+ assert config.service_depends_on("test-host", "svc1-abc") ==
["dep1", "dep2-abc"]