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
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
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))
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
}, } 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() tree_old_addresses_arrays_memory = TEST_DATA_STORE.load( host_name=HostName("tree_old_addresses_arrays_memory")) tree_old_addresses = TEST_DATA_STORE.load( host_name=HostName("tree_old_addresses")) tree_old_arrays = TEST_DATA_STORE.load(host_name=HostName("tree_old_arrays")) tree_old_interfaces = TEST_DATA_STORE.load( host_name=HostName("tree_old_interfaces")) tree_old_memory = TEST_DATA_STORE.load(host_name=HostName("tree_old_memory")) tree_old_heute = TEST_DATA_STORE.load(host_name=HostName("tree_old_heute")) tree_new_addresses_arrays_memory = TEST_DATA_STORE.load( host_name=HostName("tree_new_addresses_arrays_memory")) tree_new_addresses = TEST_DATA_STORE.load( host_name=HostName("tree_new_addresses")) tree_new_arrays = TEST_DATA_STORE.load(host_name=HostName("tree_new_arrays")) tree_new_interfaces = TEST_DATA_STORE.load( host_name=HostName("tree_new_interfaces"))