Created
November 1, 2020 09:10
-
-
Save candlerb/4b06ad735e396f9cc0867c8549327865 to your computer and use it in GitHub Desktop.
Prometheus exporter for pmacct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import json | |
import subprocess | |
LABELS={} # add any static labels here, eg hostname | |
def export(metric, labels, value): | |
lstr = ",".join(("%s=\"%s\"" % (k,v) for k,v in labels.items())) | |
print("%s{%s} %d" % (metric, lstr, value)) | |
for aggregate in ["inbound", "outbound"]: | |
res = subprocess.run(["pmacct", "-s", "-p", "/tmp/%s.pipe" % aggregate, "-O", "json"], | |
stdout=subprocess.PIPE) | |
if res.returncode: | |
print(res.stdout) | |
res.check_returncode() | |
for line in res.stdout.splitlines(): | |
data = json.loads(line) | |
b = data.pop("bytes") | |
p = data.pop("packets") | |
data.update(LABELS) | |
data["aggregate"] = aggregate | |
export("flow_bytes", data, b) | |
export("flow_packets", data, p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment