From a8f5128bf1d60f80598b18cb7e65b9f0d379fc5b Mon Sep 17 00:00:00 2001 From: Hanne Moa Date: Fri, 20 Jan 2023 11:33:19 +0100 Subject: [PATCH] Bundle nav.metrics.names.escape_metric_name --- .../isc_dhpcd_graphite/isc_dhpcd_graphite.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/contrib/scripts/isc_dhpcd_graphite/isc_dhpcd_graphite.py b/contrib/scripts/isc_dhpcd_graphite/isc_dhpcd_graphite.py index 1f49e3010a..9e807598be 100644 --- a/contrib/scripts/isc_dhpcd_graphite/isc_dhpcd_graphite.py +++ b/contrib/scripts/isc_dhpcd_graphite/isc_dhpcd_graphite.py @@ -13,14 +13,12 @@ import pickle import re import socket +import string import struct import subprocess import sys from time import time -from nav.metrics.names import escape_metric_name - - DEFAULT_PREFIX = "nav.dhcp" DEFAULT_CONFIG_FILE = "/etc/dhcpd/dhcpd.conf" DEFAULT_CMD_PATH = pathlib.Path("/usr/bin/dhcpd-pools") @@ -29,6 +27,7 @@ # graphite likes pickle protocol 2. Python 3: 3, Python 3.8+: 4 PICKLE_PROTOCOL = range(0, pickle.HIGHEST_PROTOCOL + 1) +LEGAL_METRIC_CHARACTERS = string.ascii_letters + string.digits + "-_" FLAGS = "-f j" METRIC_MAPPER = { "defined": "max", @@ -42,6 +41,19 @@ Metric = namedtuple("Metric", ["path", "value", "timestamp"]) +# vendored from nav.metrics.names.escape_metric_name +def escape_metric_name(name): + """ + Escapes any character of `name` that may not be used in graphite metric + names. + """ + if name is None: + return name + name = name.replace('\x00', '') # some devices have crazy responses! + name = ''.join([c if c in LEGAL_METRIC_CHARACTERS else "_" for c in name]) + return name + + # parse comand line flags def parse_args(): parser = argparse.ArgumentParser(description="Send dhcp stats to graphite")