Skip to content

Commit 97d1b7b

Browse files
committed
Bundle nav.metrics.names.escape_metric_name
1 parent deea0c2 commit 97d1b7b

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

contrib/scripts/isc_dhpcd_graphite/isc_dhpcd_graphite.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
import pickle
1414
import re
1515
import socket
16+
import string
1617
import struct
1718
import subprocess
1819
import sys
1920
from time import time
2021

21-
from nav.metrics.names import escape_metric_name
22-
23-
2422
DEFAULT_PREFIX = "nav.dhcp"
2523
DEFAULT_CONFIG_FILE = "/etc/dhcpd/dhcpd.conf"
2624
DEFAULT_CMD_PATH = pathlib.Path("/usr/bin/dhcpd-pools")
@@ -29,6 +27,7 @@
2927

3028
# graphite likes pickle protocol 2. Python 3: 3, Python 3.8+: 4
3129
PICKLE_PROTOCOL = range(0, pickle.HIGHEST_PROTOCOL + 1)
30+
LEGAL_METRIC_CHARACTERS = string.ascii_letters + string.digits + "-_"
3231
FLAGS = "-f j"
3332
METRIC_MAPPER = {
3433
"defined": "max",
@@ -42,6 +41,19 @@
4241
Metric = namedtuple("Metric", ["path", "value", "timestamp"])
4342

4443

44+
# vendored from nav.metrics.names.escape_metric_name
45+
def escape_metric_name(name):
46+
"""
47+
Escapes any character of `name` that may not be used in graphite metric
48+
names.
49+
"""
50+
if name is None:
51+
return name
52+
name = name.replace('\x00', '') # some devices have crazy responses!
53+
name = ''.join([c if c in LEGAL_METRIC_CHARACTERS else "_" for c in name])
54+
return name
55+
56+
4557
# parse comand line flags
4658
def parse_args():
4759
parser = argparse.ArgumentParser(description="Send dhcp stats to graphite")

0 commit comments

Comments
 (0)