Skip to content

Instantly share code, notes, and snippets.

@candlerb
Created November 1, 2020 09:10
Show Gist options
  • Save candlerb/4b06ad735e396f9cc0867c8549327865 to your computer and use it in GitHub Desktop.
Save candlerb/4b06ad735e396f9cc0867c8549327865 to your computer and use it in GitHub Desktop.
Prometheus exporter for pmacct
#!/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