Пример #1
0
    def do_execute(self, diagnostics_parameters: DiagnosticsParameters,
                   job_interface: BackgroundProcessInterface) -> None:
        job_interface.send_progress_update(_("Diagnostics dump started..."))

        job_interface.send_progress_update(repr(diagnostics_parameters))
        job_interface.send_progress_update(repr(serialize_wato_parameters(diagnostics_parameters)))

        site = diagnostics_parameters["site"]
        timeout = html.request.request_timeout - 2
        result = check_mk_automation(site,
                                     "create-diagnostics-dump",
                                     args=serialize_wato_parameters(diagnostics_parameters),
                                     timeout=timeout,
                                     non_blocking_http=True)

        job_interface.send_progress_update(result["output"])

        if result["tarfile_created"]:
            tarfile_path = result['tarfile_path']
            download_url = html.makeuri_contextless([("site", site),
                                                     ("tarfile_name", str(Path(tarfile_path)))],
                                                    "download_diagnostics_dump.py")
            button = html.render_icon_button(download_url, _("Download"), "diagnostics_dump_file")

            job_interface.send_progress_update(_("Dump file: %s") % tarfile_path)
            job_interface.send_result_message(_("%s Creating dump file successfully") % button)

        else:
            job_interface.send_result_message(_("Creating dump file failed"))
Пример #2
0
    def _fetch_agent_output(self, job_interface):
        job_interface.send_progress_update(
            _("Fetching '%s'...") % self._request.agent_type)

        agent_output_result = get_agent_output(
            self._request.host.site_id(),
            self._request.host.name(),
            self._request.agent_type,
        )

        if not agent_output_result.success:
            job_interface.send_progress_update(
                _("Failed: %s") % agent_output_result.service_details)

        preview_filepath = os.path.join(
            job_interface.get_work_dir(),
            AgentOutputPage.file_name(self._request))
        store.save_text_to_file(
            preview_filepath,
            agent_output_result.raw_agent_data.decode("utf-8"),
        )

        download_url = makeuri_contextless(
            request,
            [("host", self._request.host.name()),
             ("type", self._request.agent_type)],
            filename="download_agent_output.py",
        )

        button = html.render_icon_button(download_url, _("Download"),
                                         "agent_output")
        job_interface.send_progress_update("Job finished.")
        job_interface.send_result_message(
            _("%s Click on the icon to download the agent output.") % button)
Пример #3
0
    def _fetch_agent_output(self, job_interface):
        job_interface.send_progress_update(
            _("Fetching '%s'...") % self._request.agent_type)

        success, output, agent_data = watolib.check_mk_automation(
            self._request.host.site_id(), "get-agent-output",
            [self._request.host.name(), self._request.agent_type])

        if not success:
            job_interface.send_progress_update(_("Failed: %s") % output)

        preview_filepath = os.path.join(
            job_interface.get_work_dir(),
            AgentOutputPage.file_name(self._request))
        store.save_file(preview_filepath, agent_data)

        download_url = makeuri_contextless(
            request,
            [("host", self._request.host.name()),
             ("type", self._request.agent_type)],
            filename="download_agent_output.py",
        )

        button = html.render_icon_button(download_url, _("Download"),
                                         "agent_output")
        job_interface.send_progress_update(
            _("Finished. Click on the icon to download the data."))
        job_interface.send_result_message(_("%s Finished.") % button)
Пример #4
0
    def do_execute(
        self,
        diagnostics_parameters: DiagnosticsParameters,
        job_interface: BackgroundProcessInterface,
    ) -> None:
        job_interface.send_progress_update(_("Diagnostics dump started..."))

        site = diagnostics_parameters["site"]
        timeout = request.request_timeout - 2
        result = create_diagnostics_dump(
            site,
            serialize_wato_parameters(diagnostics_parameters),
            timeout,
        )

        job_interface.send_progress_update(result.output)

        if result.tarfile_created:
            tarfile_path = result.tarfile_path
            download_url = makeuri_contextless(
                request,
                [("site", site), ("tarfile_name", str(Path(tarfile_path).name))],
                filename="download_diagnostics_dump.py",
            )
            button = html.render_icon_button(download_url, _("Download"), "diagnostics_dump_file")

            job_interface.send_progress_update(_("Dump file: %s") % tarfile_path)
            job_interface.send_result_message(_("%s Retrieve created dump file") % button)

        else:
            job_interface.send_result_message(_("Creating dump file failed"))
