Last active
October 21, 2017 19:03
-
-
Save athoik/6102727 to your computer and use it in GitHub Desktop.
A simple bandwidth monitoring using /proc/net/dev
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/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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you for sharing