Skip to content

Commit

Permalink
Declare and use dhcp metric path in nav.metrics.templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jorund1 committed Jul 30, 2024
1 parent 65dd2f4 commit 78607bf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
14 changes: 5 additions & 9 deletions python/nav/dhcp/generic_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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)
10 changes: 10 additions & 0 deletions python/nav/metrics/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit 78607bf

Please sign in to comment.