Пример #5
0
    def _render_tag_tree_level(self, tree_spec, path, cwd, title,
                               tree) -> None:
        if not self._is_tag_subdir(
                path=path, cwd=cwd) and not self._is_tag_subdir(path=cwd,
                                                                cwd=path):
            return

        container: ContextManager[bool] = nullcontext(False)
        if path != cwd and self._is_tag_subdir(path, cwd):
            bullet = self._tag_tree_bullet(self._tag_tree_worst_state(tree),
                                           path, False)
            if self._tag_tree_has_svc_problems(tree):
                bullet += html.render_icon_button(
                    self._tag_tree_url(tree_spec, path, "svcproblems"),
                    _("Show the service problems contained in this branch"),
                    "svc_problems",
                    target="main",
                )

            if path:
                container = foldable_container(
                    treename="tag-tree",
                    id_=".".join(map(str, path)),
                    isopen=False,
                    title=bullet + title,
                    icon="foldable_sidebar",
                )

        with container:
            for (node_title, node_value), subtree in sorted(
                    tree.get("_children", {}).items()):
                subpath = path + [node_value or ""]
                url = self._tag_tree_url(tree_spec, subpath, "allhosts")

                if "_num_hosts" in subtree:
                    node_title += " (%d)" % subtree["_num_hosts"]

                node_title = html.render_a(node_title, href=url, target="main")

                if "_children" not in subtree:
                    if self._is_tag_subdir(path, cwd):
                        html.write_html(
                            self._tag_tree_bullet(subtree.get("_state", 0),
                                                  subpath, True))
                        if subtree.get("_svc_problems"):
                            url = self._tag_tree_url(tree_spec, subpath,
                                                     "svcproblems")
                            html.icon_button(
                                url,
                                _("Show the service problems contained in this branch"
                                  ),
                                "svc_problems",
                                target="main",
                            )
                        html.write_html(node_title)
                        html.br()
                else:
                    self._render_tag_tree_level(tree_spec, subpath, cwd,
                                                node_title, subtree)
Пример #6
0
def format_plugin_output(output: str,
                         row: "Optional[Row]" = None,
                         shall_escape: bool = True) -> HTML:
    assert not isinstance(output, dict)
    ok_marker = '<b class="stmark state0">OK</b>'
    warn_marker = '<b class="stmark state1">WARN</b>'
    crit_marker = '<b class="stmark state2">CRIT</b>'
    unknown_marker = '<b class="stmark state3">UNKN</b>'

    # In case we have a host or service row use the optional custom attribute
    # ESCAPE_PLUGIN_OUTPUT (set by host / service ruleset) to override the global
    # setting.
    if row:
        custom_vars = row.get("service_custom_variables",
                              row.get("host_custom_variables", {}))
        if "ESCAPE_PLUGIN_OUTPUT" in custom_vars:
            shall_escape = custom_vars["ESCAPE_PLUGIN_OUTPUT"] == "1"

    if shall_escape:
        output = escaping.escape_attribute(output)
    else:
        output = "%s" % output

    output = (output.replace("(!)",
                             warn_marker).replace("(!!)", crit_marker).replace(
                                 "(?)",
                                 unknown_marker).replace("(.)", ok_marker))

    if row and "[running on" in output:
        a = output.index("[running on")
        e = output.index("]", a)
        hosts = output[a + 12:e].replace(" ", "").split(",")
        h = get_host_list_links(row["site"], hosts)
        output = output[:a] + "running on " + ", ".join(h) + output[e + 1:]

    prevent_url_icons = (row.get("service_check_command", "")
                         == "check_mk-checkmk_agent"
                         if row is not None else False)
    if shall_escape and not prevent_url_icons:
        http_url = r"(http[s]?://[A-Za-z0-9\-._~:/?#\[\]@!$&'()*+,;=%]+)"
        # (?:&lt;A HREF=&quot;), (?: target=&quot;_blank&quot;&gt;)? and endswith(" </A>") is a special
        # handling for the HTML code produced by check_http when "clickable URL" option is active.
        output = re.sub(
            "(?:&lt;A HREF=&quot;)?" + http_url +
            "(?: target=&quot;_blank&quot;&gt;)?",
            lambda p: str(
                html.render_icon_button(
                    unescape(p.group(1).replace("&quot;", "")),
                    unescape(p.group(1).replace("&quot;", "")),
                    "link",
                )),
            output,
        )

        if output.endswith(" &lt;/A&gt;"):
            output = output[:-11]

    return HTML(output)
