Differenze tra le versioni 2 e 3
4704
Commento:
|
4704
converted to 1.6 markup
|
Nessuna differenza riscontrata. |
1 #
2 # OLSR Topology Logger. Dumps dot files from the OLSR dot plugin (http://www.olsr.org).
3 # Usage: olsrtopologylogger.py [-t timeoutseconds] [-s sleepseconds] [{+,-}h]
4 # [-x n] hostname [port]
5 #
6 # -t timeoutseconds set the read timeout to timeoutseconds (default 120).
7 # -s sleepseconds pause sleepseconds between reads (default 300).
8 # +h use human-readable date and time for filenames.
9 # -h use floating point seconds since the epoch for filenames (default).
10 # -x n read and dump to file n times (-1=infinite) (default -1)
11 # hostname the hostname
12 # port the port (default 2004)
13 #
14 # example: olsrtopologylogger.py -x 3 -s 3 localhost
15
16 import telnetlib
17 import time
18 import sys
19
20 params={
21 'PORT':2004, #tcp
22 'HOST':'127.0.0.1',
23 'TIMEOUT':120,
24 'SLEEP':300,
25 'HUMANREADABLE':False,
26 'TIMES':-1,
27 'EXT':'.dot'
28 }
29
30 def readfromdotplugin(host='127.0.0.1',port='2004',timeout=120, exitonerror=True):
31 try:
32 dotcon=telnetlib.Telnet(host,port)
33 except:
34 sys.stderr.write("Error. Can't connect to %s:%s.\n" % (host,port))
35 if exitonerror:
36 sys.exit(2)
37 else:
38 return ""
39 dotoutput=""
40 dotoutput=dotcon.read_until('}',timeout)
41 dotoutput+='\n'
42 dotcon.close()
43 return dotoutput
44 #readfromdotplugin
45
46 def gettimestamp(humanreadable=False):
47 if humanreadable:
48 ts=time.asctime()
49 ts=ts.replace(' ','_')
50 ts=ts.replace(':','.')
51 else:
52 ts=time.time() #seconds since the epoch
53 return ts
54 #gettimestamp
55
56 def processoptions(argvlist,params):
57 if len(argvlist)<2:
58 instructions= "OLSR Topology Logger. Dumps dot files from the OLSR dot plugin (http://www.olsr.org).\n"
59 instructions+="Usage: %s [-t timeoutseconds] [-s sleepseconds] [{+,-}h] \n" % (argvlist[0])
60 instructions+="\t\t[-x n] hostname [port]\n\n"
61 instructions+="\t-t timeoutseconds\tset the read timeout to timeoutseconds (default %d).\n" % (params['TIMEOUT'],)
62 instructions+="\t-s sleepseconds \tpause sleepseconds between reads (default %d).\n" % (params['SLEEP'],)
63 instructions+="\t+h \tuse human-readable date and time for filenames"
64 if params['HUMANREADABLE']:
65 instructions+=" (default).\n"
66 else:
67 instructions+=".\n"
68 instructions+="\t-h \tuse floating point seconds since the epoch for filenames"
69 if not params['HUMANREADABLE']:
70 instructions+=" (default).\n"
71 else:
72 instructions+=".\n"
73 instructions+="\t-x n \tread and dump to file n times (-1=infinite) (default %s)\n" % (params['TIMES'],)
74 instructions+="\thostname \tthe hostname\n"
75 instructions+="\tport \tthe port (default %s)\n" % (params['PORT'],)
76 # print instructions
77 sys.stderr.write(instructions)
78 sys.exit(1)
79 else:
80 i=1
81 while i<len(argvlist):
82 arg=argvlist[i]
83 if arg=='+h':
84 params['HUMANREADABLE']=True
85 elif arg=='-h':
86 params['HUMANREADABLE']=False
87 elif arg=='-t':
88 try:
89 timeoutseconds=argvlist[i+1]
90 params['TIMEOUT']=int(timeoutseconds)
91 i+=1
92 except:
93 pass
94 elif arg=='-s':
95 try:
96 sleepseconds=argvlist[i+1]
97 params['SLEEP']=int(sleepseconds)
98 i+=1
99 except:
100 pass
101 elif arg=='-x':
102 try:
103 n=argvlist[i+1]
104 params['TIMES']=int(n)
105 i+=1
106 except:
107 pass
108 else:
109 params['HOST']=arg
110 try:
111 port=argvlist[i+1]
112 params['PORT']=int(port)
113 i+=1
114 except:
115 pass
116 i+=1
117 #processoptions
118
119
120 if __name__=="__main__":
121
122 processoptions(sys.argv,params)
123
124 try:
125 firstiteration=True
126 iterations=params['TIMES']
127 while iterations==-1 or iterations>0:
128 print "reading..."
129 dotoutput=readfromdotplugin(params['HOST'],params['PORT'],params['TIMEOUT'],firstiteration)
130 firstiteration=False
131 timestamp=gettimestamp(params['HUMANREADABLE'])
132
133 filename='%s' % (timestamp,)
134 filename+=params['EXT']
135
136 try:
137 try:
138 dotfile=file(filename,'r')
139 dotfile.close()
140 erstr="Warning! File %s already exists! Skipping...",(filename,)
141 # sys.stderr.write(erstr)
142 print erstr
143 except IOError:
144 dotfile=file(filename,'w')
145 dotfile.write(dotoutput)
146 dotfile.close()
147 print "%s: %s created." % (time.asctime(),filename)
148 except:
149 sys.stderr.write("I/O Error!!")
150 sys.exit(2)
151
152
153 print "sleeping %d seconds..." % (params['SLEEP'],)
154 time.sleep(params['SLEEP'])
155
156 if iterations>0:
157 iterations-=1
158 #while
159 except KeyboardInterrupt:
160 pass