Skip to content

Commit

Permalink
Bundle nav.metrics.names.escape_metric_name
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Mar 7, 2024
1 parent a3aa1e8 commit a8f5128
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions contrib/scripts/isc_dhpcd_graphite/isc_dhpcd_graphite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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",
Expand All @@ -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")
Expand Down

0 comments on commit a8f5128

Please sign in to comment.