Пример #7
0
    def _render_tag_tree_level(self, tree_spec, path, cwd, title, tree):
        if (not self._is_tag_subdir(path=path, cwd=cwd)
                and not self._is_tag_subdir(path=cwd, cwd=path)):
            return

        if path != cwd and self._is_tag_subdir(path, cwd):
            bullet = self._tag_tree_bullet(self._tag_tree_worst_state(tree),
                                           path, False)
            if self._tag_tree_has_svc_problems(tree):
                bullet += html.render_icon_button(
                    self._tag_tree_url(tree_spec, path, "svcproblems"),
                    _("Show the service problems contained in this branch"),
                    "svc_problems",
                    target="main")

            if path:
                html.begin_foldable_container(
                    "tag-tree",
                    ".".join(map(str, path)),
                    False,
                    HTML(bullet + title),
                    icon="foldable_sidebar",
                )

        for (node_title,
             node_value), subtree in sorted(tree.get("_children", {}).items()):
            subpath = path + [node_value or ""]
            url = self._tag_tree_url(tree_spec, subpath, "allhosts")

            if "_num_hosts" in subtree:
                node_title += " (%d)" % subtree["_num_hosts"]

            node_title = html.render_a(node_title, href=url, target="main")

            if "_children" not in subtree:
                if self._is_tag_subdir(path, cwd):
                    html.write(
                        self._tag_tree_bullet(subtree.get("_state", 0),
                                              subpath, True))
                    if subtree.get("_svc_problems"):
                        url = self._tag_tree_url(tree_spec, subpath,
                                                 "svcproblems")
                        html.icon_button(
                            url,
                            _("Show the service problems contained in this branch"
                              ),
                            "svc_problems",
                            target="main")
                    html.write(node_title)
                    html.br()
            else:
                self._render_tag_tree_level(tree_spec, subpath, cwd,
                                            node_title, subtree)

        if path and path != cwd and self._is_tag_subdir(path, cwd):
            html.end_foldable_container()
Пример #8
0
    def do_execute(
        self,
        diagnostics_parameters: DiagnosticsParameters,
        job_interface: BackgroundProcessInterface,
    ) -> None:
        job_interface.send_progress_update(_("Diagnostics dump started..."))

        chunks = serialize_wato_parameters(diagnostics_parameters)

        # TODO: Currently, selecting multiple sites is not possible.
        # sites = diagnostics_parameters["sites"][1]
        site = diagnostics_parameters["site"]

        timeout = request.request_timeout - 2
        results = []
        for chunk in chunks:
            chunk_result = create_diagnostics_dump(
                site,
                chunk,
                timeout,
            )
            results.append(chunk_result)

        # for site in sites:
        #    for chunk in chunks:

        #        chunk_result = create_diagnostics_dump(
        #            site,
        #            chunk,
        #            timeout,
        #        )
        #        results.append(chunk_result)

        if len(results) > 1:
            result = _merge_results(results)
        elif len(results) == 1:
            result = results[0]
        else:
            job_interface.send_result_message(
                _("Got no result to create dump file"))
            return

        job_interface.send_progress_update(result.output)

        if result.tarfile_created:
            tarfile_path = result.tarfile_path
            download_url = makeuri_contextless(
                request,
                [("site", site),
                 ("tarfile_name", str(Path(tarfile_path).name))],
                filename="download_diagnostics_dump.py",
            )
            button = html.render_icon_button(download_url, _("Download"),
                                             "diagnostics_dump_file")

            job_interface.send_progress_update(
                _("Dump file: %s") % tarfile_path)
            job_interface.send_result_message(
                _("%s Retrieve created dump file") % button)

        else:
            job_interface.send_result_message(_("Creating dump file failed"))