Module: check_mk
Branch: master
Commit: 3aa6e1d3cd84d94c39daf9d83ebf4de2db144fbd
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=3aa6e1d3cd84d9…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Aug 2 08:50:30 2017 +0200
Fixed broken analyse services automation ("Check origin and parameters" were
missing on analyse services page of WATO)
Change-Id: I9d91fd71a6f7962304bf89c6105918045e53cbc8
---
cmk_base/automations/check_mk.py | 55 ++++++++++--------------
tests/cmk_base/test_automations.py | 88 ++++++++++++++++++++++++++++++++++++++
2 files changed, 111 insertions(+), 32 deletions(-)
diff --git a/cmk_base/automations/check_mk.py b/cmk_base/automations/check_mk.py
index 47efb9a..139ee35 100644
--- a/cmk_base/automations/check_mk.py
+++ b/cmk_base/automations/check_mk.py
@@ -519,39 +519,30 @@ class AutomationAnalyseServices(Automation):
# 2. Load all autochecks of the host in question and try to find
# our service there
- try:
- path = "%s/%s.mk" % (cmk.paths.autochecks_dir, hostname)
- if os.path.exists(path):
- for entry in eval(file(path).read()):
- if len(entry) == 4: # old format
- hn, ct, item, params = entry
- else:
- ct, item, params = entry # new format without host name
- hn = hostname
-
- if (ct, item) not in table:
- continue # this is a removed duplicate or clustered service
- descr = config.service_description(hn, ct, item)
- if hn == hostname and descr == servicedesc:
- dlv =
checks.check_info[ct].get("default_levels_variable")
- if dlv:
- fs = checks.factory_settings.get(dlv, None)
- else:
- fs = None
+ for entry in discovery.read_autochecks_of(hostname):
+ ct, item, params = entry # new format without host name
- return {
- "origin" : "auto",
- "checktype" : ct,
- "checkgroup" :
checks.check_info[ct].get("group"),
- "item" : item,
- "inv_parameters" : params,
- "factory_settings" : fs,
- "parameters" :
- checks.compute_check_parameters(hostname, ct, item,
params),
- }
- except:
- if cmk.debug.enabled():
- raise
+ if (ct, item) not in table:
+ continue # this is a removed duplicate or clustered service
+
+ descr = config.service_description(hostname, ct, item)
+ if descr == servicedesc:
+ dlv = checks.check_info[ct].get("default_levels_variable")
+ if dlv:
+ fs = checks.factory_settings.get(dlv, None)
+ else:
+ fs = None
+
+ return {
+ "origin" : "auto",
+ "checktype" : ct,
+ "checkgroup" :
checks.check_info[ct].get("group"),
+ "item" : item,
+ "inv_parameters" : params,
+ "factory_settings" : fs,
+ "parameters" :
+ checks.compute_check_parameters(hostname, ct, item, params),
+ }
# 3. Classical checks
for nr, entry in enumerate(config.custom_checks):
diff --git a/tests/cmk_base/test_automations.py b/tests/cmk_base/test_automations.py
new file mode 100644
index 0000000..34699a4
--- /dev/null
+++ b/tests/cmk_base/test_automations.py
@@ -0,0 +1,88 @@
+import pytest
+import subprocess
+
+from testlib import web, repo_path
+import ast
+
+(a)pytest.fixture(scope="module")
+def test_cfg(web, site):
+ print "Applying default config"
+ web.add_host("modes-test-host", attributes={
+ "ipaddress": "127.0.0.1",
+ })
+ web.add_host("modes-test-host2", attributes={
+ "ipaddress": "127.0.0.1",
+ "tag_criticality": "test",
+ })
+ web.add_host("modes-test-host3", attributes={
+ "ipaddress": "127.0.0.1",
+ "tag_criticality": "test",
+ })
+ web.add_host("modes-test-host4", attributes={
+ "ipaddress": "127.0.0.1",
+ "tag_criticality": "offline",
+ })
+
+ site.write_file("etc/check_mk/conf.d/modes-test-host.mk",
+ "datasource_programs.append(('cat
~/var/check_mk/agent_output/<HOST>', [], ALL_HOSTS))\n")
+
+ site.makedirs("var/check_mk/agent_output/")
+ site.write_file("var/check_mk/agent_output/modes-test-host",
+ file("%s/tests/data/linux-agent-output" % repo_path()).read())
+ site.write_file("var/check_mk/agent_output/modes-test-host2",
+ file("%s/tests/data/linux-agent-output" % repo_path()).read())
+ site.write_file("var/check_mk/agent_output/modes-test-host3",
+ file("%s/tests/data/linux-agent-output" % repo_path()).read())
+
+ web.discover_services("modes-test-host")
+ web.discover_services("modes-test-host2")
+ web.discover_services("modes-test-host3")
+
+ web.activate_changes()
+ yield None
+
+ #
+ # Cleanup code
+ #
+ print "Cleaning up test config"
+
+ site.delete_dir("var/check_mk/agent_output")
+
+ site.delete_file("etc/check_mk/conf.d/modes-test-host.mk")
+
+ web.delete_host("modes-test-host")
+ web.delete_host("modes-test-host2")
+ web.delete_host("modes-test-host3")
+ web.delete_host("modes-test-host4")
+
+#.
+# .--automation----------------------------------------------------------.
+# | _ _ _ |
+# | __ _ _ _| |_ ___ _ __ ___ __ _| |_(_) ___ _ __ |
+# | / _` | | | | __/ _ \| '_ ` _ \ / _` | __| |/ _ \| '_ \ |
+# | | (_| | |_| | || (_) | | | | | | (_| | |_| | (_) | | | | |
+# | \__,_|\__,_|\__\___/|_| |_| |_|\__,_|\__|_|\___/|_| |_| |
+# | |
+# '----------------------------------------------------------------------'
+
+def test_automation_analyse_service_autocheck(test_cfg, site):
+ p = site.execute(["cmk", "--automation",
"analyse-service", "--", "modes-test-host", "CPU
load"],
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ stdout, stderr = p.communicate()
+ assert p.wait() == 0
+ assert stderr == ""
+ data = ast.literal_eval(stdout)
+
+ assert data["origin"] == "auto"
+ assert data["checktype"] == "cpu.loads"
+ assert data["item"] == None
+ assert data["checkgroup"] == "cpu_load"
+
+
+def test_automation_analyse_service_no_check(test_cfg, site):
+ p = site.execute(["cmk", "--automation",
"analyse-service", "--", "modes-test-host", "XXX CPU
load"],
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ stdout, stderr = p.communicate()
+ assert p.wait() == 0
+ assert stderr == ""
+ assert stdout == "{}\n"