Module: check_mk
Branch: master
Commit: fbde9e5e65384224664c61ae75ae814e1e0f1250
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=fbde9e5e653842…
Author: Sebastian Herbord <sh(a)mathias-kettner.de>
Date: Mon May 9 15:11:59 2016 +0200
3130 periodic service discovery can now be configured to not activate specific service
changes
The rule "Periodic service discovery" now allows setting a whitelist and a
blacklist on service
description.
A service for which a change was automatically discovered that doesn't match any
whitelist or one
that does match any blacklist will still show up in the discovery service but it will not
be
activated automatically.
---
.werks/3130 | 13 +++++++
ChangeLog | 1 +
modules/discovery.py | 56 ++++++++++++++++++++++++----
web/plugins/wato/check_mk_configuration.py | 12 +++++-
4 files changed, 74 insertions(+), 8 deletions(-)
diff --git a/.werks/3130 b/.werks/3130
new file mode 100644
index 0000000..48ef6ae
--- /dev/null
+++ b/.werks/3130
@@ -0,0 +1,13 @@
+Title: periodic service discovery can now be configured to not activate specific service
changes
+Level: 1
+Component: wato
+Compatible: compat
+Version: 1.2.9i1
+Date: 1462799265
+Class: feature
+
+The rule "Periodic service discovery" now allows setting a whitelist and a
blacklist on service
+description.
+A service for which a change was automatically discovered that doesn't match any
whitelist or one
+that does match any blacklist will still show up in the discovery service but it will not
be
+activated automatically.
diff --git a/ChangeLog b/ChangeLog
index 87b8516..fe959ad 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -290,6 +290,7 @@
* 3239 The delay and the period of notifications can be fractions of a minute now.
* 3116 Timerange buttons for downtimes can now be configured...
* 3469 Show type of rule match of a rule (first, per-parameter, all) in WATO...
+ * 3130 periodic service discovery can now be configured to not activate specific
service changes...
* 3060 FIX: Folder properties: Fixed exception when a user has no alias set...
* 3062 FIX: Git integration: Fixed not adding files in WATO folders to git control
* 3203 FIX: Distributed WATO: Fixed exception in remote host service discovery...
diff --git a/modules/discovery.py b/modules/discovery.py
index b006d3f..c223f14 100644
--- a/modules/discovery.py
+++ b/modules/discovery.py
@@ -142,8 +142,11 @@ def do_discovery_for(hostname, check_types, only_new, use_caches,
on_error):
# determine changed services on host.
# param mode: can be one of "new", "remove", "fixall",
"refresh"
# param do_snmp_scan: if True, a snmp host will be scanned, otherwise uses only the check
types
-# previously discovered
-def discover_on_host(mode, hostname, do_snmp_scan, use_caches,
on_error="ignore"):
+# previously discovereda
+# param servic_filter: if a filter is set, it controls whether items are touched by the
discovery.
+# if it returns False for a new item it will not be added, if it
returns
+# False for a vanished item, that item is kept
+def discover_on_host(mode, hostname, do_snmp_scan, use_caches,
on_error="ignore", service_filter=None):
counts = {
"added" : 0,
"removed" : 0,
@@ -153,6 +156,9 @@ def discover_on_host(mode, hostname, do_snmp_scan, use_caches,
on_error="ignore"
if hostname not in all_active_realhosts():
return [0, 0, 0, 0], ""
+ if service_filter is None:
+ service_filter = lambda hostname, check_type, item: True
+
err = None
try:
@@ -175,7 +181,7 @@ def discover_on_host(mode, hostname, do_snmp_scan, use_caches,
on_error="ignore"
# that way.
if check_source in ("new"):
- if mode in ("new", "fixall", "refresh"):
+ if mode in ("new", "fixall", "refresh") and
service_filter(hostname, check_type, item):
counts["added"] += 1
new_items[(check_type, item)] = paramstring
@@ -187,7 +193,7 @@ def discover_on_host(mode, hostname, do_snmp_scan, use_caches,
on_error="ignore"
elif check_source in ("obsolete", "vanished"):
# keep item, if we are currently only looking for new services
# otherwise fix it: remove ignored and non-longer existing services
- if mode not in ("fixall", "remove"):
+ if mode not in ("fixall", "remove") or not
service_filter(hostname, check_type, item):
new_items[(check_type, item)] = paramstring
counts["kept"] += 1
else:
@@ -246,6 +252,10 @@ def default_discovery_check_parameters():
}
+def discovery_filter_by_lists(hostname, check_type, item, whitelist, blacklist):
+ description = service_description(hostname, check_type, item)
+ return whitelist.match(description) is not None and\
+ blacklist.match(description) is None
def check_discovery(hostname, ipaddress=None):
params = discovery_check_parameters(hostname) or \
@@ -270,6 +280,20 @@ def check_discovery(hostname, ipaddress=None):
long_infotexts = []
need_rediscovery = False
+ params_rediscovery = params.get("inventory_rediscovery", {})
+
+ if params_rediscovery.get("service_whitelist", []) or\
+ params_rediscovery.get("service_blacklist", []):
+ # whitelist. if none is specified, this matches everything
+ whitelist = regex("|".join(["(%s)" % pat for pat in
params_rediscovery.get("service_whitelist", [".*"])]))
+ # blacklist. if none is specified, this matches nothing
+ blacklist = regex("|".join(["(%s)" % pat for pat in
params_rediscovery.get("service_blacklist", ["(?!x)x"])]))
+
+ item_filters = lambda hostname, check_type, item:\
+ discovery_filter_by_lists(hostname, check_type, item, whitelist,
blacklist)
+ else:
+ item_filters = None
+
for check_state, title, params_key, default_state in [
( "new", "unmonitored",
"severity_unmonitored", inventory_check_severity ),
( "vanished", "vanished",
"severity_vanished", 0 ),
@@ -277,12 +301,17 @@ def check_discovery(hostname, ipaddress=None):
affected_check_types = {}
count = 0
+ unfiltered = False
for (check_type, item), (check_source, paramstring) in services.items():
if check_source == check_state:
count += 1
affected_check_types.setdefault(check_type, 0)
affected_check_types[check_type] += 1
+
+ if not unfiltered and item_filters(hostname, check_type, item):
+ unfiltered = True
+
long_infotexts.append("%s: %s: %s" % (title, check_type,
service_description(hostname, check_type, item)))
if affected_check_types:
@@ -293,8 +322,9 @@ def check_discovery(hostname, ipaddress=None):
if params.get("inventory_rediscovery", False):
mode = params["inventory_rediscovery"]["mode"]
- if (check_state == "new" and mode in ( 0, 2, 3 )) or
\
- check_state == "vanished" and mode in ( 1, 2, 3 ):
+ if unfiltered and\
+ ((check_state == "new" and mode in ( 0, 2, 3
)) or
+ (check_state == "vanished" and mode in ( 1, 2, 3
))):
need_rediscovery = True
else:
infotexts.append("no %s services found" % title)
@@ -448,12 +478,24 @@ def discover_marked_hosts():
# have to do hosts one-by-one because each could have a different configuration
params = discovery_check_parameters(hostname) or
default_discovery_check_parameters()
+ params_rediscovery = params["inventory_rediscovery"]
+ if "service_blacklist" in params_rediscovery or
"service_whitelist" in params_rediscovery:
+ # whitelist. if none is specified, this matches everything
+ whitelist = regex("|".join(["(%s)" % pat for pat in
params_rediscovery.get("service_whitelist", [".*"])]))
+ # blacklist. if none is specified, this matches nothing
+ blacklist = regex("|".join(["(%s)" % pat for pat in
params_rediscovery.get("service_blacklist", ["(?!x)x"])]))
+ item_filters = lambda hostname, check_type, item:\
+ discovery_filter_by_lists(hostname, check_type, item, whitelist,
blacklist)
+ else:
+ item_filters = None
+
why_not = may_rediscover(params)
if not why_not:
redisc_params = params["inventory_rediscovery"]
verbose(" Doing discovery with mode '%s'...\n" %
mode_table[redisc_params["mode"]])
result, error = discover_on_host(mode_table[redisc_params["mode"]],
hostname,
- params["inventory_check_do_scan"],
True)
+ params["inventory_check_do_scan"],
True,
+ service_filter=item_filters)
if error is not None:
if error:
verbose("failed: %s\n" % error)
diff --git a/web/plugins/wato/check_mk_configuration.py
b/web/plugins/wato/check_mk_configuration.py
index c318642..8b570fd 100644
--- a/web/plugins/wato/check_mk_configuration.py
+++ b/web/plugins/wato/check_mk_configuration.py
@@ -1863,8 +1863,18 @@ register_rule(group,
help = _("Here you can have the changes activated
whenever services "
"have been added or removed."),
)),
+ ( "service_whitelist",
+ ListOfStrings(
+ title = _("Whitelist Services"),
+ allow_empty = False,
+ )),
+ ( "service_blacklist",
+ ListOfStrings(
+ title = _("Blacklist Services"),
+ allow_empty = False,
+ )),
],
- optional_keys = None,
+ optional_keys = ["service_whitelist",
"service_blacklist"],
)),
],
optional_keys = ["inventory_rediscovery"],