Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f34e308

Browse files
hmpflunkwill42
andauthoredNov 29, 2022
Improve dhcpd pool script
* Stop crashing on invalid location * Show defaults in script-argument help-text * Add README for script Co-authored-by: Morten Brekkevold <morten.brekkevold@sikt.no>
1 parent fe217e2 commit f34e308

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
======================================
2+
Usage and notes for isc_dhcpd_graphite
3+
======================================
4+
5+
This script needs python3.
6+
7+
For arguments, try::
8+
9+
isc_dhcpd_graphite.py --help
10+
11+
The script runs ``dhcpd_pool`` (full path given with -C) with the flag
12+
``-f j`` to make it emit json.
13+
14+
Building the prefix
15+
===================
16+
17+
The dotted-path that graphite uses to store data is controlled by the prefix
18+
argument to the script (-p), the optional location argument (-l) and what
19+
``dhcpd_pool`` returns as its ``location`` key.
20+
21+
The default prefix is "nav.dhcp".
22+
23+
If ``-p`` is "nav.bloviate", ``-l`` is not set and the ``location`` in the json
24+
is "vlan1" the resulting graphite path is ``nav.bloviate.vlan1``.
25+
26+
27+
Assumptions about Location
28+
--------------------------
29+
30+
The script assumes that the value of the ``location``-key contains a vlan-name
31+
of the form "vlanNUMBER", regex ``vlan\d+``. If this is not the case, a warning
32+
is issued on stderr and that row of results is not passed on to graphite.
33+
34+
The location-value is normalized so that a value of "Student vlan2 new" is sent
35+
to graphite as "vlan2".

‎contrib/scripts/isc_dhpcd_graphite.py ‎contrib/scripts/isc_dhpcd_graphite/isc_dhpcd_graphite.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,20 @@ def parse_args():
5555
"-f",
5656
"--config-file",
5757
type=pathlib.Path,
58-
help="Complete path to dhcpd-pools config-file. Usually located in /etc/dhcpd/",
58+
help="Complete path to dhcpd-pools config-file. Usually located in /etc/dhcpd/. Default: %(default)s",
5959
default=DEFAULT_CONFIG_FILE,
6060
)
6161
parser.add_argument(
6262
"-C",
6363
"--command",
64-
help="Path to dhcpd-pools command",
64+
help="Path to dhcpd-pools command. Default: %(default)s",
6565
type=pathlib.Path,
6666
default=DEFAULT_CMD_PATH,
6767
)
6868
parser.add_argument(
6969
"-p",
7070
"--prefix",
71-
help="Path prefix to use for the metric, overriding the default",
71+
help="Path prefix to use for the metric, overriding the default. Default: %(default)s",
7272
type=str,
7373
default=DEFAULT_PREFIX,
7474
)
@@ -86,7 +86,7 @@ def parse_args():
8686
parser.add_argument(
8787
"-P",
8888
"--protocol",
89-
help="Protocol to use to send to graphite server",
89+
help="Protocol to use to send to graphite server. Default: %(default)s",
9090
choices=protocol_choices,
9191
default=str(DEFAULT_PROTOCOL),
9292
type=str,
@@ -159,6 +159,8 @@ def _tuplify(jsonblob, prefix):
159159
output = list()
160160
for vlan_stat in data:
161161
vlan = _clean_vlan(vlan_stat["location"])
162+
if not vlan:
163+
continue
162164
for key, metric in METRIC_MAPPER.items():
163165
path = f"{prefix}.{vlan}.{metric}"
164166
value = vlan_stat[key]
@@ -168,7 +170,10 @@ def _tuplify(jsonblob, prefix):
168170

169171
def _clean_vlan(location):
170172
regex = re.search("vlan\d+", location)
171-
return regex.group()
173+
if regex:
174+
return regex.group()
175+
sys.stderr.write(f"No vlan found in location {location}: invalid\n")
176+
return None
172177

173178

174179
# send the data

0 commit comments

Comments
 (0)
Please sign in to comment.