예제 #1
0
    def create_graph_recipes(self, ident_info, destination=None):
        graph_identification_info = ident_info

        def get_info(key):
            try:
                return graph_identification_info[key]
            except KeyError:
                raise MKUserError(
                    None,
                    _("Graph identification: The '%s' attribute is missing") %
                    key)

        site = get_info('site')
        host_name = get_info('host_name')
        service_description = get_info('service_description')
        row = get_graph_data_from_livestatus(site, host_name,
                                             service_description)
        translated_metrics = translated_metrics_from_row(row)

        graph_recipes = []
        for index, graph_template in matching_graph_templates(
                graph_identification_info,
                translated_metrics,
        ):
            graph_recipe = create_graph_recipe_from_template(
                graph_template,
                translated_metrics,
                row,
            )
            # Put the specification of this graph into the graph_recipe
            spec_info = graph_identification_info.copy()
            spec_info["graph_index"] = index
            graph_recipe["specification"] = ("template", spec_info)
            graph_recipes.append(graph_recipe)
        return graph_recipes
예제 #2
0
def _answer_graph_image_request() -> None:
    try:
        host_name = request.var("host")
        if not host_name:
            raise MKGeneralException(_("Missing mandatory \"host\" parameter"))

        service_description = request.var("service", "_HOST_")

        site = request.var("site")
        # FIXME: We should really enforce site here. But it seems that the notification context
        # has no idea about the site of the host. This could be optimized later.
        #if not site:
        #    raise MKGeneralException("Missing mandatory \"site\" parameter")
        try:
            row = get_graph_data_from_livestatus(site, host_name, service_description)
        except livestatus.MKLivestatusNotFoundError:
            if config.debug:
                raise
            raise Exception(
                _("Cannot render graph: host %s, service %s not found.") %
                (host_name, service_description))

        site = row["site"]

        # Always use 25h graph in notifications
        end_time = time.time()
        start_time = end_time - (25 * 3600)

        graph_render_options = graph_image_render_options()

        graph_identification = (
            "template",
            {
                "site": site,
                "host_name": host_name,
                "service_description": service_description,
                "graph_index": None,  # all graphs
            })

        graph_data_range = graph_image_data_range(graph_render_options, start_time, end_time)
        graph_recipes = graph_identification_types.create_graph_recipes(
            graph_identification, destination=html_render.GraphDestinations.notification)
        num_graphs = request.get_integer_input("num_graphs") or len(graph_recipes)

        graphs = []
        for graph_recipe in graph_recipes[:num_graphs]:
            graph_artwork = artwork.compute_graph_artwork(graph_recipe, graph_data_range,
                                                          graph_render_options)
            graph_png = render_graph_image(graph_artwork, graph_data_range, graph_render_options)

            graphs.append(base64.b64encode(graph_png).decode("ascii"))

        response.set_data(json.dumps(graphs))

    except Exception as e:
        logger.error("Call to ajax_graph_images.py failed: %s\n%s", e, traceback.format_exc())
        if config.debug:
            raise
예제 #3
0
    def create_graph_recipes(self,
                             ident_info,
                             destination=None) -> Sequence[GraphRecipe]:
        graph_identification_info = ident_info

        def get_info(key):
            try:
                return graph_identification_info[key]
            except KeyError:
                raise MKUserError(
                    None,
                    _("Graph identification: The '%s' attribute is missing") %
                    key)

        site = get_info("site")
        host_name = get_info("host_name")
        service_description = get_info("service_description")
        row = get_graph_data_from_livestatus(site, host_name,
                                             service_description)
        translated_metrics = translated_metrics_from_row(row)

        graph_recipes = []
        for index, graph_template in matching_graph_templates(
                graph_identification_info,
                translated_metrics,
        ):
            graph_template_tuned = self.template_tuning(
                graph_template,
                site=site,
                host_name=host_name,
                service_description=service_description,
                destination=destination,
            )
            if graph_template_tuned is None:
                continue

            graph_recipe = create_graph_recipe_from_template(
                graph_template_tuned,
                translated_metrics,
                row,
            )
            # Put the specification of this graph into the graph_recipe
            spec_info = graph_identification_info.copy()
            # Performance graph dashlets already use graph_id, but for example in reports, we still
            # use graph_index. We should switch to graph_id everywhere (CMK-7308). Once this is
            # done, we can remove the line below.
            spec_info["graph_index"] = index
            spec_info["graph_id"] = graph_template_tuned["id"]
            graph_recipe["specification"] = ("template", spec_info)
            graph_recipes.append(graph_recipe)
        return graph_recipes