Skip to content

Instantly share code, notes, and snippets.

@kimsyversen
Last active December 24, 2016 17:46
Show Gist options
  • Save kimsyversen/432af8f32a4110f190e5636305c00b85 to your computer and use it in GitHub Desktop.
Save kimsyversen/432af8f32a4110f190e5636305c00b85 to your computer and use it in GitHub Desktop.
How to collect network statistics from MacOS and insert in influxdb (for use in grafana) (verified on 10.9)
#!/usr/bin/python
"""
Prereqs:
sudo pip install influxdb
sudo pip install simplejson
Grafana queries
SELECT derivative(mean("value"),1s) FROM "rx_bytes" WHERE $timeFilter GROUP BY time($interval) fill(null)
SELECT derivative(mean("value"),1s) FROM "tx_bytes" WHERE $timeFilter GROUP BY time($interval) fill(null)
"""
from influxdb import InfluxDBClient
import time
import json, os
import subprocess
client = InfluxDBClient('localhost', 8086, 'root', 'root', 'databasename')
while True:
tx = subprocess.check_output("netstat -i -I en0 -b | grep kimmba | cut -d' ' -f14", shell=True)
rx = subprocess.check_output("netstat -i -I en0 -b | grep kimmba | cut -d' ' -f16", shell=True)
payload_tx = [
{
"measurement" : "tx_bytes",
"fields": {
"value" : int(tx),
}
}
]
payload_rx = [
{
"measurement" : "rx_bytes",
"fields": {
"value" : int(rx)
}
}
]
client.write_points(payload_tx)
client.write_points(payload_rx)
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment