[Tutor] Dynamic change of dictionaries

Lloyd Kvam pythontutor@venix.com
Wed, 19 Jun 2002 08:18:52 -0400


Two non-python thoughts about what you are trying to do:

MRTG provides perl and C code to issue SNMP requests to collect traffic
statistics, graph the stats, and display the graphs in a web page.
http://people.ee.ethz.ch/~oetiker/webtools/mrtg/
MRTG: The Multi Router Traffic Grapher
This may be over-kill for your purposes.

If you are tracking web usage, the squid proxy server would collect stats
while also providing caching.
http://www.squid-cache.org/
Squid Web Proxy Cache

Marc wrote:

> Hi people,
> I need to make a simple monitor for my network.
> I need to monitor how much each machine is using the bandwidth.
> The first tool that comes to mind is tcpdump. So far Im able to see how
> much a given IP shows up with:
> # tcpdump -nq -i eth0 dst host 10.15.50.3 and port 80
> but i need to show it "graphically".
> 
> Some examples to show what I mean:
> 
> The following program fakes a tcpdump output for easy testing.
> # fakeDump.py
> 
> 18:57:28.1024437448 10.0.0.8.2279 > 10.0.0.3.80: tcp 0 (DF)
> 18:57:28.1024437448 10.0.0.19.1792 > 10.0.0.3.80: tcp 0 (DF)
> 18:57:29.1024437449 10.0.0.20.1570 > 10.0.0.3.80: tcp 0 (DF)
> 18:57:29.1024437449 10.0.0.22.2045 > 10.0.0.3.80: tcp 0 (DF)
> 18:57:30.1024437450 10.0.0.6.2114 > 10.0.0.3.80: tcp 0 (DF)
> 18:57:30.1024437450 10.0.0.8.1487 > 10.0.0.3.80: tcp 0 (DF)
> 18:57:31.1024437451 10.0.0.8.1653 > 10.0.0.3.80: tcp 0 (DF)
> 18:57:31.1024437451 10.0.0.14.2290 > 10.0.0.3.80: tcp 0 (DF)
> 18:57:32.1024437452 10.0.0.10.1523 > 10.0.0.3.80: tcp 0 (DF)
> 18:57:32.1024437452 10.0.0.9.2290 > 10.0.0.3.80: tcp 0 (DF)
> 
> The interesting part here is the ip number: 
> I need is to tell how much 10.0.0.8 shows up.
> And 10.0.0.6 .
> And 10.0.0.22.
> And .... You get the idea.
> So I can show it like a gauge. Screenshots :)
> Pay attention to 10.0.0.6 and how it "disappear" due to no traffic:
> 
> 10.0.0.8   ######
> 10.0.0.6   ####
> 10.0.0.22  ##############
> 10.0.0.99  ########	
> 
> 
> Some minutes later:
> 
> 10.0.0.8   #######################################
> 10.0.0.6   #
> 10.0.0.22  #########
> 10.0.0.99  #########################	
> 
> Some minutes later:
> 
> 10.0.0.8   #######################################
> 10.0.0.22  #########
> 10.0.0.99  #########################	
> 
> 
> 
> I was thinking about a dictionary.
> 
> 'IP' : number of show up in X minutes
> dict={'10.0.0.6':5,'10.0.0.22':33,'10.0.0.8':42} 
> 
> 
> Given the code below, how do I change the dict dynamically with a output
> like the command above?
> 
> Thanks in advance for _any_ help.
> 
> 
> ####################################################################
> # simulates a traffic monitor
> import time,os,random
> 
> header="\t\t\tTraffic Monitor(CTRL+C to exit)"
> dict={'10.0.0.6':5,'10.0.0.22':33,'10.0.0.8':42} 
> 
> while (1):
> 	os.system('clear')
> 	print header
> 	print time.ctime(time.time())
> 	try:
> 		for item in dict:
> 			print item,'\t'+'#'*dict[item]
> 			dict[item]=dict[item]-1
> 			if dict[item]< 0: # delete idle IPs.
> 			   del( dict[item]) #  = random.randrange (1,65)
> 	except RuntimeError:
> 			pass
> 	time.sleep(1)
> ###################################################################
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> #Fakes a tcpdump output
> #tcpdump -nq -i eth0 dst host 10.0.0.3 and port 80 
> #something like:
> #18:31:27.854995 10.0.0.1.33209 > 10.0.0.3.80: tcp 0 (DF)
> 
> import time,random
> 
> while (1):
> 	print "%s 10.0.0.%s.%s > 10.0.0.3.80: tcp 0 (DF)"%(time.strftime('%X.%s'),random.randrange(1,25),random.randrange(1024,2500))
> 	time.sleep(0.5)
> 	
> 
> 
> ------------------------------------------------------------------------
> 
> import time,os,random
> header="\t\t\t\tMonitor Traffic"
> dict={'10.0.0.6':5,'10.0.0.22':3,'10.0.0.3':2}
> while (1):
> 	os.system('clear')
> 	print header
> 	print time.ctime(time.time())
> 	try:
> 		for item in dict:
> 			print item,'\t'+'#'*dict[item]
> 			dict[item]=dict[item]-1
> 			if dict[item]< 0: 
> 			    dict[item] = random.randrange (1,65)
> 	except RuntimeError:
> 			pass
> 	time.sleep(1)
> 


-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice: 
603-443-6155
fax: 
801-459-9582