Module: check_mk
Branch: master
Commit: d705f4b80441505c425f0616049aec0746cf451c
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=d705f4b8044150…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Mar 31 15:56:55 2016 +0200
3351 FIX Network scan: Fixed number in hosts not being updated during network scan
---
.werks/3351 | 10 +++++++
ChangeLog | 1 +
modules/check_mk.py | 2 +-
modules/check_mk_base.py | 32 ++++++++++++--------
web/htdocs/wato.py | 51 ++++++++++++++++++++++++++++++++
web/plugins/wato/builtin_attributes.py | 5 +++-
6 files changed, 87 insertions(+), 14 deletions(-)
diff --git a/.werks/3351 b/.werks/3351
new file mode 100644
index 0000000..cc51b21
--- /dev/null
+++ b/.werks/3351
@@ -0,0 +1,10 @@
+Title: Network scan: Fixed number in hosts not being updated during network scan
+Level: 1
+Component: wato
+Class: fix
+Compatible: compat
+State: unknown
+Version: 1.2.9i1
+Date: 1459432586
+
+
diff --git a/ChangeLog b/ChangeLog
index 7d9085c..b95220f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -187,6 +187,7 @@
* 3302 FIX: Improved error handling when trying to edit a not existing rule
* 3338 FIX: Improved error handling when host to be edited does not exist
* 3318 FIX: CSV bulk import of hosts: fix handling of CSV column headers...
+ * 3351 FIX: Network scan: Fixed number in hosts not being updated during network
scan
Notifications:
* 3263 Notifications: allow users to restrict by their contact groups...
diff --git a/modules/check_mk.py b/modules/check_mk.py
index f203315..6670e68 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -2298,7 +2298,7 @@ def service_description(hostname, check_type, item):
return descr.strip()
-# Get rules for piggyback translation for that hostname
+# Get a dict that specifies the actions to be done during the hostname translation
def get_piggyback_translation(hostname):
rules = host_extra_conf(hostname, piggyback_translation)
translations = {}
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index 64a457d..b09d8dc 100644
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -655,24 +655,32 @@ def remove_piggyback_info_from(sourcehost, keep=[]):
pass
return removed
+
def translate_piggyback_host(sourcehost, backedhost):
translation = get_piggyback_translation(sourcehost)
+ return do_hostname_translation(translation, backedhost)
+
+# When changing this keep it in sync with
+# a) check_mk_base.py: do_hostname_translation()
+# b) wato.py: do_hostname_translation()
+# FIXME TODO: Move the common do_hostname_translation() in a central Check_MK module
+def do_hostname_translation(translation, hostname):
# 1. Case conversion
caseconf = translation.get("case")
if caseconf == "upper":
- backedhost = backedhost.upper()
+ hostname = hostname.upper()
elif caseconf == "lower":
- backedhost = backedhost.lower()
+ hostname = hostname.lower()
# 2. Drop domain part (not applied to IP addresses!)
- if translation.get("drop_domain") and not backedhost[0].isdigit():
- backedhost = backedhost.split(".", 1)[0]
+ if translation.get("drop_domain") and not hostname[0].isdigit():
+ hostname = hostname.split(".", 1)[0]
- # To make it possible to match umlauts we need to change the backendhost
+ # To make it possible to match umlauts we need to change the hostname
# to a unicode string which can then be matched with regexes etc.
# We assume the incoming name is correctly encoded in UTF-8
- backedhost = decode_incoming_string(backedhost)
+ hostname = decode_incoming_string(hostname)
# 3. Regular expression conversion
if "regex" in translation:
@@ -680,19 +688,19 @@ def translate_piggyback_host(sourcehost, backedhost):
if not expr.endswith('$'):
expr += '$'
rcomp = regex(expr)
- mo = rcomp.match(backedhost)
+ mo = rcomp.match(hostname)
if mo:
- backedhost = subst
+ hostname = subst
for nr, text in enumerate(mo.groups()):
- backedhost = backedhost.replace("\\%d" % (nr+1), text)
+ hostname = hostname.replace("\\%d" % (nr+1), text)
# 4. Explicity mapping
for from_host, to_host in translation.get("mapping", []):
- if from_host == backedhost:
- backedhost = to_host
+ if from_host == hostname:
+ hostname = to_host
break
- return backedhost.encode('utf-8') # change back to UTF-8 encoded string
+ return hostname.encode('utf-8') # change back to UTF-8 encoded string
def read_cache_file(relpath, max_cache_age):
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index e036031..adcadce 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -13451,6 +13451,10 @@ def Levels(**kwargs):
default_value = default_value,
)
+# When changing this keep it in sync with
+# a) check_mk_base.py: do_hostname_translation()
+# b) wato.py: do_hostname_translation()
+# FIXME TODO: Move the common do_hostname_translation() in a central Check_MK module
def HostnameTranslation(**kwargs):
help = kwargs.get("help")
title = kwargs.get("title")
@@ -13526,6 +13530,44 @@ def HostnameTranslation(**kwargs):
)),
])
+
+# When changing this keep it in sync with
+# a) check_mk_base.py: do_hostname_translation()
+# b) wato.py: do_hostname_translation()
+# FIXME TODO: Move the common do_hostname_translation() in a central Check_MK module
+def do_hostname_translation(translation, hostname):
+ # 1. Case conversion
+ caseconf = translation.get("case")
+ if caseconf == "upper":
+ hostname = hostname.upper()
+ elif caseconf == "lower":
+ hostname = hostname.lower()
+
+ # 2. Drop domain part (not applied to IP addresses!)
+ if translation.get("drop_domain") and not hostname[0].isdigit():
+ hostname = hostname.split(".", 1)[0]
+
+ # 3. Regular expression conversion
+ if "regex" in translation:
+ expr, subst = translation.get("regex")
+ if not expr.endswith('$'):
+ expr += '$'
+ rcomp = regex(expr)
+ mo = rcomp.match(hostname)
+ if mo:
+ hostname = subst
+ for nr, text in enumerate(mo.groups()):
+ hostname = hostname.replace("\\%d" % (nr+1), text)
+
+ # 4. Explicit mapping
+ for from_host, to_host in translation.get("mapping", []):
+ if from_host == hostname:
+ hostname = to_host
+ break
+
+ return hostname
+
+
#.
# .--User Profile--------------------------------------------------------.
# | _ _ ____ __ _ _ |
@@ -15133,16 +15175,24 @@ def do_network_scan_automation():
automation_commands["network-scan"] = do_network_scan_automation
+# FIXME/TODO: What happens in case of duplicate host names?
def add_scanned_hosts_to_folder(folder, found):
+ translation =
folder.attribute("network_scan").get("translate_names", {})
+
entries = []
for host_name, ipaddress in found:
+ host_name = do_hostname_translation(translation, host_name)
+
attrs = {
"ipaddress" : ipaddress,
"tag_criticality" : "offline",
}
entries.append((host_name, attrs, None))
+ lock_exclusive()
folder.create_hosts(entries)
+ folder.save()
+ unlock_exclusive()
def save_network_scan_result(folder, result):
@@ -15177,6 +15227,7 @@ def ip_addresses_to_scan(folder):
to_scan.difference_update(exclude)
# Reduce by all known host addresses
+ # FIXME/TODO: Shouldn't this filtering be done on the central site?
to_scan.difference_update(known_ip_addresses())
return to_scan
diff --git a/web/plugins/wato/builtin_attributes.py
b/web/plugins/wato/builtin_attributes.py
index b58d653..b6d9f87 100644
--- a/web/plugins/wato/builtin_attributes.py
+++ b/web/plugins/wato/builtin_attributes.py
@@ -179,6 +179,9 @@ class NetworkScanAttribute(ValueSpecAttribute):
choices = get_all_user_ids,
default_value = lambda: config.user_id,
)),
+ ("translate_names", HostnameTranslation(
+ title = _("Translate Hostnames"),
+ )),
],
title = _("Network Scan"),
help = _("For each folder an automatic network scan can be
configured. It will "
@@ -186,7 +189,7 @@ class NetworkScanAttribute(ValueSpecAttribute):
"to each IP address to check whether or not a host is using
this ip "
"address. Each new found host will be added to the current
folder by "
"it's hostname, when resolvable via DNS, or by it's
IP address."),
- optional_keys = ["max_parallel_pings"],
+ optional_keys = ["max_parallel_pings",
"translate_names"],
default_text = _("Not configured."),
)
)