Development

Showing transfer rate on the console

A program to “graphically” show on a linux console the transfer rate on each interface

Credit due to the unknown original author of this program way back in time…

#!/usr/bin/python
 
# netrate - a program to "graphically" show on a linux console
# the transfer rate on each interface
# also tested on rxvt
 
# it will display the transfer rate for the last second (coming soon),
# and the transfer rate for the last SECONDS seconds.
#
 
NUM_SAMPLES=5                   # number of samples per second
NUM_SECONDS=5                   # number of seconds
MIN_RATE=1024.0                 # max_rate does not go below this value
 
# terminal settings
TERM_HOME='\x1b[H'
TERM_CLR_EOL='\x1b[K'
TERM_CLR_SCREEN='\x1b[J'
TERM_IN_COLOR='\x1b[41m'                # red
TERM_OUT_COLOR='\x1b[42m'               # green
TERM_NORMAL_COLOR='\x1b[m'              # no attributes
 
TERM_WIDTH=80
 
from string import split
from time import time,sleep
from sys import stdin,stdout,argv,exit
from getopt import getopt
 
 
# help
def help():
    print "Usage:\n\t%s -h\n"%argv[0]
    print "\t%s [-w width] [-s seconds] [-n num_samples]\n"%argv[0]
    exit()
 
# main
options,junk=getopt(argv[1:],"hw:s:n:")
del junk
for i in options:
    if(i[0]=='-h'):
        help()
    if(i[0]=='-w'):
        try:
            TERM_WIDTH=int(i[1])
        except:
            help()
    if(i[0]=='-s'):
        try:
            NUM_SECONDS=int(i[1])
        except:
            help()
    if(i[0]=='-n'):
        try:
            NUM_SAMPLES=int(i[1])
        except:
            help()
del options
 
delay=1.0/NUM_SAMPLES
 
statfile=open('/proc/net/dev','r')
# first get the current status...
tmp=statfile.readlines()
statfile.close()
del statfile
tmp=tmp[2:]
num_interfaces=len(tmp)
interfaces=[0,]*num_interfaces
initial_in=[]
initial_out=[]
max_rate=MIN_RATE
 
for i in range(num_interfaces):
    tokens=split(tmp[i][7:])
    interfaces[i]=split(tmp[i][:6])[0]
    initial_in.append(float(tokens[0]))
    initial_out.append(float(tokens[8]))
 
total=NUM_SAMPLES*NUM_SECONDS
times=[time()-1.0]*total
in_values=[]
out_values=[]
 
for i in range(total):
    in_values.append(initial_in[:]*num_interfaces)
    out_values.append(initial_out[:]*num_interfaces)
 
del initial_in,initial_out
 
current=0
last=1
 
 
# print samples[0][1][0][1]
# print interfaces
 
# I keep a circular buffer, so samples[current] is the current sample, and
# samples[last] is the earliest sample, which will be overwritten next time
 
stdout.write(TERM_HOME+TERM_CLR_SCREEN)
 
while 1:
    statfile=open('/proc/net/dev')
    tmp=statfile.readlines()
    tmp=tmp[2:]
    statfile.close()
 
    times[current]=time()
    for i in range(num_interfaces):
        tokens=split(tmp[i][7:])
        in_values[current][i]=float(tokens[0])
        out_values[current][i]=float(tokens[8])
 
    sleep(delay)
    stdout.write(TERM_HOME)
 
    stdout.write('Max Rate: %10.2f\n'%max_rate)
    for i in range(num_interfaces):
        time_diff=(times[current]-times[last]) * 1024.0
        stdout.write('%-10s: in:%10.2f kb/sec   out:%10.2f kb/sec'\
            %(interfaces[i],\
            (in_values[current][i]-in_values[last][i])/time_diff,\
            (out_values[current][i]-out_values[last][i])/time_diff))
        stdout.write(TERM_CLR_EOL+'\n')
        in_rate=(in_values[current][i]-in_values[last][i])/time_diff
        out_rate=(out_values[current][i]-out_values[last][i])/time_diff
        max_rate=max(in_rate,out_rate,max_rate)
        stdout.write(TERM_IN_COLOR+(' '*int(TERM_WIDTH*in_rate/max_rate))\
            +TERM_NORMAL_COLOR+TERM_CLR_EOL+'\n')
        stdout.write(TERM_OUT_COLOR+(' '*int(TERM_WIDTH*out_rate/max_rate))\
            +TERM_NORMAL_COLOR+TERM_CLR_EOL+'\n')
    stdout.flush()
    current=current+1
    last=last+1
    if(current==total):
        current=0
        max_rate=max(max_rate*0.90,MIN_RATE)
    else:
        if(last==total):
            last=0
comments powered by Disqus