def _do_inv_for( config_cache: config.ConfigCache, host_config: config.HostConfig, ipaddress: Optional[HostAddress], *, sources: data_sources.DataSources, multi_host_sections: Optional[MultiHostSections], ) -> Tuple[StructuredDataTree, StructuredDataTree]: hostname = host_config.hostname _initialize_inventory_tree() inventory_tree = g_inv_tree status_data_tree = StructuredDataTree() 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( config_cache, host_config, sources, multi_host_sections, hostname, ipaddress, inventory_tree, status_data_tree, ) inventory_tree.normalize_nodes() status_data_tree.normalize_nodes() return inventory_tree, status_data_tree
def _save_inventory_tree( hostname: HostName, inventory_tree: StructuredDataTree, ) -> Optional[StructuredDataTree]: store.makedirs(cmk.utils.paths.inventory_output_dir) filepath = cmk.utils.paths.inventory_output_dir + "/" + hostname if inventory_tree.is_empty(): # Remove empty inventory files. Important for host inventory icon if os.path.exists(filepath): os.remove(filepath) if os.path.exists(filepath + ".gz"): os.remove(filepath + ".gz") return None old_tree = StructuredDataTree().load_from(filepath) old_tree.normalize_nodes() if old_tree.is_equal(inventory_tree): console.verbose("Inventory was unchanged\n") return None if old_tree.is_empty(): console.verbose("New inventory tree\n") else: console.verbose("Inventory tree has changed\n") old_time = os.stat(filepath).st_mtime arcdir = "%s/%s" % (cmk.utils.paths.inventory_archive_dir, hostname) store.makedirs(arcdir) os.rename(filepath, arcdir + ("/%d" % old_time)) inventory_tree.save_to(cmk.utils.paths.inventory_output_dir, hostname) return old_tree
def test_structured_data_StructuredDataTree_building_tree(): def plugin_dict(): node = struct_tree.get_dict("level0_0.level1_dict.") for a, b in [("d1", "D1"), ("d2", "D2")]: node.setdefault(a, b) def plugin_list(): node = struct_tree.get_list("level0_1.level1_list:") for a, b in [("l1", "L1"), ("l2", "L2")]: node.append({a: b}) def plugin_nested_list(): node = struct_tree.get_list("level0_2.level1_nested_list:") for index in range(10): array: Dict[str, List[Dict[str, str]]] = {"foo": []} for a, b in [("nl1", "NL1"), ("nl2", "NL2")]: array["foo"].append({a: "%s-%s" % (b, index)}) node.append(array) struct_tree = StructuredDataTree() plugin_dict() plugin_list() plugin_nested_list() struct_tree.normalize_nodes() assert struct_tree.has_edge("level0_0") assert struct_tree.has_edge("level0_1") assert struct_tree.has_edge("level0_2") assert not struct_tree.has_edge("foobar") level1_dict = struct_tree.get_sub_attributes(["level0_0", "level1_dict"]) level1_list = struct_tree.get_sub_numeration(["level0_1", "level1_list"]) level1_nested_list_con = struct_tree.get_sub_container( ["level0_2", "level1_nested_list"]) level1_nested_list_num = struct_tree.get_sub_numeration( ["level0_2", "level1_nested_list"]) level1_nested_list_att = struct_tree.get_sub_attributes( ["level0_2", "level1_nested_list"]) assert isinstance(level1_dict, Attributes) assert 'd1' in level1_dict.get_child_data() assert 'd2' in level1_dict.get_child_data() assert isinstance(level1_list, Numeration) known_keys = [key for row in level1_list.get_child_data() for key in row] assert 'l1' in known_keys assert 'l2' in known_keys assert level1_nested_list_num is None assert level1_nested_list_att is None assert isinstance(level1_nested_list_con, Container) assert list( level1_nested_list_con._nodes) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def _do_inv_for_cluster(host_config: config.HostConfig) -> InventoryTrees: inventory_tree = StructuredDataTree() _set_cluster_property(inventory_tree, host_config) if not host_config.nodes: return InventoryTrees(inventory_tree, StructuredDataTree()) inv_node = inventory_tree.get_list("software.applications.check_mk.cluster.nodes:") for node_name in host_config.nodes: inv_node.append({ "name": node_name, }) inventory_tree.normalize_nodes() return InventoryTrees(inventory_tree, StructuredDataTree())
def test_structured_data_StructuredDataTree_building_tree(): def plugin_dict(): node = struct_tree.get_dict("level0_0.level1_dict.") for a, b in [("d1", "D1"), ("d2", "D2")]: node.setdefault(a, b) def plugin_list(): node = struct_tree.get_list("level0_1.level1_list:") for a, b in [("l1", "L1"), ("l2", "L2")]: node.append({a: b}) def plugin_nested_list(): node = struct_tree.get_list("level0_2.level1_nested_list:") for index in xrange(10): array = {"foo": []} for a, b in [("nl1", "NL1"), ("nl2", "NL2")]: array["foo"].append({a: "%s-%s" % (b, index)}) node.append(array) struct_tree = StructuredDataTree() plugin_dict() plugin_list() plugin_nested_list() struct_tree.normalize_nodes() assert struct_tree.has_edge("level0_0") assert struct_tree.has_edge("level0_1") assert struct_tree.has_edge("level0_2") assert not struct_tree.has_edge("foobar") level1_dict = struct_tree.get_sub_attributes(["level0_0", "level1_dict"]) level1_list = struct_tree.get_sub_numeration(["level0_1", "level1_list"]) level1_nested_list_con = struct_tree.get_sub_container( ["level0_2", "level1_nested_list"]) level1_nested_list_num = struct_tree.get_sub_numeration( ["level0_2", "level1_nested_list"]) level1_nested_list_att = struct_tree.get_sub_attributes( ["level0_2", "level1_nested_list"]) assert 'd1' in level1_dict.get_child_data().keys() assert 'd2' in level1_dict.get_child_data().keys() assert ['l1'] in [x.keys() for x in level1_list.get_child_data()] assert ['l2'] in [x.keys() for x in level1_list.get_child_data()] assert level1_nested_list_num is None assert level1_nested_list_att is None assert level1_nested_list_con._edges.keys() == [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
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
def _do_inv_for(sources, multi_host_sections, host_config, ipaddress): # type: (data_sources.DataSources, data_sources.MultiHostSections, config.HostConfig, Optional[str]) -> Tuple[StructuredDataTree, StructuredDataTree] hostname = host_config.hostname _initialize_inventory_tree() inventory_tree = g_inv_tree status_data_tree = StructuredDataTree() 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) inventory_tree.normalize_nodes() status_data_tree.normalize_nodes() return inventory_tree, status_data_tree