Module: check_mk
Branch: master
Commit: dfaf15d14a4e301c458cbd5a1fd33e60adbc8e3b
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=dfaf15d14a4e30…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Apr 8 16:29:21 2019 +0200
Add discovered labels to HW/SW inventory tree
* Add the discovered labels and the origin plugin names to the inventory
tree. This is done for debugging and transparency reasons.
* Added discovered_host_labels.add_label method for adding labels and
adding the label information to the HW/SW inventory tree in one step.
* Cleaned up tests on the way
CMK-1818
Change-Id: I07a92b6948650f4b5e0565d952d6d90819f7ce72
---
cmk/gui/plugins/views/inventory.py | 20 ++++++++++
cmk_base/discovered_host_labels.py | 17 ++++++++-
cmk_base/inventory.py | 2 +-
inventory/lnx_distro | 2 +-
tests/unit/cmk_base/test_discovered_host_labels.py | 43 +++++++++++++---------
5 files changed, 63 insertions(+), 21 deletions(-)
diff --git a/cmk/gui/plugins/views/inventory.py b/cmk/gui/plugins/views/inventory.py
index 4165f92..2eb922c 100644
--- a/cmk/gui/plugins/views/inventory.py
+++ b/cmk/gui/plugins/views/inventory.py
@@ -78,6 +78,7 @@ from cmk.gui.plugins.views import (
paint_age,
declare_1to1_sorter,
cmp_simple_number,
+ render_labels,
)
@@ -521,6 +522,11 @@ def inv_paint_csv_labels(csv_list):
@decorate_inv_paint
+def inv_paint_cmk_label(label):
+ return "labels", render_labels({label[0]: label[1]},
object_type="host", with_links=True)
+
+
+@decorate_inv_paint
def inv_paint_container_ready(ready):
if ready == 'yes':
css_class = "if_state_up"
@@ -1012,6 +1018,20 @@ inventory_displayhints.update({
"title": _("Cluster host"), "short":
_("Cluster"), "paint": "bool"
},
".software.applications.check_mk.cluster.nodes:": {"title":
_("Nodes")},
+ ".software.applications.check_mk.host_labels:": {
+ "title": _("Discovered host labels"),
+ "keyorder": [
+ "label",
+ "inventory_plugin_name",
+ ],
+ },
+ ".software.applications.check_mk.host_labels:*.label": {
+ "title": _("Label"),
+ "paint": "cmk_label",
+ },
+ ".software.applications.check_mk.host_labels:*.inventory_plugin_name": {
+ "title": _("Discovered by inventory plugin"),
+ },
".software.applications.docker.": {
"icon": "docker", "title": "Docker",
"keyorder": [
"version",
diff --git a/cmk_base/discovered_host_labels.py b/cmk_base/discovered_host_labels.py
index f61b13f..e697120 100644
--- a/cmk_base/discovered_host_labels.py
+++ b/cmk_base/discovered_host_labels.py
@@ -61,8 +61,9 @@ class DiscoveredHostLabelsStore(object):
class DiscoveredHostLabels(collections.MutableMapping, object):
"""Encapsulates the discovered labels of a single host during
runtime"""
- def __init__(self, **kwargs):
+ def __init__(self, inventory_tree, **kwargs):
super(DiscoveredHostLabels, self).__init__()
+ self._inventory_tree = inventory_tree
self._labels = kwargs
def is_empty(self):
@@ -85,3 +86,17 @@ class DiscoveredHostLabels(collections.MutableMapping, object):
def to_dict(self):
return self._labels
+
+ # TODO: Once we redesign the hw/sw inventory plugin API check if we can move it to
the
+ # inventory API.
+ def add_label(self, key, value, plugin_name):
+ """Add a label to the collection of discovered labels and
inventory tree
+
+ Add it to the inventory tree for debugging purposes
+ """
+ self[key] = value
+ labels =
self._inventory_tree.get_list("software.applications.check_mk.host_labels:")
+ labels.append({
+ "label": (key, value),
+ "inventory_plugin_name": plugin_name,
+ })
diff --git a/cmk_base/inventory.py b/cmk_base/inventory.py
index 3fb4ddb..4090ec7 100644
--- a/cmk_base/inventory.py
+++ b/cmk_base/inventory.py
@@ -187,7 +187,7 @@ def _do_inv_for(sources, multi_host_sections, hostname, ipaddress,
do_status_dat
_initialize_inventory_tree()
inventory_tree = g_inv_tree
status_data_tree = StructuredDataTree()
- discovered_host_labels = DiscoveredHostLabels()
+ discovered_host_labels = DiscoveredHostLabels(inventory_tree)
node = inventory_tree.get_dict("software.applications.check_mk.cluster.")
if config.is_cluster(hostname):
diff --git a/inventory/lnx_distro b/inventory/lnx_distro
index 7086ee9..410ad21 100644
--- a/inventory/lnx_distro
+++ b/inventory/lnx_distro
@@ -64,7 +64,7 @@ def inv_lnx_distro(info, inventory_tree, discovered_host_labels):
break
if "name" in node:
- discovered_host_labels["os"] = node["name"]
+ discovered_host_labels.add_label("os", node["name"],
plugin_name="lnx_distro")
def _parse_lnx_distro(info):
diff --git a/tests/unit/cmk_base/test_discovered_host_labels.py
b/tests/unit/cmk_base/test_discovered_host_labels.py
index 32c2070..4c7a171 100644
--- a/tests/unit/cmk_base/test_discovered_host_labels.py
+++ b/tests/unit/cmk_base/test_discovered_host_labels.py
@@ -4,30 +4,31 @@
import collections
import pytest # type: ignore
import cmk.utils.paths
+from cmk.utils.structured_data import StructuredDataTree
from cmk_base.discovered_host_labels import DiscoveredHostLabels,
DiscoveredHostLabelsStore
-def test_discovered_host_labels_type():
- labels = DiscoveredHostLabels()
+(a)pytest.fixture()
+def labels():
+ return DiscoveredHostLabels(StructuredDataTree())
+
+
+def test_discovered_host_labels_type(labels):
assert isinstance(labels, collections.MutableMapping)
-def test_discovered_host_labels_is_empty():
- labels = DiscoveredHostLabels()
+def test_discovered_host_labels_is_empty(labels):
assert labels.is_empty()
labels["abc"] = "123"
assert not labels.is_empty()
-def test_discovered_host_labels_getitem():
- labels = DiscoveredHostLabels()
+def test_discovered_host_labels_getitem(labels):
labels["abc"] = "123"
assert labels["abc"] == "123"
-def test_discovered_host_labels_delitem():
- labels = DiscoveredHostLabels()
-
+def test_discovered_host_labels_delitem(labels):
labels["abc"] = "123"
assert "abc" in labels
@@ -35,9 +36,7 @@ def test_discovered_host_labels_delitem():
assert "abc" not in labels
-def test_discovered_host_labels_iter():
- labels = DiscoveredHostLabels()
-
+def test_discovered_host_labels_iter(labels):
labels["abc"] = "123"
labels["xyz"] = "bla"
@@ -45,8 +44,7 @@ def test_discovered_host_labels_iter():
assert sorted(list(labels.iterkeys())) == ["abc", "xyz"]
-def test_discovered_host_labels_len():
- labels = DiscoveredHostLabels()
+def test_discovered_host_labels_len(labels):
assert len(labels) == 0
labels["abc"] = "123"
@@ -55,8 +53,7 @@ def test_discovered_host_labels_len():
assert len(labels) == 2
-def test_discovered_host_labels_to_dict():
- labels = DiscoveredHostLabels()
+def test_discovered_host_labels_to_dict(labels):
assert labels.to_dict() == {}
labels["äbc"] = "123"
@@ -68,6 +65,17 @@ def test_discovered_host_labels_to_dict():
}
+def test_discovered_host_add_label(labels):
+ labels.add_label("abc", "123", plugin_name="xyz")
+ assert labels["abc"] == "123"
+ inventory_labels = labels._inventory_tree.get_list(
+ "software.applications.check_mk.host_labels:")
+ assert inventory_labels[0] == {
+ 'inventory_plugin_name': 'xyz',
+ 'label': ('abc', '123'),
+ }
+
+
@pytest.fixture()
def discovered_labels_dir(tmp_path, monkeypatch):
path = tmp_path / "var" / "check_mk" /
"discovered_host_labels"
@@ -79,9 +87,8 @@ def test_discovered_host_labels_store_file_path(discovered_labels_dir):
assert DiscoveredHostLabelsStore("host").file_path == discovered_labels_dir
/ "host.mk"
-def test_discovered_host_labels_store_save(discovered_labels_dir):
+def test_discovered_host_labels_store_save(labels, discovered_labels_dir):
store = DiscoveredHostLabelsStore("host")
- labels = DiscoveredHostLabels()
labels["xyz"] = "äbc"
label_dict = labels.to_dict()