Skip to content
Permalink
Browse files

Remove host label discovery from HW/SW inventory

The host label discovery will also be done by the discovery function of
the checks in the future. The just removed functionality will be added
back soon. See following commits for details.

CMK-2458

Change-Id: I9921f475b576fb4886773fa24c8112c6977d146f
  • Loading branch information...
LarsMichelsen committed Jul 30, 2019
1 parent 1a3cd55 commit 40e564f0741c7fbe1773b00f8e23f804c7c08d26
@@ -118,17 +118,6 @@ def valuespec(self):
],
default_value=True,
)),
("host_label_inventory",
DropdownChoice(
title=_("Host label discovery"),
help=_("All hosts configured via this ruleset will try to find new "
"host labels during every check cycle."),
choices=[
(True, _("Do automatic host label discovery")),
(False, _("Do not perform automatic host label discovery")),
],
default_value=True,
)),
]),
title=_("Do hardware/software Inventory"),
help=_("All hosts configured via this ruleset will do a hardware and "
@@ -2553,25 +2553,6 @@ def do_status_data_inventory(self):

return params.get('status_data_inventory', False)

@property
def do_host_label_discovery(self):
# type: () -> bool

# TODO: Use dict(self.active_checks).get("cmk_inv", [])?
rules = active_checks.get('cmk_inv')
if rules is None:
return True

entries = self._config_cache.host_extra_conf(self.hostname, rules)

if not entries:
return True # No matching rule -> disable

# Convert legacy rules to current dict format (just like the valuespec)
params = {} if entries[0] is None else entries[0]

return params.get("host_label_inventory", True)

@property
def service_level(self):
# type: () -> Optional[int]
@@ -77,18 +77,6 @@ def add_label(self, label):
self._labels[label.name] = label.value
self._plugin_name[label.name] = label.plugin_name

# TODO: Once we redesign the hw/sw inventory plugin API check if we can
# move it to the inventory API.
def add_labels_to_inventory_tree(self, inventory_tree):
"""Add a label + plugin to the inventory tree
"""
inv_labels = inventory_tree.get_list("software.applications.check_mk.host_labels:")
for label_id, label_value in self._labels.iteritems():
inv_labels.append({
"label": (label_id, label_value),
"plugin_name": self._plugin_name[label_id],
})


class ABCLabel(object):
"""Representing a service label in Checkmk
@@ -38,7 +38,6 @@
import cmk.utils.tty as tty
from cmk.utils.exceptions import MKGeneralException
from cmk.utils.structured_data import StructuredDataTree
from cmk.utils.labels import DiscoveredHostLabelsStore
import cmk.utils.debug

import cmk_base.utils
@@ -51,7 +50,6 @@
import cmk_base.cleanup
import cmk_base.decorator
import cmk_base.check_api as check_api
from cmk_base.discovered_labels import DiscoveredHostLabels

#.
# .--Inventory-----------------------------------------------------------.
@@ -118,16 +116,15 @@ def do_inv_check(hostname, options):
status, infotexts, long_infotexts, perfdata = 0, [], [], []

sources = data_sources.DataSources(hostname, ipaddress)
old_timestamp, inventory_tree, status_data_tree, discovered_host_labels = _do_inv_for(
old_timestamp, inventory_tree, status_data_tree = _do_inv_for(
sources,
multi_host_sections=None,
host_config=host_config,
ipaddress=ipaddress,
do_status_data_inv=host_config.do_status_data_inventory,
)

if (inventory_tree.is_empty() and status_data_tree.is_empty() and
discovered_host_labels.is_empty()):
if inventory_tree.is_empty() and status_data_tree.is_empty():
infotexts.append("Found no data")

else:
@@ -160,9 +157,6 @@ def do_inv_check(hostname, options):
if not status_data_tree.is_empty():
infotexts.append("Found %s status entries" % status_data_tree.count_entries())

if not discovered_host_labels.is_empty():
infotexts.append("Found %s host labels" % len(discovered_host_labels))

for source in sources.get_data_sources():
source_state, source_output, _source_perfdata = source.get_summary_result_for_inventory()
# Do not output informational (state = 0) things. These information are shown by the "Check_MK" service
@@ -181,7 +175,7 @@ def do_inventory_actions_during_checking_for(sources, multi_host_sections, host_
if not do_status_data_inventory:
_cleanup_status_data(hostname)

if not do_status_data_inventory and not host_config.do_host_label_discovery:
if not do_status_data_inventory:
return # nothing to do here

# This is called during checking, but the inventory plugins are not loaded yet
@@ -210,13 +204,12 @@ def _cleanup_status_data(hostname):


def _do_inv_for(sources, multi_host_sections, host_config, ipaddress, do_status_data_inv):
# type: (data_sources.DataSources, data_sources.MultiHostSections, config.HostConfig, Optional[str], bool) -> Tuple[Optional[float], StructuredDataTree, StructuredDataTree, DiscoveredHostLabels]
# type: (data_sources.DataSources, data_sources.MultiHostSections, config.HostConfig, Optional[str], bool) -> Tuple[Optional[float], StructuredDataTree, StructuredDataTree]
hostname = host_config.hostname

_initialize_inventory_tree()
inventory_tree = g_inv_tree
status_data_tree = StructuredDataTree()
discovered_host_labels = DiscoveredHostLabels()

node = inventory_tree.get_dict("software.applications.check_mk.cluster.")
if host_config.is_cluster:
@@ -225,9 +218,8 @@ def _do_inv_for(sources, multi_host_sections, host_config, ipaddress, do_status_
else:
node["is_cluster"] = False
_do_inv_for_realhost(host_config, sources, multi_host_sections, hostname, ipaddress,
inventory_tree, status_data_tree, discovered_host_labels)
inventory_tree, status_data_tree)

discovered_host_labels.add_labels_to_inventory_tree(inventory_tree)
inventory_tree.normalize_nodes()
old_timestamp = _save_inventory_tree(hostname, inventory_tree)
_run_inventory_export_hooks(host_config, inventory_tree)
@@ -237,11 +229,6 @@ def _do_inv_for(sources, multi_host_sections, host_config, ipaddress, do_status_
(tty.bold, tty.yellow, inventory_tree.count_entries(), tty.normal)
]

if host_config.do_host_label_discovery:
DiscoveredHostLabelsStore(hostname).save(discovered_host_labels.to_dict())
success_msg.append("and %s%s%d%s host labels" %
(tty.bold, tty.yellow, len(discovered_host_labels), tty.normal))

console.section_success(", ".join(success_msg))

if do_status_data_inv:
@@ -252,7 +239,7 @@ def _do_inv_for(sources, multi_host_sections, host_config, ipaddress, do_status_
"Found %s%s%d%s status entries" %
(tty.bold, tty.yellow, status_data_tree.count_entries(), tty.normal))

return old_timestamp, inventory_tree, status_data_tree, discovered_host_labels
return old_timestamp, inventory_tree, status_data_tree


def _do_inv_for_cluster(host_config, inventory_tree):
@@ -268,7 +255,7 @@ def _do_inv_for_cluster(host_config, inventory_tree):


def _do_inv_for_realhost(host_config, sources, multi_host_sections, hostname, ipaddress,
inventory_tree, status_data_tree, discovered_host_labels):
inventory_tree, status_data_tree):
for source in sources.get_data_sources():
if isinstance(source, data_sources.SNMPDataSource):
source.set_on_error("raise")
@@ -321,7 +308,6 @@ def _do_inv_for_realhost(host_config, sources, multi_host_sections, hostname, ip
for dynamic_arg_name, dynamic_arg_value in [
("inventory_tree", inventory_tree),
("status_data_tree", status_data_tree),
("discovered_host_labels", discovered_host_labels),
]:
if dynamic_arg_name in inv_function_args:
inv_function_args.remove(dynamic_arg_name)

This file was deleted.

@@ -44,7 +44,7 @@
# /etc/oracle-release|Oracle LinuxServer release 7.1


def inv_lnx_distro(info, inventory_tree, discovered_host_labels):
def inv_lnx_distro(info, inventory_tree):
parsed = _parse_lnx_distro(info)

node = inventory_tree.get_dict("software.os.")
@@ -63,10 +63,6 @@ def inv_lnx_distro(info, inventory_tree, discovered_host_labels):
handler(node, parsed[file_name])
break

if "name" in node:
discovered_host_labels.add_label(
HostLabel(u"os", unicode(node["name"]), discovered_by=("inventory", "lnx_distro")),) # pylint: disable=undefined-variable


def _parse_lnx_distro(info):
parsed = {}
@@ -1567,28 +1567,6 @@ def test_host_config_do_status_data_inventory(monkeypatch, result, ruleset):
assert config_cache.get_host_config("abc").do_status_data_inventory == result


@pytest.mark.parametrize("result,ruleset", [
(True, None),
(True, []),
(True, [(None, [], config.ALL_HOSTS, {})]),
(True, [({}, [], config.ALL_HOSTS, {})]),
(True, [({
"host_label_inventory": True
}, [], config.ALL_HOSTS, {})]),
(False, [({
"host_label_inventory": False
}, [], config.ALL_HOSTS, {})]),
])
def test_host_config_do_host_label_discovery_for(monkeypatch, result, ruleset):
ts = Scenario().add_host("abc")
ts.set_option("active_checks", {
"cmk_inv": ruleset,
})
config_cache = ts.apply(monkeypatch)

assert config_cache.get_host_config("abc").do_host_label_discovery == result


@pytest.mark.parametrize("hostname,result", [
("testhost1", None),
("testhost2", 10),
@@ -96,21 +96,6 @@ def test_discovered_host_labels_store_save(discovered_host_labels_dir):
assert store.load() == label_dict


def test_discovered_host_labels_store_to_inventory_tree(discovered_host_labels_dir):
labels = DiscoveredHostLabels(HostLabel(u"xyz", u"äbc", plugin_name="plugin_name_1"))

tree = StructuredDataTree()
labels.add_labels_to_inventory_tree(tree)

inv_labels = tree.get_list("software.applications.check_mk.host_labels:")
assert inv_labels == [
{
"label": (u"xyz", u"äbc"),
"plugin_name": "plugin_name_1"
},
]


def test_service_label():
name, value = u"äbc", u"d{--lulu--}dd"
l = ServiceLabel(name, value)

0 comments on commit 40e564f

Please sign in to comment.
You can’t perform that action at this time.