From 78607bfad8610bedb9785d3048876e873f2718a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rund=20Helleb=C3=B8?= Date: Mon, 29 Jul 2024 10:26:20 +0200 Subject: [PATCH] Declare and use dhcp metric path in nav.metrics.templates --- python/nav/dhcp/generic_metrics.py | 14 +++++--------- python/nav/metrics/templates.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/python/nav/dhcp/generic_metrics.py b/python/nav/dhcp/generic_metrics.py index ad31daa8de..824aeeb6de 100644 --- a/python/nav/dhcp/generic_metrics.py +++ b/python/nav/dhcp/generic_metrics.py @@ -2,7 +2,7 @@ from enum import Enum from IPy import IP from nav.metrics import carbon, CONFIG -from nav.metrics.names import escape_metric_name +from nav.metrics.templates import metric_path_for_subnet_dhcp from typing import Iterator from datetime import datetime @@ -29,12 +29,6 @@ class DhcpMetricSource: specific line of DHCP servers and import the metrics into NAV's graphite server. Subclasses need to implement `fetch_metrics`. """ - - graphite_prefix: str - - def __init__(self, graphite_prefix="nav.dhcp"): - self.graphite_prefix = graphite_prefix - def fetch_metrics(self) -> Iterator[DhcpMetric]: """ Fetch DhcpMetrics having keys `TOTAL` and `ASSIGNED` for each subnet of the @@ -54,7 +48,9 @@ def fetch_metrics_to_graphite( """ graphite_metrics = [] for metric in self.fetch_metrics(): - graphite_path = f"{self.graphite_prefix}.{escape_metric_name(metric.subnet_prefix.strNormal())}.{metric.key}" + metric_path = metric_path_for_subnet_dhcp( + metric.subnet_prefix, str(metric.key) + ) datapoint = (metric.timestamp, metric.value) - graphite_metrics.append((graphite_path, datapoint)) + graphite_metrics.append((metric_path, datapoint)) carbon.send_metrics_to(graphite_metrics, host, port) diff --git a/python/nav/metrics/templates.py b/python/nav/metrics/templates.py index 0f8a70e2a9..9207725804 100644 --- a/python/nav/metrics/templates.py +++ b/python/nav/metrics/templates.py @@ -184,3 +184,13 @@ def metric_path_for_multicast_usage(group, sysname): group=metric_prefix_for_multicast_group(group), sysname=escape_metric_name(sysname), ) + + +def metric_path_for_subnet_dhcp(subnet_prefix, metric_name): + tmpl = "nav.dhcp.{subnet_prefix}.{metric_name}" + if hasattr(subnet_prefix, 'strNormal') and callable(subnet_prefix.strNormal): + subnet_prefix = subnet_prefix.strNormal() + return tmpl.format( + subnet_prefix=escape_metric_name(subnet_prefix), + metric_name=metric_name + )