Module: check_mk
Branch: master
Commit: 0aa06ba3a16818fcccceea6918ad7152f5fbd5ac
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0aa06ba3a16818…
Author: Simon Betz <si(a)mathias-kettner.de>
Date: Tue Sep 18 09:16:52 2018 +0200
6595 FIX if.include: Recognize duplicate interface items correctly
The recognition works correct but not in the following situation
where the wrong network interface data is checked.
<ul>
<li><tt>Use desciption</tt> or <tt>use alias</tt> is
configured in the
ruleset <tt>Network Interface and Switch Port Discovery</tt>.</li>
<li>Two or more network interfaces exist with same description or alias.</li>
<li>Only one network interface with this description or alias is
discovered.</li>
<li>The discovered network interface does not rank first.</li>
</ul>
Now the index is attached if an alias or description exists at least twice.
Change-Id: I996a820796234ad6e571f8bcffc51a024af647c1
---
.werks/6595 | 21 +++++++++++++++++++
checks/if.include | 23 +++++++++++++++------
tests/unit/checks/test_if_check.py | 41 ++++++++++++++++++++++++--------------
3 files changed, 64 insertions(+), 21 deletions(-)
diff --git a/.werks/6595 b/.werks/6595
new file mode 100644
index 0000000..302570c
--- /dev/null
+++ b/.werks/6595
@@ -0,0 +1,21 @@
+Title: if.include: Recognize duplicate interface items correctly
+Level: 1
+Component: checks
+Class: fix
+Compatible: incomp
+Edition: cre
+State: unknown
+Version: 1.6.0i1
+Date: 1537254518
+
+The recognition works correct but not in the following situation
+where the wrong network interface data is checked.
+<ul>
+<li><tt>Use desciption</tt> or <tt>use alias</tt> is
configured in the
+ruleset <tt>Network Interface and Switch Port Discovery</tt>.</li>
+<li>Two or more network interfaces exist with same description or
alias.</li>
+<li>Only one network interface with this description or alias is
discovered.</li>
+<li>The discovered network interface does not rank first.</li>
+</ul>
+
+Now the index is attached if an alias or description exists at least twice.
diff --git a/checks/if.include b/checks/if.include
index 84ada9c..abb3834 100644
--- a/checks/if.include
+++ b/checks/if.include
@@ -290,6 +290,8 @@ def inventory_if_common(info, has_nodeinfo = False):
settings_sets = host_extra_conf(host_name(), inventory_if_rules)
seen_indicies = set()
pre_inventory = []
+ global_seen_items = set()
+ duplicates = set()
if not settings_sets:
# if there are no settings, use defaults
@@ -315,6 +317,7 @@ def inventory_if_common(info, has_nodeinfo = False):
match_desc = settings.get('match_desc')
pad_portnumbers = settings.get('pad_portnumbers',
if_inventory_pad_portnumbers)
+
# Allow main check to set no port type (e.g. hitachi_hnas_fc_if)
porttypes.append("")
have_groups = {}
@@ -356,6 +359,11 @@ def inventory_if_common(info, has_nodeinfo = False):
else:
item = if_pad_with_zeroes(info, ifIndex, has_nodeinfo, pad_portnumbers)
+ if item not in global_seen_items:
+ global_seen_items.add(item)
+ else:
+ duplicates.add(item)
+
current_host_name = host_name()
# Prepare grouped interfaces
@@ -420,7 +428,12 @@ def inventory_if_common(info, has_nodeinfo = False):
if ifSpeed != "" and if_inventory_monitor_speed:
params["speed"] = int(ifSpeed)
seen_indicies.add(ifIndex)
- pre_inventory.append((item, "%r" % params, int(ifIndex)))
+
+ try:
+ index_as_item = int(item) == int(ifIndex)
+ except (TypeError, ValueError):
+ index_as_item = False
+ pre_inventory.append((item, "%r" % params, int(ifIndex),
index_as_item))
# Create pseudo interfaces out of groups
# On cluster groups we have no 'interfaces' key
@@ -458,14 +471,12 @@ def inventory_if_common(info, has_nodeinfo = False):
params["speed"] = total_speed
# Note: the group interface index is always set to 1
- pre_inventory.append((group_name, "%r" % params, 1))
+ pre_inventory.append((group_name, "%r" % params, 1, False))
inventory = []
- s = set()
- duplicates = set(x[0] for x in pre_inventory if x[0] in s or s.add(x[0]))
# Check for duplicate items (e.g. when using Alias as item and the alias is not
unique)
- for item, params, index in pre_inventory:
- if item in duplicates:
+ for item, params, index, index_as_item in pre_inventory:
+ if not index_as_item and item in duplicates:
new_item = "%s %d" % (item, index)
else:
new_item = item
diff --git a/tests/unit/checks/test_if_check.py b/tests/unit/checks/test_if_check.py
index 90ffd8d..f9223c6 100644
--- a/tests/unit/checks/test_if_check.py
+++ b/tests/unit/checks/test_if_check.py
@@ -30,20 +30,31 @@ pytestmark = pytest.mark.checks
# ifPhysAddress
-(a)pytest.mark.parametrize("info,inventory_if_rules,result"sult", [
- ([], [], 0),
- ([20*[""]], [], 0),
- ([20*["0"]], [], 0),
- ([21*[""]], [], 0),
- ([21*["0"]], [], 0),
- ([["1", "EINS", "6", "10", "1"] +
13*["0"] + ["EINS-ALIAS", "00:00:00:00:00:00"]], [], 1),
- ([["1", "EINS", "6", "10", "2"] +
13*["0"] + ["EINS-ALIAS", "00:00:00:00:00:00"]], [], 0),
- ([["1", "EINS", "00000", "10", "1"]
+ 13*["0"] + ["EINS-ALIAS", "00:00:00:00:00:00"]], [], 0),
- ([["1", "EINS", "6", "10", "1"] +
13*["0"] + ["EINS-ALIAS", "00:00:00:00:00:00"],
- ["1", "EINS-DUPLICATE", "6", "10",
"1"] + 13*["0",] + ["EINS-ALIAS-DUPLICATE",
"00:00:00:00:00:00"]], [], 1),
+(a)pytest.mark.parametrize("info,settings,items"tems", [
+ ([], [], []),
+ ([20*[""]], [], []),
+ ([20*["0"]], [], []),
+ ([21*[""]], [], []),
+ ([21*["0"]], [], []),
+ ([["1", "FOO", "6", "10", "1"] +
13*["0"] + ["FOO-ALIAS", "00:00:00:00:00:00"],
+ ["1", "FOO-DUPLICATE", "6", "10",
"1"] + 13*["0"] + ["FOO-ALIAS-DUPLICATE",
"00:00:00:00:00:00"]],
+ [], ["1"]),
+ ([["1", "FOO", "6", "10", "1"] +
13*["0"] + ["FOO-ALIAS", "00:00:00:00:00:00"],
+ ["2", "BAR", "6", "10", "1"] +
13*["0"] + ["BAR-ALIAS", "00:00:00:00:00:00"]],
+ [], ["1", "2"]),
+ ([["1", "FOO", "6", "10", "1"] +
13*["0"] + ["FOO-ALIAS", "00:00:00:00:00:00"],
+ ["2", "BAR", "6", "10", "1"] +
13*["0"] + ["BAR-ALIAS", "00:00:00:00:00:00"]],
+ [{"use_desc": True}], ["FOO", "BAR"]),
+ ([["1", "FOO", "6", "10", "1"] +
13*["0"] + ["FOO-ALIAS", "00:00:00:00:00:00"],
+ ["2", "FOO", "6", "10", "1"] +
13*["0"] + ["FOO-ALIAS", "00:00:00:00:00:00"]],
+ [{"use_desc": True}], ["FOO 1", "FOO 2"]),
+ ([["1", "FOO", "6", "10", "2"] +
13*["0"] + ["FOO-ALIAS", "00:00:00:00:00:00"],
+ ["2", "FOO", "6", "10", "1"] +
13*["0"] + ["FOO-ALIAS", "00:00:00:00:00:00"]],
+ [{"use_desc": True}], ["FOO 2"]),
])
-def test_if_inventory_if_common_count_interfaces(check_manager, monkeypatch, info,
inventory_if_rules, result):
+def test_if_inventory_if_common_discovered_items(check_manager, monkeypatch, info,
settings, items):
check = check_manager.get_check("if")
- #TODO How to handle several "host_extra_conf"s?
- #monkeypatch.setitem(check.context, "host_extra_conf", lambda _, __:
inventory_if_rules)
- assert len(check.run_discovery(info)) == result
+ monkeypatch.setitem(check.context, "host_extra_conf", lambda _, __:
settings)
+ monkeypatch.setitem(check.context, "_prepare_if_group_patterns_from_conf",
lambda: {})
+ discovered_items = [e[0] for e in check.run_discovery(info)]
+ assert discovered_items == items