def test_discovered_host_labels_add():
    labels_1 = DiscoveredHostLabels()
    labels_1.add_label(HostLabel(u"äbc", u"123", "plugin_1"))

    labels_2 = DiscoveredHostLabels()
    labels_2.add_label(HostLabel(u"xyz", u"blä", "plugin_2"))

    new_labels = labels_1 + labels_2
    assert new_labels.to_dict() == {
        u"äbc": {
            "value": u"123",
            "plugin_name": "plugin_1",
        },
        u"xyz": {
            "value": u"blä",
            "plugin_name": "plugin_2",
        },
    }

    labels_1 += labels_2
    assert labels_1.to_dict() == {
        u"äbc": {
            "value": u"123",
            "plugin_name": "plugin_1",
        },
        u"xyz": {
            "value": u"blä",
            "plugin_name": "plugin_2",
        },
    }
def test_discovered_host_labels_to_dict():
    labels = DiscoveredHostLabels()
    assert labels.to_dict() == {}

    labels.add_label(HostLabel(u"äbc", u"123", "plugin_1"))
    labels.add_label(HostLabel(u"xyz", u"blä", "plugin_2"))

    assert labels.to_dict() == {
        u"äbc": {
            "value": u"123",
            "plugin_name": "plugin_1",
        },
        u"xyz": {
            "value": u"blä",
            "plugin_name": "plugin_2",
        },
    }
def test_discovered_host_labels_store_save(discovered_host_labels_dir):
    store = DiscoveredHostLabelsStore("host")

    labels = DiscoveredHostLabels(HostLabel(u"xyz", u"äbc"))
    label_dict = labels.to_dict()

    assert not store.file_path.exists()  # pylint: disable=no-member

    store.save(label_dict)
    assert store.file_path.exists()  # pylint: disable=no-member
    assert store.load() == label_dict
Exemple #4
0
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]
    hostname = host_config.hostname

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

    node = inventory_tree.get_dict("software.applications.check_mk.cluster.")
    if host_config.is_cluster:
        node["is_cluster"] = True
        _do_inv_for_cluster(host_config, inventory_tree)
    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.normalize_nodes()
    old_timestamp = _save_inventory_tree(hostname, inventory_tree)
    _run_inventory_export_hooks(host_config, inventory_tree)

    success_msg = [
        "Found %s%s%d%s inventory entries" %
        (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:
        status_data_tree.normalize_nodes()
        _save_status_data_tree(hostname, status_data_tree)

        console.section_success("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