def test_get_graph_templates(load_plugins, metric_names, check_command, graph_ids): perfdata: List[Tuple] = [(n, 0, u'', None, None, None, None) for n in metric_names] translated_metrics = utils.translate_metrics(perfdata, check_command) assert set(graph_ids) == set( t['id'] for t in utils.get_graph_templates(translated_metrics))
def matching_graph_templates( graph_identification_info: Mapping, translated_metrics: TranslatedMetrics, ) -> Iterable[Tuple[int, GraphTemplate]]: graph_index = graph_identification_info.get( "graph_index") # can be None -> no restriction by index graph_id = graph_identification_info.get( "graph_id") # can be None -> no restriction by id yield from ((index, graph_template) for index, graph_template in enumerate( get_graph_templates(translated_metrics)) if (graph_index is None or index == graph_index) and ( graph_id is None or graph_template["id"] == graph_id))
def _graph_choices_from_livestatus_row(row) -> Iterable[Tuple[str, str]]: def _metric_title_from_id(metric_or_graph_id: MetricName) -> str: metric_id = metric_or_graph_id.replace("METRIC_", "") return str(metric_info.get(metric_id, {}).get("title", metric_id)) def _graph_template_title(graph_template: Mapping) -> str: return str(graph_template.get("title")) or _metric_title_from_id( graph_template["id"]) yield from (( template["id"], _graph_template_title(template), ) for template in get_graph_templates(translated_metrics_from_row(row)))
def matching_graph_templates( graph_identification_info: TemplateGraphSpec, translated_metrics: TranslatedMetrics, ) -> Iterable[Tuple[int, GraphTemplate]]: graph_index = graph_identification_info.get( "graph_index") # can be None -> no restriction by index graph_id = graph_identification_info.get( "graph_id") # can be None -> no restriction by id # Single metrics if (isinstance(graph_id, str) and graph_id.startswith("METRIC_") and graph_id[7:] in translated_metrics): yield (0, get_graph_template(graph_id)) return yield from ((index, graph_template) for index, graph_template in enumerate( get_graph_templates(translated_metrics)) if (graph_index is None or index == graph_index) and ( graph_id is None or graph_template["id"] == graph_id))
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') graph_index = graph_identification_info.get( "graph_index") # can be None -> show all graphs row = get_graph_data_from_livestatus(site, host_name, service_description) translated_metrics = translated_metrics_from_row(row) graph_templates = get_graph_templates(translated_metrics) site = row["site"] graph_recipes = [] for index, graph_template in enumerate(graph_templates): if graph_index is None or index == graph_index and graph_template is not None: 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
def page_pnp_template(): try: template_id = html.request.var("id") check_command, perf_string = template_id.split(":", 1) # TODO: pnp-templates/default.php still returns a default value of # 1 for the value and "" for the unit. perf_data, _ = parse_perf_data(perf_string) translated_metrics = translate_metrics(perf_data, check_command) if not translated_metrics: return # check not supported # Collect output in string. In case of an exception to not output # any definitions output = "" for graph_template in get_graph_templates(translated_metrics): graph_code = render_graph_pnp(graph_template, translated_metrics) output += graph_code html.write(output) except Exception: html.write("An error occured:\n%s\n" % traceback.format_exc())
def test_get_graph_templates(metric_names, check_command, graph_ids): perfdata: Perfdata = [(n, 0, "", None, None, None, None) for n in metric_names] translated_metrics = utils.translate_metrics(perfdata, check_command) assert set(graph_ids) == set( t["id"] for t in utils.get_graph_templates(translated_metrics))