Skip to content

Instantly share code, notes, and snippets.

@athoik
Last active October 21, 2017 19:03
Show Gist options
  • Save athoik/6102727 to your computer and use it in GitHub Desktop.
Save athoik/6102727 to your computer and use it in GitHub Desktop.
A simple bandwidth monitoring using /proc/net/dev
#!/usr/bin/env python
import time
def getRxTx():
rx = 0L
tx = 0L
with open("/proc/net/dev") as dev:
data = dev.read()
data = data.split("\n")[2:]
for iface in data:
iface = iface.split()
if not iface: break
rx += long(iface[1])
tx += long(iface[9])
return (rx,tx)
def getMetric(value):
gauge = "B"
if value > 1024:
gauge = "KB"
value = round(value/1024.0,2)
if value > 1024:
gauge = "MB"
value = round(value/1024.0,2)
return "%s %s" % (value, gauge)
(rx, tx) = getRxTx()
time.sleep(1)
(nrx, ntx) = getRxTx()
print "D: %s/s\nU: %s/s" % ( getMetric(nrx-rx), getMetric(ntx-tx))
@hyptechdev2015
Copy link

thank you for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment