def get_wato_folder(row: Dict, how: str, with_links: bool = True) -> Union[str, HTML]: filename = row["host_filename"] if not filename.startswith("/wato/") or not filename.endswith("/hosts.mk"): return "" wato_path = filename[6:-9] try: title_path = watolib.get_folder_title_path(wato_path, with_links) except MKGeneralException: # happens when a path can not be resolved using the local WATO. # e.g. when having an independent site with different folder # hierarchy added to the GUI. # Display the raw path rather than the exception text. title_path = wato_path.split("/") except Exception as e: return "%s" % e if how == "plain": return title_path[-1] if how == "abs": return HTML(" / ").join(title_path) # We assume that only hosts are show, that are below the current WATO path. # If not then better output absolute path then wrong path. current_path = request.var("wato_folder") if not current_path or not wato_path.startswith(current_path): return HTML(" / ").join(title_path) depth = current_path.count("/") + 1 return HTML(" / ").join(title_path[depth:])
def _add_host_to_tree(self, tree_spec, tree, host_row, tag_groups, topics): _site, _host_name, wato_folder, state, _num_ok, num_warn, \ num_crit, num_unknown, custom_variables = host_row if wato_folder.startswith("/wato/"): folder_path = wato_folder[6:-9] folder_path_components = folder_path.split("/") if watolib.Folder.folder_exists(folder_path): folder_titles = watolib.get_folder_title_path(folder_path)[ 1:] # omit main folder else: folder_titles = [] state, have_svc_problems = self._calculate_state( state, num_crit, num_unknown, num_warn) tags = set(custom_variables.get("TAGS", []).split()) # Now go through the levels of the tree. Each level may either be # - a tag group id, or # - "topic:" plus the name of a tag topic. That topic should only contain # checkbox tags, or: # - "folder:3", where 3 is the folder level (starting at 1) # The problem with the "topic" entries is, that a host may appear several # times! parent_level_branches = [tree] for level_spec in tree_spec: this_level_branches = [] for tree_entry in parent_level_branches: if level_spec.startswith("topic:"): topic = level_spec[6:] if topic not in topics: continue # silently skip not existing topics # Iterate over all host tag groups with that topic for tag_group in topics[topic]: for tag in tag_group.tags: if tag.id in tags: this_level_branches.append( tree_entry.setdefault( "_children", {}).setdefault( (tag.title, tag.id), {})) elif level_spec.startswith("folder:"): level = int(level_spec.split(":", 1)[1]) if level <= len(folder_titles): node_title = folder_titles[level - 1] node_value = "folder:%d:%s" % ( level, folder_path_components[level - 1]) else: node_title = _("Hosts in this folder") node_value = "folder:%d:" % level this_level_branches.append( tree_entry.setdefault("_children", {}).setdefault( (node_title, node_value), {})) elif level_spec.startswith("foldertree:"): path_components = [] foldertree_tree_entry = tree_entry for path_component in folder_path_components: path_components.append(path_component) level = len(path_components) if level <= len(folder_titles): node_title = folder_titles[level - 1] node_value = "foldertree:%d:%s" % ( level, path_components[level - 1]) else: node_title = _("Main folder") node_value = "foldertree:%d:" % level foldertree_tree_entry = foldertree_tree_entry.setdefault( "_children", {}).setdefault( (node_title, node_value), {}) if path_components == folder_path_components: # Direct host parent this_level_branches.append(foldertree_tree_entry) else: # It' a tag group if level_spec not in tag_groups: continue # silently skip not existant tag groups tag_value, tag_title = self._get_tag_group_value( tag_groups[level_spec], tags) if self._trees[self._current_tree_id].get("exclude_empty_tag_choices", False) \ and tag_value is None: continue this_level_branches.append( tree_entry.setdefault("_children", {}).setdefault( (tag_title, tag_value), {})) parent_level_branches = this_level_branches # Add the numbers/state of this host to the last level the host is invovled with for tree_entry in this_level_branches: tree_entry.setdefault("_num_hosts", 0) tree_entry.setdefault("_state", 0) tree_entry["_num_hosts"] += 1 tree_entry["_svc_problems"] = tree_entry.get( "_svc_problems", False) or have_svc_problems if state == 2 or tree_entry["_state"] == 2: tree_entry["_state"] = 2 else: tree_entry["_state"] = max(state, tree_entry["_state"])