Versione 1 del 2006-10-22 14:53:04

Nascondi questo messaggio
Italiano English
Modifica History Actions

olsrtopologylogger.py

   1 import telnetlib
   2 import time
   3 import sys
   4 
   5 params={
   6 'PORT':2004, #tcp
   7 'HOST':'127.0.0.1',
   8 'TIMEOUT':120,
   9 'SLEEP':300,
  10 'HUMANREADABLE':False,
  11 'TIMES':-1,
  12 'EXT':'.dot'
  13 }
  14 
  15 def readfromdotplugin(host='127.0.0.1',port='2004',timeout=120):
  16 	try:
  17 		dotcon=telnetlib.Telnet(host,port)
  18 	except:
  19 		print "Error. Can't connect to %s:%s.\n" % (host,port)
  20 		sys.exit(2)
  21 	dotoutput=""
  22 	dotoutput=dotcon.read_until('}',timeout)
  23 	dotoutput+='\n'
  24 	dotcon.close()
  25 	return dotoutput
  26 #readfromdotplugin
  27 	
  28 def gettimestamp(humanreadable=False):
  29 	if humanreadable:
  30 		ts=time.asctime()
  31 		ts=ts.replace(' ','_')
  32 		ts=ts.replace(':','.')
  33 	else:
  34 		ts=time.time()	#seconds since the epoch
  35 	return ts
  36 #gettimestamp
  37 
  38 def processoptions(argvlist,params):
  39 	if len(argvlist)<2:
  40 		instructions= "OLSR Topology Logger. Creates dot files from the dot plugin (http://www.olsr.org)\n"
  41 		instructions+="Usage: %s [-t timeoutseconds] [-s sleepseconds] [{+,-}h] \n" % (argvlist[0])
  42 		instructions+="\t\t[-x n] hostname [port]\n\n"
  43 		instructions+="Where:\n"
  44 		instructions+="\t-t timeoutseconds\tset the read timeout to timeoutseconds (default %d).\n" % (params['TIMEOUT'],)
  45 		instructions+="\t-s sleepseconds  \tpause sleepseconds between reads (default %d).\n" % (params['SLEEP'],)
  46 		instructions+="\t+h               \tuse human-readable date and time for filenames"
  47 		if params['HUMANREADABLE']:
  48 			instructions+=" (default).\n"
  49 		else:
  50 			instructions+=".\n"
  51 		instructions+="\t-h               \tuse floating point seconds since the epoch for filenames"
  52 		if not params['HUMANREADABLE']:
  53 			instructions+=" (default).\n"
  54 		else:
  55 			instructions+=".\n"
  56 		instructions+="\t-x n           \tread and dump to file n times (-1=infinite) (default %s)\n" % (params['TIMES'],)
  57 		instructions+="\thostname         \tthe hostname\n"
  58 		instructions+="\tport             \tthe port (default %s)\n" % (params['PORT'],)
  59 		print instructions
  60 		sys.exit(1)
  61 	else:
  62 		i=1
  63 		while i<len(argvlist):
  64 			arg=argvlist[i]
  65 			if arg=='+h':
  66 				params['HUMANREADABLE']=True
  67 			elif arg=='-h':
  68 				params['HUMANREADABLE']=False
  69 			elif arg=='-t':
  70 				try:
  71 					timeoutseconds=argvlist[i+1]
  72 					params['TIMEOUT']=int(timeoutseconds)
  73 					i+=1
  74 				except:
  75 					pass
  76 			elif arg=='-s':
  77 				try:
  78 					sleepseconds=argvlist[i+1]
  79 					params['SLEEP']=int(sleepseconds)
  80 					i+=1
  81 				except:
  82 					pass
  83  			elif arg=='-x':
  84 				try:
  85 					n=argvlist[i+1]
  86 					params['TIMES']=int(n)
  87 					i+=1
  88 				except:
  89 					pass
  90 			else:
  91 				params['HOST']=arg
  92 				try:
  93 					port=argvlist[i+1]
  94 					params['PORT']=int(port)
  95 					i+=1
  96 				except:
  97 					pass
  98 			i+=1
  99 #processoptions		
 100 if __name__=="__main__":
 101 	
 102 	processoptions(sys.argv,params)
 103 	
 104 	try:
 105 		iterations=params['TIMES']	
 106 		while iterations==-1 or iterations>0:
 107 			dotoutput=readfromdotplugin(params['HOST'],params['PORT'],params['TIMEOUT'])
 108 			timestamp=gettimestamp(params['HUMANREADABLE'])
 109 			
 110 			filename='%s' % (timestamp,)
 111 			filename+=params['EXT']
 112 			
 113 			dotfile=file(filename,'w')
 114 			dotfile.write(dotoutput)
 115 			dotfile.close()
 116 			
 117 			time.sleep(params['SLEEP'])
 118 			
 119 			if iterations>0: 
 120 				iterations-=1
 121 		#while
 122 	except KeyboardInterrupt:
 123 		pass