def paint(self, value, hostname): value = convert_cgroups_from_tuple(value) texts: List[HTML] = [] self.load_data() if self._contactgroups is None: # conditional caused by horrible API raise Exception("invalid contact groups") items = self._contactgroups.items() for name, cgroup in sorted(items, key=lambda x: x[1]["alias"]): if name in value["groups"]: display_name = cgroup.get("alias", name) texts.append( HTMLWriter.render_a( display_name, href=makeuri_contextless( request, [("mode", "edit_contact_group"), ("edit", name)], filename="wato.py", ), )) result: HTML = HTML(", ").join(texts) if texts and value["use"]: result += HTMLWriter.render_span( HTMLWriter.render_b("*"), title= _("These contact groups are also used in the monitoring configuration." ), ) return "", result
def _render_manpage_list(titles, manpage_list, path_comp, heading): def translate(t): return titles.get(t, t) html.h3(heading) with table_element(searchable=False, sortable=False, css="check_catalog") as table: for entry in sorted(manpage_list, key=lambda x: x["title"]): if not isinstance(entry, dict): continue table.row() url = makeuri( request, [ ("mode", "check_manpage"), ("check_type", entry["name"]), ("back", makeuri(request, [])), ], ) table.cell(_("Type of Check"), HTMLWriter.render_a(entry["title"], href=url), css=["title"]) table.cell(_("Plugin Name"), HTMLWriter.render_tt(entry["name"]), css=["name"]) table.cell(_("Agents"), ", ".join(map(translate, sorted(entry["agents"]))), css=["agents"])
def query_limit_exceeded_warn(limit: Optional[int], user_config: LoggedInUser) -> None: """Compare query reply against limits, warn in the GUI about incompleteness""" text = HTML(_("Your query produced more than %d results. ") % limit) if request.get_ascii_input("limit", "soft") == "soft" and user_config.may( "general.ignore_soft_limit" ): text += HTMLWriter.render_a( _("Repeat query and allow more results."), target="_self", href=makeuri(request, [("limit", "hard")]), ) elif request.get_ascii_input("limit") == "hard" and user_config.may( "general.ignore_hard_limit" ): text += HTMLWriter.render_a( _("Repeat query without limit."), target="_self", href=makeuri(request, [("limit", "none")]), ) text += escaping.escape_to_html_permissive( " " + _("<b>Note:</b> the shown results are incomplete and do not reflect the sort order.") ) html.show_warning(text)
def page(self) -> cmk.gui.pages.PageResult: breadcrumb = make_simple_page_breadcrumb( mega_menu_registry["help_links"], _("Info")) make_header( html, self._title(), breadcrumb=breadcrumb, ) html.open_div(id_="info_title") html.h1(_("Your monitoring machine")) html.a( HTMLWriter.render_img(theme.url("images/tribe29.svg")), "https://tribe29.com", target="_blank", ) html.close_div() html.div(None, id_="info_underline") html.open_div(id_="info_intro_text") html.span(_("Open. Effective. Awesome.")) html.span( _("May we present? Monitoring as it's supposed to be: " "incredibly quick to install, infinetely scalable, highly customizable and " "designed for admins.")) html.span( _("Visit our %s to learn more about Checkmk and about the %s.") % ( HTMLWriter.render_a( _("website"), "https://checkmk.com", target="_blank"), HTMLWriter.render_a( _("latest version"), "https://checkmk.com/product/latest-version", target="_blank", ), )) html.close_div() version_major_minor = re.sub(r".\d+$", "", Version(__version__).version_base) if version_major_minor: current_version_link = "https://checkmk.com/product/checkmk-%s" % version_major_minor else: current_version_link = "https://checkmk.com/product/latest-version" html.open_div(id="info_image") html.open_a(href=current_version_link, target="_blank") html.img(theme.url("images/monitoring-machine.png")) html.close_a() html.close_div() html.close_div() html.open_div(id_="info_footer") html.span( _("© %s tribe29 GmbH. All Rights Reserved.") % time.strftime("%Y")) html.a(_("License agreement"), href="https://checkmk.com/legal.html", target="_blank") html.close_div()
def render(self, row: Row, cell: Cell) -> CellSpec: classes = ["perfometer"] if is_stale(row): classes.append("stale") try: title, h = Perfometer(row).render() if title is None and h is None: return "", "" except Exception as e: logger.exception("error rendering performeter") if active_config.debug: raise return " ".join(classes), _("Exception: %s") % e assert h is not None content = (HTMLWriter.render_div(HTML(h), class_=["content"]) + HTMLWriter.render_div(title, class_=["title"]) + HTMLWriter.render_div("", class_=["glass"])) # pnpgraph_present: -1 means unknown (path not configured), 0: no, 1: yes if display_options.enabled( display_options.X) and row["service_pnpgraph_present"] != 0: url = cmk_graph_url(row, "service") disabled = False else: url = "javascript:void(0)" disabled = True return " ".join(classes), HTMLWriter.render_a( content=content, href=url, title=escaping.strip_tags(title), class_=["disabled"] if disabled else [], )
def _show_tree(self): td_style = None if self._wrap_texts == "wrap" else "white-space: nowrap;" tree = self._get_tree() depth = status_tree_depth(tree) leaves = self._gen_table(tree, depth, len(self._row["aggr_hosts"]) > 1) html.open_table(class_=["aggrtree", "ltr"]) odd = "odd" for code, colspan, parents in leaves: html.open_tr() leaf_td = HTMLWriter.render_td(code, class_=["leaf", odd], style=td_style, colspan=colspan) odd = "even" if odd == "odd" else "odd" tds = [leaf_td] for rowspan, c in parents: tds.append( HTMLWriter.render_td(c, class_=["node"], style=td_style, rowspan=rowspan)) if self._mirror: tds.reverse() html.write_html(HTML("").join(tds)) html.close_tr() html.close_table()
def page(self): with table_element("roles") as table: users = userdb.load_users() for rid, role in sorted(self._roles.items(), key=lambda a: (a[1]["alias"], a[0])): table.row() # Actions table.cell(_("Actions"), css=["buttons"]) edit_url = folder_preserving_link([("mode", "edit_role"), ("edit", rid)]) clone_url = make_action_link([("mode", "roles"), ("_clone", rid)]) delete_url = make_confirm_link( url=make_action_link([("mode", "roles"), ("_delete", rid)]), message=_("Do you really want to delete the role %s?") % rid, ) html.icon_button(edit_url, _("Properties"), "edit") html.icon_button(clone_url, _("Clone"), "clone") if not role.get("builtin"): html.icon_button(delete_url, _("Delete this role"), "delete") # ID table.cell(_("Name"), rid) # Alias table.cell(_("Alias"), role["alias"]) # Type table.cell(_("Type"), _("builtin") if role.get("builtin") else _("custom")) # Modifications table.cell( _("Modifications"), HTMLWriter.render_span( str(len(role["permissions"])), title=_("That many permissions do not use the factory defaults."), ), ) # Users table.cell( _("Users"), HTML(", ").join( [ HTMLWriter.render_a( user.get("alias", user_id), folder_preserving_link([("mode", "edit_user"), ("edit", user_id)]), ) for (user_id, user) in users.items() if rid in user["roles"] ] ), )
def show(self) -> None: only_sites = snapin_site_choice("mkeventd_performance", get_event_console_site_choices()) try: entries = self._mkeventd_performance_entries(only_sites) except Exception as e: html.show_error("%s" % e) return html.open_table(class_=["mkeventd_performance"]) for _index, left, right in entries: html.tr(HTMLWriter.render_td("%s:" % left) + HTMLWriter.render_td(right)) html.close_table()
def _render_tag_group( tag_group_id_or_label_key: Union[TaggroupID, str], tag_id_or_label_value: Union[TagID, str], object_type: str, with_link: bool, label_type: str, label_source: str, ) -> HTML: span = HTMLWriter.render_tag( HTMLWriter.render_div( HTMLWriter.render_span( "%s:%s" % ( tag_group_id_or_label_key, tag_id_or_label_value, ), class_=["tagify__tag-text"], ) ), class_=["tagify--noAnim", label_source], ) if not with_link: return span if label_type == "tag_group": type_filter_vars: HTTPVariables = [ ("%s_tag_0_grp" % object_type, tag_group_id_or_label_key), ("%s_tag_0_op" % object_type, "is"), ("%s_tag_0_val" % object_type, tag_id_or_label_value), ] elif label_type == "label": type_filter_vars = [ ( "%s_label" % object_type, json.dumps( [{"value": "%s:%s" % (tag_group_id_or_label_key, tag_id_or_label_value)}] ), ), ] else: raise NotImplementedError() url_vars: HTTPVariables = [ ("filled_in", "filter"), ("search", "Search"), ("view_name", "searchhost" if object_type == "host" else "searchsvc"), ] url = makeuri_contextless(request, url_vars + type_filter_vars, filename="view.py") return HTMLWriter.render_a(span, href=url)
def _tag_tree_bullet(self, state, path, leaf) -> HTML: code = HTMLWriter.render_div( " ", class_=["tagtree"] + (["leaf"] if leaf else []) + ["statebullet", "state%d" % state], ) if not leaf: code = HTMLWriter.render_a( code, href="javascript:virtual_host_tree_enter('%s');" % "|".join(path), title=_("Display the tree only below this node"), ) return code + " "
def action(self) -> ActionResult: renaming_config = self._vs_renaming_config().from_html_vars("") self._vs_renaming_config().validate_value(renaming_config, "") renamings = self._collect_host_renamings(renaming_config) if not renamings: flash(_("No matching host names")) return None warning = self._renaming_collision_error(renamings) if warning: flash(warning) return None message = HTMLWriter.render_b( _("Do you really want to rename to following hosts?" "This involves a restart of the monitoring core!")) rows = [] for _folder, host_name, target_name in renamings: rows.append( HTMLWriter.render_tr( HTMLWriter.render_td(host_name) + HTMLWriter.render_td(" → %s" % target_name))) message += HTMLWriter.render_table(HTML().join(rows)) nr_rename = len(renamings) c = _confirm( _("Confirm renaming of %d %s") % (nr_rename, ungettext("host", "hosts", nr_rename)), message, ) if c: title = _("Renaming of %s") % ", ".join("%s → %s" % x[1:] for x in renamings) host_renaming_job = RenameHostsBackgroundJob(title=title) host_renaming_job.set_function(rename_hosts_background_job, renamings) try: host_renaming_job.start() except background_job.BackgroundJobAlreadyRunning as e: raise MKGeneralException( _("Another host renaming job is already running: %s") % e) return redirect(host_renaming_job.detail_url()) if c is False: # not yet confirmed return FinalizeRequest(code=200) return None # browser reload
def test_render_a(request_context): a = HTMLWriter.render_a("bla", href="blu", class_=["eee"], target="_blank") assert compare_html(a, '<a href="blu" target="_blank" class="eee">bla</a>') a = HTMLWriter.render_a( "b<script>alert(1)</script>la", href="b<script>alert(1)</script>lu", class_=["eee"], target="_blank", ) assert compare_html( a, '<a href="b<script>alert(1)</script>lu" target="_blank" ' 'class="eee">b<script>alert(1)</script>la</a>', )
def render_tree_folder(tree_id, folder, js_func): subfolders = folder.get(".folders", {}).values() is_leaf = len(subfolders) == 0 # Suppress indentation for non-emtpy root folder if folder[".path"] == "" and is_leaf: html.open_ul() # empty root folder elif folder and folder[".path"] != "": html.open_ul(style="padding-left:0px;") title = HTMLWriter.render_a( "%s (%d)" % (folder["title"], folder[".num_hosts"]), href="#", class_="link", onclick="%s(this, '%s');" % (js_func, folder[".path"]), ) if not is_leaf: with foldable_container( treename=tree_id, id_="/" + folder[".path"], isopen=False, title=HTML(title), icon="foldable_sidebar", padding=6, ): for subfolder in sorted(subfolders, key=lambda x: x["title"].lower()): render_tree_folder(tree_id, subfolder, js_func) else: html.li(title) html.close_ul()
def _render_tree(self, tree): for group, attrs in tree.items(): aggr_group_tree = "/".join(attrs["__path__"]) fetch_url = makeuri_contextless( request, [ ("view_name", "aggr_all"), ("aggr_group_tree", aggr_group_tree), ], filename="view.py", ) if attrs.get("__children__"): with foldable_container( treename="bi_aggregation_group_trees", id_=aggr_group_tree, isopen=False, title=HTML( HTMLWriter.render_a( group, href=fetch_url, target="main", )), icon="foldable_sidebar", ): self._render_tree(attrs["__children__"]) else: html.open_ul() bulletlink(group, fetch_url) html.close_ul()
def show_log_list(): title = _("All problematic logfiles") breadcrumb = make_simple_page_breadcrumb( mega_menu_registry.menu_monitoring(), title) make_header(html, title, breadcrumb, _log_list_page_menu(breadcrumb)) if request.has_var("_ack") and not request.var("_do_actions") == _("No"): do_log_ack(site=None, host_name=None, file_name=None) return for site, host_name, logs in all_logs(): if not logs: continue all_logs_empty = not any( parse_file(site, host_name, file_name) for file_name in logs) if all_logs_empty: continue # Logfile vanished html.h3( HTMLWriter.render_a( host_name, href=makeuri( request, [("site", site), ("host", host_name)], ), ), class_="table", ) list_logs(site, host_name, logs) html.footer()
def list_logs(site, host_name, logfile_names): with table_element(empty_text=_("No logs found for this host.")) as table: for file_name in logfile_names: table.row() file_display = form_file_to_ext(file_name) uri = makeuri(request, [("site", site), ("host", host_name), ("file", file_display)]) logfile_link = HTMLWriter.render_a(file_display, href=uri) try: log_chunks = parse_file(site, host_name, file_name) if not log_chunks: continue # Logfile vanished worst_log = get_worst_chunk(log_chunks) last_log = get_last_chunk(log_chunks) state = worst_log["level"] state_name = form_level(state) table.cell(_("Level"), state_name, css=["state%d" % state]) table.cell(_("Logfile"), logfile_link) table.cell(_("Last Entry"), form_datetime(last_log["datetime"])) table.cell(_("Entries"), len(log_chunks), css=["number"]) except Exception: if active_config.debug: raise table.cell(_("Level"), "") table.cell(_("Logfile"), logfile_link) table.cell(_("Last Entry"), "") table.cell(_("Entries"), _("Corrupted"))
def render_object_ref_as_icon( object_ref: Optional[ObjectRef]) -> Optional[HTML]: if object_ref is None: return None url, title = _get_object_reference(object_ref) if not url: return None icons = { ObjectRefType.Host: "host", ObjectRefType.Folder: "folder", ObjectRefType.User: "******", ObjectRefType.Rule: "rule", ObjectRefType.Ruleset: "rulesets", } return HTMLWriter.render_a( content=html.render_icon( icons.get(object_ref.object_type, "link"), title="%s: %s" % (object_ref.object_type.name, title) if title else None, ), href=url, )
def _display_log(self, log): with table_element( css="data wato auditlog audit", limit=None, sortable=False, searchable=False ) as table: for entry in log: table.row() table.cell( _("Time"), HTMLWriter.render_nobr(render.date_and_time(float(entry.time))), css=["narrow"], ) user_txt = ("<i>%s</i>" % _("internal")) if entry.user_id == "-" else entry.user_id table.cell(_("User"), user_txt, css=["nobreak narrow"]) table.cell( _("Object type"), entry.object_ref.object_type.name if entry.object_ref else "", css=["narrow"], ) table.cell(_("Object"), render_object_ref(entry.object_ref) or "", css=["narrow"]) text = HTML(escaping.escape_text(entry.text).replace("\n", "<br>\n")) table.cell(_("Summary"), text) if self._show_details: diff_text = HTML( escaping.escape_text(entry.diff_text).replace("\n", "<br>\n") if entry.diff_text else "" ) table.cell(_("Details"), diff_text)
def end() -> None: global g_header_open g_header_open = False section_close() html.tr(HTMLWriter.render_td("", colspan=2), class_=["bottom"]) html.close_tbody() html.close_table()
def render_object_ref( object_ref: Optional[ObjectRef]) -> Union[str, HTML, None]: url, title = _get_object_reference(object_ref) if title and not url: return title if not title: return None return HTMLWriter.render_a(title, href=url)
def _paint_aggr_state_short(state, assumed=False): if state is None: return "", "" name = short_service_state_name(state["state"], "") classes = "state svcstate state%s" % state["state"] if assumed: classes += " assumed" return classes, HTMLWriter.render_span(name, class_=["state_rounded_fill"])
def paint(self, value, hostname): parts = [ HTMLWriter.render_a( hn, "wato.py?" + urlencode_vars([("mode", "edit_host"), ("host", hn)])) for hn in value ] return "", HTML(", ").join(parts)
def page_index() -> None: # Redirect to mobile GUI if we are a mobile device and the index is requested if is_mobile(request, response): raise HTTPRedirect(makeuri(request, [], filename="mobile.py")) title = get_page_heading() content = HTMLWriter.render_iframe("", src=_get_start_url(), name="main") SidebarRenderer().show(title, content)
def _show_dropdown_trigger(self, dropdown: PageMenuDropdown) -> None: html.popup_trigger( HTMLWriter.render_h2(dropdown.title), ident="menu_" + dropdown.name, method=MethodInline(self._render_dropdown_area(dropdown)), data=dropdown.popup_data, popup_group="menu", )
def render_color_icon(color: str) -> HTML: return HTMLWriter.render_div( "", class_="color", # NOTE: When we drop support for IE11 we can use #%s4c instead of rgba(...) style="background-color: rgba(%d, %d, %d, 0.3); border-color: %s;" % (*hex_color_to_rgb_color(color), color), )
def text_with_links_to_user_translated_html( elements: Iterable[Tuple[str, Optional[str]]], separator: str = "", ) -> HTML: return HTML(separator).join( HTMLWriter.render_a(user_translation, href=url, title=user_translation ) if url else escape_to_html_permissive( user_translation, escape_links=False) for txt, url in elements for user_translation in [_u(txt)] if txt)
def render_metricometer(stack) -> HTML: if len(stack) not in (1, 2): raise MKGeneralException( _("Invalid Perf-O-Meter definition %r: only one or two entries are allowed" ) % stack) h = HTML().join(map(render_perfometer, stack)) if len(stack) == 2: h = HTMLWriter.render_div(h, class_="stacked") return h
def _get_popup_trigger_content(self, active_icon: Icon, menu_item: MainMenuItem) -> HTML: content = html.render_icon(menu_item.icon) + html.render_icon( active_icon, class_=["active"]) if not user.get_attribute("nav_hide_icons_title"): content += HTMLWriter.render_div(menu_item.title) return content
def render(self, row, cell): url = makeuri_contextless( request, [ ("crash_id", row["crash_id"]), ("site", row["site"]), ], filename="crash.py", ) return (None, HTMLWriter.render_a(row["crash_id"], href=url))
def _render_rule_reason(self, title, title_url, reason, reason_url, is_default, setting: Union[str, HTML]) -> None: if title_url: title = HTMLWriter.render_a(title, href=title_url) forms.section(title) if reason: reason = HTMLWriter.render_a(reason, href=reason_url) html.open_table(class_="setting") html.open_tr() if is_default: html.td(HTMLWriter.render_i(reason), class_="reason") html.td(setting, class_=["settingvalue", "unused"]) else: html.td(reason, class_="reason") html.td(setting, class_=["settingvalue", "used"]) html.close_tr() html.close_table()