forked from wevertoncordeiro/TraceCollection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze1_convert_from_directory_to_swarm.py
379 lines (304 loc) · 13.1 KB
/
analyze1_convert_from_directory_to_swarm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/python
'''
First step to process the monitoring outputs.
This scripts converts from session directory structure format to session swarm format (one file per swarm).
'''
import sys, time, getopt, urllib, random, struct, commands, os, platform, operator, socket
from analyze0_main import *
PL_FILE_NAME = "00_data_source/servers_status.txt"
CONVERT_FROM_SWARM_TO_PEERS = True
PLOT_SCATTER_PEERS_OVER_TIME = True
SPLIT_MONITORS = True
delaDir = Dirs().delayed
def print_usage():
print ""
print "This script converts Tracker lens results from a dir (many files) to SWARM format (one file per swarm, one peerlist per line)"
#print "This script first creates an output file for session given swarm from session set files (inside session chosen directory) from monitoring."
#print "Then, the 'convert2' script converts the resulting file to session_line format."
print ""
print "USAGE:"
print "-h --help show this usage help message"
print "-f --file=[file] input file (html) with magnetlinks"
print "-d --dir=[dir] list of directories ($dir) that contains monitoring outputs separated by ','. (find $dir -file_name_base '*.gz') "
print "-p --pos=[number] position of the first swarm "
print "-n --num=[number] number of swarms to analyze"
print "-t --time=[epoch] monitoring start"
print "-s --sort To sort or not to sort? default is not to sort"
print "-l --peerlistSize max peer list Size. Value=0 means no limit. Default value=0 "
print "-r --resolution. {0 .. 100}% Value=0 means none. Default value=100 (%, full resolution) "
#print "-m --mon=[number] number of monitoring results to analyze 1<=x<=50"
print ""
def is_float_try(str_):
try:
float(str_)
return True
except ValueError:
return False
if __name__ == '__main__':
# HEADER
print "\n"
script_name = os.path.basename(__file__)
print script_name.upper()
print "-" * len(script_name)
# INITIALIZE VARIABLES
inputFileName = ""
inputDirs = ""
swarm_pos = 0
swarm_number = 1
monitoringStartEpoch = 0
resolution = 100
toSort = True
maxPeerListSize=0
#processa parametros
try:
optlist, args = getopt.gnu_getopt(sys.argv[1:], 'hf:d:p:n:t:sl:r:',
['help', 'file=', 'dir=', 'pos=', 'num=', 'time=', 'sort', 'peerListSize=', 'resolution='])
for o, a in optlist:
if o in ["-h", "--help"]:
print_usage()
sys.exit(0)
elif o in ["-f", '--file']:
inputFileName = a
elif o in ["-d", '--dir']:
inputDirs = a
elif o in ["-p", '--pos']:
swarm_pos = int(a)
elif o in ["-n", '--num']:
swarm_number = int(a)
elif o in ["-t", '--time']:
monitoringStartEpoch = int(a)
elif o in ["-s", "--sort"]:
toSort = True
elif o in ["-l", "--peerListSize"]:
maxPeerListSize = int(a)
elif o in ["-r", "--resolution"]:
resolution = int(a)
except Exception, e:
print "Exception:", e
print "Please, use -h or --help for instructions on how to use this script."
sys.exit(-1)
if inputDirs == "" or inputFileName == "":
print "input dir or output file is null."
print "Please, use -h or --help for instructions on how to use this script."
sys.exit(-1)
#le arquivo
countTorrents = 0
countTorrentsTrackers = 0
i = 0
torrents = list();
trackers = {};
try:
print "Opening_file:%s" % inputFileName
fData = open(inputFileName, 'r')
print "Loading_file:%s" % inputFileName
sData = fData.read().split('\n')
fData.close()
for line in sData:
print ""
i = i + 1
if line.partition('magnet:')[2] == '':
pass
# if debug:
# print "blank_line..."
else:
print "line_being_processed:", line
countTorrents += 1
magnetlink = line.partition('magnet:')[2].partition('"')[0]
#print magnetlink
infohash_hex = magnetlink.partition('btih:')[2].partition('&')[0]
torrent = Torrent(infohash_hex)
print "infohash_hex:", infohash_hex
#procura pelos enderecos de rastreadores
it = 0
while True:
it = it + 1
tracker = magnetlink.partition('&tr=')[2]
if tracker == '':
break
else:
countTorrentsTrackers += 1
magnetlink = tracker
tracker = tracker.partition('&')[0]
tracker = tracker.replace('%3A', ':').replace('%2F', '/')
# tracker = tracker.replace('udp:','http:')
print "tracker:", it, " end:", tracker
# tr = tr(infohash_url, tracker, 0)
# print tr
torrent.trackers.append(tracker)
tracker_ = None
if not tracker in trackers:
tracker_ = Tracker(tracker)
else:
tracker_ = trackers[tracker]
tracker_.torrents.append(infohash_hex)
trackers[tracker] = tracker_
torrents.append(torrent)
except Exception, e:
print "error_during_file_processing:", inputFileName, " line:", i, " exception:", e
sys.exit()
print "file_processing_finish!"
print "NUMBER_OF_TORRENTS:" + str(countTorrents) + " NUMBER_OF_TORRENTS_AND_TRACKERS:" + str(countTorrentsTrackers)
for t in torrents:
pass
#print session_begin.infohash, " ", len(session_begin.trackers), " ", session_begin.trackers
for t in trackers:
tracker = trackers[t]
#print session_begin, " ", len(tracker.torrents), " ", tracker.torrents
if swarm_pos + swarm_number > countTorrents:
print_usage()
print "_-----_---_-_---_-_-__-_--_----_--_-_____---_"
print " Swarm pos + number must be < ", countTorrents
print " Swarm pos : ", swarm_pos
print " Swarm number: ", swarm_number
sys.exit()
print "\n\n\n"
print "+-------------------------------------"
print " SETTINGS"
print "+-------------------------------------"
print " SPLIT MONITORS : ", SPLIT_MONITORS
print ""
print "+-------------------------------------"
print " INPUT PARAMETERS"
print "+-------------------------------------"
print " Swarm Input file : ", inputFileName
print " Input dirs : ", inputDirs
print " Swarm position : ", swarm_pos
print " Swarm number : ", swarm_number
print " Resolution : ", resolution
#print " Swarms: ", torrents[swarm_pos:swarm_pos + swarm_number]
print " To sort? : ", toSort
print ""
print "+-------------------------------------"
print " CALCULATED PARAMETERS"
print "+-------------------------------------"
monitors = []
try:
f = open(PL_FILE_NAME)
sentinelsFileName = "01_data_servers/servers_sentinels.txt"
monitorsFileName = "01_data_servers/servers_monitors.txt"
f_sentinels = open(sentinelsFileName, 'w')
f_monitors = open(monitorsFileName, 'w')
for l in f:
# print l
monitor = Monitor(l)
if SPLIT_MONITORS:
monitors.append(monitor)
if monitor.isSentinel:
f_sentinels.write(monitor.host + "\n")
else:
f_monitors.write(monitor.host + "\n")
if not SPLIT_MONITORS:
monitor = Monitor()
monitor.line = "all"
monitor.startTimeEpoch = monitoringStartEpoch
monitors.append(monitor)
f.close()
f_sentinels.close()
f_monitors.close()
monitoringStartEpoch = min(monitors, key=lambda m: m.startTimeEpoch).startTimeEpoch
print " Sentinels file name :", sentinelsFileName
print " Monitors file name :", monitorsFileName
print " Start time epoch :", monitoringStartEpoch
except Exception as e:
print "[Warning] some file with sentinel and/monitor were not found.", e
print ""
print "+-------------------------------------"
print " PROCESSING DATA"
print "+-------------------------------------"
s = 0
c = 0
timec = 0
times = 0
sumSize_f = 0
sumSize_s = 0
count_torrent = 1
for t in torrents[swarm_pos:swarm_pos + swarm_number]:
print "\n TORRENT (%d/%d) : %s \n"%(count_torrent, len(torrents[swarm_pos:swarm_pos + swarm_number]), t)
count_torrent += 1
#temporary files
tempName = t.tempFileName
procName = t.procFileName
sortName = t.get_swarm_file_name(resolution)
cmd = "rm %s " %(tempName)
run_cmd(cmd, 1)
count_slot = -1
hash_count_monitor = 0
# this loop is necessary for correcting monitors clock delays
for m in monitors:
print "\n\tMONITOR:", m
#pass
#Compatibilidade Linux/MacOS
#Linux
tempName2 = delaDir + t.infohash + "_m_%02d.txt" % hash_count_monitor
hash_count_monitor += 1
cmd = "rm %s "%(tempName2)
run_cmd(cmd, 2, False)
gzcat = " zcat "
if "Darwin" in platform.platform():
#MacOS
gzcat = " gzcat "
for inputDir in inputDirs.split(','):
#print "\session_begin\tMonitor:", m.monitorName
#print "\session_begin\tDir :", inputDir
#print "\session_begin\tTorrent:", session_begin.infohash
principal = " grep %s | " % t.infohash
principal += " while read line; do peer_id=`echo $line | cut -d \" \" -f 6-`; session_begin=`echo $line | cut -d \" \" -f 4`; echo $session_begin $peer_id; done "
if monitor.line == "all":
# TODO: FIX HACK FOR THE EXPERIMENT NAME
cmd = ' find ' + inputDir + ' -name "*%s*.gz"' % "2018-11-30_17-"
else:
cmd = ' find ' + inputDir + ' -name "*%s*.gz"' % m.monitorName
cmd += ' | xargs ' + gzcat + " | " + principal + " >> " + tempName2
run_cmd(cmd, 2, True)
#print "open files: ", tempName2, tempName
fin = open(tempName2, 'r')
fout = open(tempName, 'a')
res_total = 0
res_count_1 = 0
res_count_2 = 0
for line_fin in fin:
try:
epoch_original = float(line_fin.split(" ")[0])
except ValueError:
if is_float_try(line_fin.split(" ")[1]):
epoch_original = float(line_fin.split(" ")[1])
else:
# hack to read output from old monitor versions...
print "error value while processing file."
print "file in : ", tempName2
print "file out:", tempName
print "line in : ", line_fin
continue
#epoch_original = float(line_fin.split(" ")[1])
except:
print "error while processing file."
print "file in : ", tempName2
print "file out:", tempName
print "line in : ", line_fin
sys.exit()
epoch_delayed = epoch_original + m.client_minus_server_epoch
resolution_value = random.randint(0, 100)
#print "resolution_value: %d resolution: %d" % (resolution_value, resolution)
res_total +=1
if resolution_value <= resolution:
res_count_1 += 1
fout.write("%f %s\n" % (epoch_delayed, line_fin.split(" ")[1:]))
else:
res_count_2 += 1
fin.close()
fout.close()
if res_total > 0:
print "res_total: %d (%f) res_count_1 (file): %d (%f) res_count_2 (ignored): %d (%f)" %(res_total, (res_total*1.0/res_total), res_count_1, (res_count_1*1.0/res_total), res_count_2, (res_count_2*1.0/res_total))
else:
print "res_total == 0"
cmd = "rm %s"%tempName2
run_cmd(cmd, 2, False)
#print "close files: ", tempName2, tempName
cmd = " sort -n -k 1 " + tempName + " > " + sortName
run_cmd(cmd, 2, True)
print "+-------------------------------------"
print " END"
print "+-------------------------------------"
print ""
print "%s %s finished!" % (now_str(), script_name)
print ""