This repository has been archived by the owner on Aug 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tool.py
executable file
·669 lines (523 loc) · 19.4 KB
/
tool.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#!/usr/bin/env python
#
# Copyright (c) 2011 Simone Basso <bassosimone@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
'''
This is the script I use to manage and analyze the set of Neubot
databases collected over time. This is just for personal use and
so might not work for you out of the box.
'''
import collections
import decimal
import getopt
import json
import syslog
import sqlite3
import time
import bz2
import re
import sys
import os
from matplotlib import pyplot
from matplotlib import dates
sys.path.insert(0, '../neubot')
from neubot.database import DATABASE
from neubot.database import migrate
class __FakeGeoIP:
''' Fake geoip provider '''
def record_by_addr(self, address):
''' Fake record_by_addr method '''
def org_by_addr(self, address):
''' Fake org_by_addr method '''
def __get_geoloc_city():
''' Return a geoloc city provider '''
try:
import GeoIP
return GeoIP.open('/usr/local/share/GeoIP/GeoLiteCity.dat',
GeoIP.GEOIP_STANDARD)
except:
return __FakeGeoIP()
def __get_geoloc_asn():
''' Return a geoloc asn provider '''
try:
import GeoIP
return GeoIP.open('/usr/local/share/GeoIP/GeoIPASNum.dat',
GeoIP.GEOIP_STANDARD)
except:
return __FakeGeoIP()
GEOLOC_CITY = __get_geoloc_city()
GEOLOC_ASN = __get_geoloc_asn()
def __connect(path):
'''
This function connects to the database at @path and sets up
sqlite3.Row as row factory, so that we can treat results both
as tuples and as dictionaries.
'''
connection = sqlite3.connect(path)
connection.row_factory = sqlite3.Row
return connection
def __info(connection):
'''
This function reads information on the database referenced
by @connection, i.e. reads and prints ``config`` table.
'''
result = {}
cursor = connection.cursor()
cursor.execute('SELECT * FROM config;')
for name, value in cursor:
result[name] = value
return result
def __decompress(path):
'''
This function decompress a compressed Neubot database
on the current working directory
'''
outputpath = os.path.basename(path).replace('.bz2', '')
inputfp = bz2.BZ2File(path)
outputfp = open(outputpath, 'w')
while True:
chunk = inputfp.read(262144)
if not chunk:
break
outputfp.write(chunk)
outputfp.close()
inputfp.close()
return outputpath
def __create_empty(path):
'''
This functions creates an empty Neubot database at @path,
jusing Neubot internals to do that.
'''
DATABASE.set_path(path)
connection = DATABASE.connection()
connection.commit()
connection.close()
def __migrate(connection):
'''
This function migrates the database at @connection to the
current database format. To do that, this function uses
directly the Neubot routines written for this purpose.
'''
migrate.migrate(connection)
def __sanitize(table):
'''
Write query making sure that the table name contains only
lowercase letters or underscores.
'''
stripped = re.sub(r'[^a-z_]', '', table)
if stripped != table:
raise RuntimeError("Invalid table name")
return table
def __lookup_can_publish(connection, table):
'''
This function lookups the number of measurement with the
can_publish permission in @table in the database referenced
by @connection.
'''
cursor = connection.cursor()
cursor.execute('''SELECT COUNT(timestamp) FROM %s
WHERE privacy_can_publish=1;''' % __sanitize(table))
count = next(cursor)[0]
if not count:
return 0
return count
def __lookup_count_uuids(connection, table):
'''
This function lookups the number of uuids in @table in the
database referenced by @connection.
'''
cursor = connection.cursor()
cursor.execute('SELECT COUNT(DISTINCT(uuid)) FROM %s;' % __sanitize(table))
count = next(cursor)[0]
if not count:
return 0
return count
def __lookup_count(connection, table):
'''
This function lookups the number of measurement in @table in the
database referenced by @connection.
'''
cursor = connection.cursor()
cursor.execute('SELECT COUNT(timestamp) FROM %s;' % __sanitize(table))
count = next(cursor)[0]
if not count:
return 0
return count
def __lookup_first(connection, table):
'''
This function lookups the first measurement in @table in the
database referenced by @connection.
'''
cursor = connection.cursor()
cursor.execute('SELECT MIN(timestamp) FROM %s;' % __sanitize(table))
minimum = next(cursor)[0]
if not minimum:
return 0
return minimum
def __lookup_last(connection, table):
'''
This function lookups the last measurement in @table in the
database referenced by @connection.
'''
cursor = connection.cursor()
cursor.execute('SELECT MAX(timestamp) FROM %s;' % __sanitize(table))
maximum = next(cursor)[0]
if not maximum:
return 0
return maximum
def __copyto_after(source, destination, table, limit):
'''
Copy from @source to @destination the content of @table
which has timestamp greater than @limit.
'''
query = None
cursor = source.cursor()
cursor.execute('SELECT * FROM %s WHERE timestamp > ?;'
% __sanitize(table), (limit,))
for result in cursor:
dictionary = dict(result)
# Otherwise we overwrite our data
del dictionary['id']
# Construct the query
if not query:
vector = ['INSERT INTO %s(' % __sanitize(table) ]
for name in dictionary.keys():
vector.append(name)
vector.append(', ')
vector[-1] = ') VALUES('
for name in dictionary.keys():
vector.append(':%s' % name)
vector.append(', ')
vector[-1] = ');'
query = "".join(vector)
# Insert
destination.execute(query, dictionary)
# Save
destination.commit()
def __anonimize(connection, table):
''' Anonimize @table of the database referenced by @connection '''
# Add columns
connection.execute(''' ALTER TABLE %s ADD COLUMN asname TEXT;'''
% __sanitize(table))
connection.execute(''' ALTER TABLE %s ADD COLUMN country_code TEXT;'''
% __sanitize(table))
connection.execute(''' ALTER TABLE %s ADD COLUMN city TEXT;'''
% __sanitize(table))
# Gather location and provider info
cursor = connection.cursor()
cursor.execute('SELECT * FROM %s' % __sanitize(table))
for row in cursor:
# Avoid violating MaxMind copyright
if int(row['privacy_can_publish']):
continue
# Ditch user address
connection.execute('''UPDATE %s SET internal_address="0.0.0.0",
real_address="0.0.0.0" WHERE id=?;''' %
__sanitize(table), (row['id'],))
# Provider information
asname = GEOLOC_ASN.org_by_addr(row['real_address'])
if asname:
asname = asname.decode('latin-1')
connection.execute(''' UPDATE %s SET asname=?
WHERE id=?''' % __sanitize(table), (asname, row['id']))
# Geo information
geodata = GEOLOC_CITY.record_by_addr(row['real_address'])
if geodata:
if geodata['country_code']:
country_code = geodata['country_code'].decode('latin-1')
connection.execute(''' UPDATE %s SET country_code=?
WHERE id=?''' % __sanitize(table), (country_code, row['id']))
if geodata['city']:
city = geodata['city'].decode('latin-1')
connection.execute(''' UPDATE %s SET city=?
WHERE id=?''' % __sanitize(table), (city, row['id']))
# Rebuild the database
connection.execute('VACUUM')
connection.commit()
def __format_date(thedate):
''' Make a timestamp much more readable '''
return time.ctime(int(thedate))
def __build_histogram(connection, table, histogram, modifiers):
'''
This function walks the @table of the database referenced by
@connection and collects statistics. Depending on the params
the result dictionary contains more or less aggregated data.
'''
cursor = connection.cursor()
cursor.execute('SELECT * FROM %s' % __sanitize(table))
for row in cursor:
stats = histogram
skip = False
for modifier in modifiers:
if modifier == 'per_instance':
instance = row['uuid']
if not instance:
skip = True
break
if not instance in stats:
stats[instance] = {}
stats = stats[instance]
elif modifier == 'per_provider':
provider = GEOLOC_ASN.org_by_addr(row['real_address'])
if not provider:
skip = True
break
# Avoid issues with provider name
provider = provider.decode('latin-1')
if not provider in stats:
stats[provider] = {}
stats = stats[provider]
elif modifier == 'per_country':
geodata = GEOLOC_CITY.record_by_addr(row['real_address'])
if not geodata or not geodata['country_code']:
skip = True
break
# Avoid issues with country code
country = geodata['country_code'].decode('latin-1')
if not country in stats:
stats[country] = {}
stats = stats[country]
elif modifier == 'per_city':
geodata = GEOLOC_CITY.record_by_addr(row['real_address'])
if not geodata or not geodata['city']:
skip = True
break
# Avoid issues with city name
city = geodata['city'].decode('latin-1')
if not city in stats:
stats[city] = {}
stats = stats[city]
elif modifier == 'per_hour':
#
# FIXME The problem with the hour calculator
# below is that it does not take into account
# the time zone. Since we're interested in
# Italy at the moment and we're in summer we
# optimize for CEST.
#
hour = (((row['timestamp']/3600) + 2) % 24)
if not hour in stats:
stats[hour] = {}
stats = stats[hour]
else:
raise RuntimeError('Invalid modifier: %s' % modifier)
if skip:
continue
#
# Save stats
#
if not stats:
stats.update({
'bittorrent':
{
'dload': [],
'upload': [],
'rtt': [],
},
'speedtest':
{
'dload': [],
'upload': [],
'rtt': [],
},
'first_test': 0,
'last_test': 0,
})
# First and last test info
if not stats['first_test']:
stats['first_test'] = row['timestamp']
if row['timestamp'] > stats['last_test']:
stats['last_test'] = row['timestamp']
stats[table]['dload'].append(row['download_speed'])
stats[table]['upload'].append(row['upload_speed'])
stats[table]['rtt'].append(row['connect_time'])
def main():
''' Dispatch control to various subcommands '''
syslog.openlog('neubot [tool]', syslog.LOG_PERROR, syslog.LOG_USER)
try:
options, arguments = getopt.getopt(sys.argv[1:], 'AMHiflNo:TX:')
except getopt.error:
sys.exit('Usage: tool.py -AMHiNT [-fl] [-o output] [-X modifier] input ...')
outfile = 'database.sqlite3'
modifiers = []
flag_histogram = False
flag_anonimize = False
flag_merge = False
flag_pretty = False
flag_info = False
flag_force = False
flag_number = False
flag_tests = False
for name, value in options:
if name == '-A':
flag_anonimize = True
elif name == '-M':
flag_merge = True
elif name == '-i':
flag_info = True
elif name == '-H':
flag_histogram = True
elif name == '-N':
flag_number = True
elif name == '-T':
flag_tests = True
elif name == '-X':
modifiers.append(value)
elif name == '-f':
flag_force = True
elif name == '-l':
flag_pretty = True
elif name == '-o':
outfile = value
sum_all = flag_anonimize + flag_merge + flag_info + flag_histogram + \
flag_number + flag_tests
if sum_all > 1:
sys.exit('Only one of -AMHiNT may be specified')
if sum_all == 0:
sys.exit('Usage: tool.py -AMHiNT [-fl] [-o output] [-X modifier] input ...')
#
# Collate takes a set of (possibly compressed) databases
# and appends to the output database only the set of results
# that is missing. So, it is safe to invoke it each time
# against the same set of files.
# Note that merge will skip old databases unless the
# force parameter is True.
#
if flag_merge:
if not os.path.exists(outfile):
__create_empty(outfile)
elif not os.path.isfile(outfile):
raise RuntimeError('Not a file')
destination = __connect(outfile)
__migrate(destination)
for argument in arguments:
# Decompress if needed
if argument.endswith('.bz2'):
syslog.syslog(syslog.LOG_INFO, 'decompress %s' % argument)
argument = __decompress(argument)
# Query configuration
source = __connect(argument)
info = __info(source)
version = decimal.Decimal(info['version'])
# Skip old databases
if version <= decimal.Decimal('2.0') and not flag_force:
syslog.syslog(syslog.LOG_WARNING, 'skipping old %s' % argument)
continue
# Migrate
syslog.syslog(syslog.LOG_INFO, 'migrate %s' % argument)
__migrate(source)
# Save
for table in ('speedtest', 'bittorrent'):
syslog.syslog(syslog.LOG_INFO, 'merging table %s' % table)
limit = __lookup_last(destination, table)
__copyto_after(source, destination, table, limit)
#
# Print information on the database so that one can get
# an idea of the information contained.
#
elif flag_info:
for argument in arguments:
target = __connect(argument)
__migrate(target)
dictionary = __info(target)
dictionary['filename'] = argument
for table in ('speedtest', 'bittorrent'):
dictionary[table] = {}
dictionary[table]['count_uuids'] = __lookup_count_uuids(target,
table)
dictionary[table]['count'] = __lookup_count(target, table)
dictionary[table]['can_publish'] = __lookup_can_publish(target,
table)
first = __lookup_first(target, table)
last = __lookup_last(target, table)
if flag_pretty:
first = __format_date(first)
last = __format_date(last)
dictionary[table]['first'] = first
dictionary[table]['last'] = last
indent = None
if flag_pretty:
indent = 4
json.dump(dictionary, sys.stdout, indent=indent)
if flag_pretty:
sys.stdout.write("\n")
#
# Zap all internet addresses with no can_publish permission
# attached regardless of the other settings.
#
elif flag_anonimize:
for argument in arguments:
target = __connect(argument)
__migrate(target)
__anonimize(target, 'speedtest')
__anonimize(target, 'bittorrent')
#
# Walk the database and collect statistics where the
# aggregation level depends on command line options
#
elif flag_histogram:
histogram = {}
for argument in arguments:
target = __connect(argument)
__migrate(target)
for table in ('speedtest', 'bittorrent'):
__build_histogram(target, table, histogram, modifiers)
sort_keys, indent = False, None
if flag_pretty:
sort_keys, indent = True, 4
json.dump(histogram, sys.stdout, indent=indent, sort_keys=sort_keys)
if flag_pretty:
sys.stdout.write("\n")
#
# Compute the cumulated number of active agents at a
# given time period.
#
elif flag_number:
number_of_agents = collections.defaultdict(set)
for argument in arguments:
target = __connect(argument)
__migrate(target)
for table in ('speedtest', 'bittorrent'):
cursor = target.cursor()
cursor.execute('SELECT * FROM %s;' % __sanitize(table))
for row in cursor:
when = int(dates.epoch2num(row['timestamp']))
number_of_agents[when].add(row['uuid'])
cumulated, xdata, ydata = 0, [], []
for when in sorted(number_of_agents.keys()):
xdata.append(when)
ydata.append(len(number_of_agents[when]))
pyplot.plot_date(xdata, ydata)
pyplot.show()
# Tries to count the number of tests per day.
elif flag_tests:
xdata, ydata = [], []
helper = collections.defaultdict(int)
for argument in arguments:
target = __connect(argument)
__migrate(target)
for table in ('speedtest', 'bittorrent'):
cursor = target.cursor()
cursor.execute('SELECT * FROM %s;' % __sanitize(table))
for row in cursor:
when = int(dates.epoch2num(row['timestamp']))
helper[when] += 1
for when in sorted(helper.keys()):
xdata.append(when)
ydata.append(helper[when])
pyplot.plot_date(xdata, ydata)
pyplot.show()
if __name__ == '__main__':
main()