Module: check_mk
Branch: master
Commit: 889d142b8bfbdab49c24dfd1485d445d322e523d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=889d142b8bfbda…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Jan 31 22:15:28 2019 +0100
Add detailed output to new discovery log detail box
* Buffer the output produced during the "try-inventory" automation call
and send it as part of the resulting data structure to the discovery
background job
* The job will write the output to the background job output file and
show it to the user in the detail box
Note for the future: We should rework the automation calls to either
work inline or at least create a better interface that we don't need
hacks like this one.
CMK-1556
Change-Id: I51895ddd9cf7e2d665fb17de6bce05220884d890
---
cmk/gui/plugins/webapi/webapi.py | 2 +-
cmk/gui/wato/pages/services.py | 10 ++++++----
cmk_base/automations/check_mk.py | 24 ++++++++++++++++++++++++
3 files changed, 31 insertions(+), 5 deletions(-)
diff --git a/cmk/gui/plugins/webapi/webapi.py b/cmk/gui/plugins/webapi/webapi.py
index 3ebf3a9..edcf55c 100644
--- a/cmk/gui/plugins/webapi/webapi.py
+++ b/cmk/gui/plugins/webapi/webapi.py
@@ -1054,7 +1054,7 @@ class APICallOther(APICallCollection):
result = watolib.check_mk_automation(
host_attributes.get("site"), "try-inventory",
["@scan"] + [hostname])
counts = {"new": 0, "old": 0}
- for entry in result:
+ for entry in result["check_table"]:
if entry[0] in counts:
counts[entry[0]] += 1
diff --git a/cmk/gui/wato/pages/services.py b/cmk/gui/wato/pages/services.py
index f0bbe1e..8a627cb 100644
--- a/cmk/gui/wato/pages/services.py
+++ b/cmk/gui/wato/pages/services.py
@@ -31,6 +31,7 @@ import json
import traceback
import time
import pprint
+import sys
from hashlib import sha256
from typing import NamedTuple, Text, List, Optional # pylint: disable=unused-import
@@ -408,8 +409,9 @@ class ServiceDiscoveryBackgroundJob(WatoBackgroundJob):
def _perform_service_scan(self, request):
"""The try-inventory automation refreshes the Check_MK internal
cache and makes the new
information available to the next try-inventory call made by
get_result()."""
- check_mk_automation(request.host.site_id(), "try-inventory",
- self._get_automation_options(request))
+ result = check_mk_automation(request.host.site_id(), "try-inventory",
+ self._get_automation_options(request))
+ sys.stdout.write(result["output"])
def _perform_automatic_refresh(self, request):
_counts, _failed_hosts = check_mk_automation(
@@ -446,14 +448,14 @@ class ServiceDiscoveryBackgroundJob(WatoBackgroundJob):
# time for all data of a host. The data sources should be able to provide this
information
# somehow.
check_table_created = time.time()
- check_table = check_mk_automation(
+ result = check_mk_automation(
request.host.site_id(), "try-inventory",
["@noscan", "@raiseerrors", request.host.name()])
return DiscoveryResult(
job_status=job_status,
check_table_created=check_table_created,
- check_table=check_table,
+ check_table=result["check_table"],
)
def _check_table_file_path(self):
diff --git a/cmk_base/automations/check_mk.py b/cmk_base/automations/check_mk.py
index 553f00d..b9b8cfc 100644
--- a/cmk_base/automations/check_mk.py
+++ b/cmk_base/automations/check_mk.py
@@ -31,6 +31,8 @@ import subprocess
import sys
import time
import shutil
+import cStringIO
+import contextlib
import cmk.utils.paths
import cmk.utils.debug
@@ -125,12 +127,34 @@ class AutomationDiscovery(DiscoveryAutomation):
automations.register(AutomationDiscovery())
+# Python 3? use contextlib.redirect_stdout
+(a)contextlib.contextmanager
+def redirect_output(where):
+ """Redirects stdout/stderr to the given file like
object"""
+ prev_stdout, prev_stderr = sys.stdout, sys.stderr
+ prev_stdout.flush()
+ prev_stderr.flush()
+ sys.stdout = sys.stderr = where
+ try:
+ yield where
+ finally:
+ where.flush()
+ sys.stdout, sys.stderr = prev_stdout, prev_stderr
+
+
class AutomationTryDiscovery(Automation):
cmd = "try-inventory" # TODO: Rename!
needs_config = True
needs_checks = True # TODO: Can we change this?
def execute(self, args):
+ cmk.utils.log.set_verbosity(2)
+
+ with redirect_output(cStringIO.StringIO()) as buf:
+ result = self._execute_discovery(args)
+ return {"output": buf.getvalue(), "check_table": result}
+
+ def _execute_discovery(self, args):
use_caches = False
do_snmp_scan = False
if args[0] == '@noscan':