#!/usr/bin/python
# coding=utf-8
 
import time
 
def net_get_state(iface):
  return time.time(),map(int, [ i for i in open('/proc/net/dev').readlines() if i.find(iface)>0 ][0].replace(':',' ').split()[1:][::8] )
 
def net_load(iface):
  '''(rx, tx) kb/s'''
  t1,state1=net_get_state(iface)
  time.sleep(1)
  t2,state2=net_get_state(iface)
  return map( lambda i,j: 1.0*(i-j)/(t2-t1)/1024.0, state2, state1)

def cellcolor(direct):
        if direct < 200.0:
                cellcol = "'#00ff00'"
	elif direct > 500.0:
		cellcol = "'#ff0000'"
	else:
		cellcol = "'#ffff00'"
	return cellcol


def showspd(ifc): 
	a=net_load(ifc)
	spup = round(a[1], 1)
        spdw = round(a[0], 1)
	cellup = cellcolor(spup)
	celldw = cellcolor(spdw)
	print("<tr>")
	print("<td bgcolor = " + cellup + ">")
	print('<p>' + ifc + ' Up:  ' + str(spup) + 'kB/s' +  '</p>')
	print("</td>")
	print("</tr>")
	print("<tr>")
	print("<td bgcolor = " + celldw + ">")
	print('<p>' + ifc + ' Down:' + str(spdw) + 'kB/s' + '</p>')
        print("</td>")
        print("</tr>")
        print("<tr>")
        print("<td><br>")
	print("</td>")
	print("</tr>")


print("Content-type: text/html\n")
print("<html>")
print("<header>")
print("<title>Proxy configurator</title>")
print("</header>")
print("<body>")

print("""<table align="left" border="1" cellpadding="1" cellspacing="1" style="width:250">""")
print("<tbody>")

showspd('eth0')
showspd('eth1')
showspd('eth2')
showspd('eth3')
showspd('eth4')
print("</tbody>")
print('</table>')
print('<meta http-equiv="refresh" content="5">')
print("</body>")
print("</html>")

