Skip to content

Instantly share code, notes, and snippets.

@kgutwin
Last active June 29, 2021 22:07
Show Gist options
  • Save kgutwin/0993a354a4f40be45d0cbd79ff2897d8 to your computer and use it in GitHub Desktop.
Save kgutwin/0993a354a4f40be45d0cbd79ff2897d8 to your computer and use it in GitHub Desktop.
Simple Linux network speed monitor
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import time
import getopt
def usage():
print """Usage: %s [-d delay]
""" % sys.argv[0]
sys.exit(1)
try:
opts, files = getopt.getopt(sys.argv[1:], "d:gb")
except getopt.GetoptError:
usage()
DELAY=1.0
GRAPH=False
BITS=False
for opt, arg in opts:
if opt == '-d':
DELAY = float(arg)
elif opt == '-g':
GRAPH = True
elif opt == '-b':
BITS = True
IFS=files
COUNTERS={}
class Graph:
def __init__(self):
self.data = {}
self.width = 80
self.scale = 1024
def add(self, interface, rates):
if interface == 'lo':
interface = 'L'
elif interface.startswith('eth'):
interface = interface[-1]
elif interface.startswith('sit'):
interface = 's'
else:
print "should add case for interface", interface
interface = interface[0]
self.data[interface] = (rates[0], rates[2])
def draw(self):
s = [" "] * self.width
for interface in self.data:
self.scale = max(self.scale, *self.data[interface])
offs = int((self.data[interface][0] * (self.width / 2)) / self.scale)
if s[offs] == " ":
s[offs] = interface
else:
s[offs] = "*"
offs = int((self.data[interface][1] * (self.width / 2)) / self.scale)
offs += int(self.width / 2) - 1
if s[offs] == " ":
s[offs] = interface
else:
s[offs] = "*"
print "".join(s)
class UniGraph(Graph):
MAP = { (1, 1, 1, 1): "█",
(1, 1, 1, 0): "▛",
(1, 1, 0, 0): "▀",
(1, 0, 1, 1): "▙",
(1, 0, 1, 0): "▌",
(1, 0, 0, 0): "▘",
(0, 0, 1, 1): "▄",
(0, 0, 1, 0): "▖",
(0, 0, 0, 0): " "
}
def draw(self):
b = []
if not self.data.keys():
print
return
interface = self.data.keys()[0]
self.scale = max(self.scale, *self.data[interface])
p = lambda x: self.scale * (float(x) / self.width)
for i in range(self.width):
dla = p(i) <= self.data[interface][0] and 1 or 0
dlb = p(i + 0.5) <= self.data[interface][0] and 1 or 0
ula = p(i) <= self.data[interface][1] and 1 or 0
ulb = p(i + 0.5) <= self.data[interface][1] and 1 or 0
t = (dla, dlb, ula, ulb)
if t == (0, 0, 0, 0):
break
b.append(self.MAP.get(t, "?"))
print "".join(b)
if GRAPH:
g = UniGraph()
while True:
start = time.time()
fp = open("/proc/net/dev")
# skip first two lines
fp.next()
fp.next()
for line in fp:
interface, counters = line.split(':')
interface = interface.strip()
if IFS and interface not in IFS:
continue
counters = counters.split()
counters = [int(counters[i]) for i in (0,1,8,9)]
if interface in COUNTERS:
oldc = COUNTERS[interface]
rates = map((lambda x,y:float(x-y)/DELAY), counters, oldc)
if BITS:
rates[0] *= 8
rates[2] *= 8
if GRAPH:
g.add(interface, rates)
print ("%6s :" % interface), " ".join(["%10d" % r for r in rates])
COUNTERS[interface] = counters
if GRAPH:
g.draw()
else:
if len(IFS) != 1:
print
fp.close()
# and wait
time.sleep(DELAY - (time.time()-start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment