Module: check_mk
Branch: master
Commit: 127e09fe5c411a87e25c972bd9c0a992b68bea27
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=127e09fe5c411a…
Author: Andreas Boesl <ab(a)mathias-kettner.de>
Date: Mon Mar 25 09:24:30 2019 +0100
added WK7249: Check_Discovery: Automatic discovery of hosts now correctly ignores
unreachable hosts
Change-Id: Ibe9aea5d1399e09a6d2c9d7e441d86cb163c8379
---
.werks/7249 | 12 ++++++++++++
cmk_base/discovery.py | 42 +++++++++++++++++++++++++++++++++++-------
2 files changed, 47 insertions(+), 7 deletions(-)
diff --git a/.werks/7249 b/.werks/7249
new file mode 100644
index 0000000..5afe2ab
--- /dev/null
+++ b/.werks/7249
@@ -0,0 +1,12 @@
+Title: Check_Discovery: Automatic discovery of hosts now correctly ignores unreachable
hosts
+Level: 1
+Component: core
+Class: fix
+Compatible: compat
+Edition: cre
+State: unknown
+Version: 1.6.0i1
+Date: 1553184609
+
+Hosts with a state differing from <tt>UP</tt> will no longer be considered in
the
+automatic check discovery routine.
diff --git a/cmk_base/discovery.py b/cmk_base/discovery.py
index a4729c5..687d0ea 100644
--- a/cmk_base/discovery.py
+++ b/cmk_base/discovery.py
@@ -495,10 +495,21 @@ def discover_marked_hosts(core):
if not hosts:
console.verbose(" Nothing to do. No hosts marked by discovery
check.\n")
+
+ # Fetch host state information from livestatus
+ host_states = _fetch_host_states()
activation_required = False
+
try:
_set_discovery_timeout()
for hostname in hosts:
+ if not _discover_marked_host_exists(hostname, all_hosts):
+ continue
+
+ # Only try to discover hosts with UP state
+ if host_states and host_states.get(hostname) != 0:
+ continue
+
if _discover_marked_host(hostname, all_hosts, now_ts, oldest_queued):
activation_required = True
@@ -520,6 +531,30 @@ def discover_marked_hosts(core):
cmk_base.core.do_restart(core)
+def _fetch_host_states():
+ host_states = {}
+ try:
+ import livestatus
+ query = "GET hosts\nColumns: name state"
+ host_states = dict(livestatus.LocalConnection().query(query))
+ except (livestatus.MKLivestatusNotFoundError, livestatus.MKLivestatusSocketError):
+ pass
+ return host_states
+
+
+def _discover_marked_host_exists(hostname, all_hosts):
+ if hostname in all_hosts:
+ return True
+
+ host_flag_path = os.path.join(_get_autodiscovery_dir(), hostname)
+ try:
+ os.remove(host_flag_path)
+ except OSError:
+ pass
+ console.verbose(" Skipped. Host %s does not exist in configuration. Removing
mark.\n" % hostname)
+ return False
+
+
def _discover_marked_host(hostname, all_hosts, now_ts, oldest_queued):
services_changed = False
@@ -527,13 +562,6 @@ def _discover_marked_host(hostname, all_hosts, now_ts,
oldest_queued):
console.verbose("%s%s%s:\n" % (tty.bold, hostname, tty.normal))
host_flag_path = os.path.join(_get_autodiscovery_dir(), hostname)
- if hostname not in all_hosts:
- try:
- os.remove(host_flag_path)
- except OSError:
- pass
- console.verbose(" Skipped. Host does not exist in configuration. Removing
mark.\n")
- return
params = discovery_check_parameters(hostname) or
default_discovery_check_parameters()
params_rediscovery = params.get("inventory_rediscovery", {})