Пример #1
0
    def _collect_infos(self,
                       collectors: Collectors) -> DiagnosticsElementJSONResult:
        checkmk_server_name = collectors.get_checkmk_server_name()
        if checkmk_server_name is None:
            raise DiagnosticsElementError("No Checkmk server found")

        inventory_store = StructuredDataStore(
            Path(cmk.utils.paths.inventory_output_dir))
        try:
            tree = inventory_store.load(host_name=checkmk_server_name)
        except FileNotFoundError:
            raise DiagnosticsElementError(
                "No HW/SW inventory tree of '%s' found" % checkmk_server_name)

        infos = {}
        attrs = tree.get_attributes(["software", "applications", "check_mk"])
        if attrs:
            infos.update(attrs.serialize())

        node = tree.get_node(["software", "applications", "check_mk"])
        if node:
            infos.update(node.serialize())

        if not infos:
            raise DiagnosticsElementError(
                "No HW/SW inventory node 'Software > Applications > Checkmk'")
        return infos
Пример #2
0
def _load_structured_data_tree(
        tree_type: Literal["inventory", "status_data"],
        hostname: Optional[HostName]) -> Optional[StructuredDataNode]:
    """Load data of a host, cache it in the current HTTP request"""
    if not hostname:
        return None

    inventory_tree_cache = g.setdefault(tree_type, {})
    if hostname in inventory_tree_cache:
        inventory_tree = inventory_tree_cache[hostname]
    else:
        if '/' in hostname:
            # just for security reasons
            return None

        tree_store = StructuredDataStore(
            Path(cmk.utils.paths.inventory_output_dir) if tree_type ==
            "inventory" else Path(cmk.utils.paths.status_data_dir))

        try:
            inventory_tree = tree_store.load(host_name=hostname)
        except Exception as e:
            if config.debug:
                html.show_warning("%s" % e)
            raise LoadStructuredDataError()
        inventory_tree_cache[hostname] = inventory_tree
    return inventory_tree
Пример #3
0
def test_real_is_equal_save_and_load(tree, tmp_path):
    store = StructuredDataStore(tmp_path)
    try:
        store.save(host_name=HostName("foo"), tree=tree)
        loaded_tree = store.load(host_name=HostName("foo"))
        assert tree.is_equal(loaded_tree)
    finally:
        shutil.rmtree(str(tmp_path))
Пример #4
0
    def _load_tree_from_file(self, filepath: Path) -> StructuredDataNode:
        try:
            tree = _filter_tree(StructuredDataStore.load_file(filepath))
        except FileNotFoundError:
            raise LoadStructuredDataError()

        if tree is None or tree.is_empty():
            # load_file may return an empty tree
            raise LoadStructuredDataError()

        return tree
Пример #5
0
def test_real_save_gzip(tmp_path):
    host_name = HostName("heute")
    target = tmp_path / str(host_name)
    raw_tree = {
        "node": {
            "foo": 1,
            "bär": 2,
        },
    }
    tree = StructuredDataNode.deserialize(raw_tree)
    store = StructuredDataStore(tmp_path)
    store.save(host_name=host_name, tree=tree)

    assert target.exists()

    gzip_filepath = target.with_suffix(".gz")
    assert gzip_filepath.exists()

    with gzip.open(str(gzip_filepath), "rb") as f:
        f.read()
Пример #6
0
def do_inventory_actions_during_checking_for(
    config_cache: config.ConfigCache,
    host_config: config.HostConfig,
    ipaddress: Optional[HostAddress],
    *,
    parsed_sections_broker: ParsedSectionsBroker,
) -> None:

    status_data_store = StructuredDataStore(
        Path(cmk.utils.paths.status_data_dir))

    if not host_config.do_status_data_inventory:
        # includes cluster case
        status_data_store.remove_files(host_name=host_config.hostname)
        return  # nothing to do here

    trees = _do_inv_for_realhost(
        host_config,
        ipaddress,
        parsed_sections_broker=parsed_sections_broker,
        run_plugin_names=EVERYTHING,
        retentions_tracker=RetentionsTracker([]),
    )
    if trees.status_data and not trees.status_data.is_empty():
        status_data_store.save(host_name=host_config.hostname,
                               tree=trees.status_data)
Пример #7
0
def _save_inventory_tree(
    hostname: HostName,
    inventory_tree: StructuredDataNode,
    retentions: Retentions,
) -> Optional[StructuredDataNode]:

    inventory_store = StructuredDataStore(cmk.utils.paths.inventory_output_dir)

    if inventory_tree.is_empty():
        # Remove empty inventory files. Important for host inventory icon
        inventory_store.remove_files(host_name=hostname)
        return None

    old_tree = inventory_store.load(host_name=hostname)
    update_result = retentions.may_update(int(time.time()), old_tree)

    if old_tree.is_empty():
        console.verbose("New inventory tree.\n")

    elif not old_tree.is_equal(inventory_tree):
        console.verbose("Inventory tree has changed. Add history entry.\n")
        inventory_store.archive(
            host_name=hostname,
            archive_dir=cmk.utils.paths.inventory_archive_dir,
        )

    elif update_result.save_tree:
        console.verbose(
            "Update inventory tree%s.\n"
            % (" (%s)" % update_result.reason if update_result.reason else "")
        )
    else:
        console.verbose(
            "Inventory tree not updated%s.\n"
            % (" (%s)" % update_result.reason if update_result.reason else "")
        )
        return None

    inventory_store.save(host_name=hostname, tree=inventory_tree)
    return old_tree
Пример #8
0
    def get_tree(timestamp: Optional[str]) -> StructuredDataNode:
        if timestamp is None:
            return StructuredDataNode()

        if timestamp in tree_lookup:
            return tree_lookup[timestamp]

        if timestamp == latest_timestamp:
            inventory_tree = load_filtered_inventory_tree(hostname)
            if inventory_tree is None:
                raise LoadStructuredDataError()
            tree_lookup[timestamp] = inventory_tree
        else:
            inventory_archive_path = Path(inventory_archive_dir, timestamp)
            tree_lookup[timestamp] = _filter_tree(
                StructuredDataStore.load_file(inventory_archive_path))
        return tree_lookup[timestamp]
Пример #9
0
    assert filtered_node.get_node(["path", "to", "nta", "nt"]) is None
    assert filtered_node.get_node(["path", "to", "nta", "na"]) is None

    assert filtered_node.get_node(["path", "to", "another",
                                   "node1"]) is not None
    assert filtered_node.get_node(["path", "to", "another",
                                   "node2"]) is not None


# Tests with real host data

TEST_DIR = "%s/tests/unit/cmk/utils/structured_data/tree_test_data" % cmk_path(
)

TEST_DATA_STORE = StructuredDataStore(Path(TEST_DIR))

tree_name_old_addresses_arrays_memory = HostName(
    "tree_old_addresses_arrays_memory")
tree_name_old_addresses = HostName("tree_old_addresses")
tree_name_old_arrays = HostName("tree_old_arrays")
tree_name_old_interfaces = HostName("tree_old_interfaces")
tree_name_old_memory = HostName("tree_old_memory")
tree_name_old_heute = HostName("tree_old_heute")

tree_name_new_addresses_arrays_memory = HostName(
    "tree_new_addresses_arrays_memory")
tree_name_new_addresses = HostName("tree_new_addresses")
tree_name_new_arrays = HostName("tree_new_arrays")
tree_name_new_interfaces = HostName("tree_new_interfaces")
tree_name_new_memory = HostName("tree_new_memory")