-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
executable file
·207 lines (158 loc) · 5.33 KB
/
process.py
File metadata and controls
executable file
·207 lines (158 loc) · 5.33 KB
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
#!/usr/bin/env python
import csv
from datetime import date
import os
import psycopg2
import envoy
import maxminddb
BIN_PATH = 'ark-tools/simple_warts.rb'
MAXMIND = maxminddb.open_database('GeoLite2-City.mmdb')
def main():
"""
Parse everything! Write results to a single output file.
"""
db = psycopg2.connect(dbname='ark', user='ark')
cursor = db.cursor()
cursor.execute('DROP TABLE IF EXISTS monitors CASCADE;')
cursor.execute('''CREATE TABLE monitors (
name char(8) primary key,
ip char(15),
location varchar,
lat real,
lng real,
asn char(12),
org_class varchar,
org_name varchar,
geom GEOMETRY(Point, 4326)
);
''')
cursor.execute('''CREATE INDEX monitors_gix
ON monitors
USING GIST (geom);
''')
# REFERENCES monitors (name)
cursor.execute('DROP TABLE IF EXISTS traces;')
cursor.execute('''CREATE TABLE traces (
id serial primary key,
probe_date date,
monitor_name char(8),
monitor_ip char(15),
monitor_as varchar,
dest_ip char(15),
dest_as varchar,
country varchar,
subdivision varchar,
city varchar,
lat real,
lng real,
rtt real,
ip_hops integer,
as_hops integer,
trace varchar,
geom GEOMETRY(Point, 4326)
);
''')
cursor.execute('''CREATE INDEX traces_gix
ON traces
USING GIST (geom);
''')
load_monitors(db)
for day in range(1, 7):
d = {
'year': 2014,
'month': 3,
'day': day
}
print(d)
parse_date(d, db)
db.close()
def load_monitors(db):
"""
Parse data on Ark monitors.
"""
cursor = db.cursor()
with open('ark-monitors-20160322.txt') as f:
reader = csv.reader(f, delimiter='|')
next(reader)
for row in reader:
data = ', '.join(['\'%s\'' % r for r in row])
data += ', ST_GeomFromText(\'POINT(%s %s)\', 4326)' % (row[4], row[3])
cursor.execute('''
INSERT INTO monitors
VALUES (%s)''' % data)
db.commit()
def parse_date(d, db):
"""
Parse all Ark files for a single day.
"""
routing_path = 'data.caida.org/datasets/routing/routeviews-prefix2as/%(year)d/%(month)02d/routeviews-rv2-%(year)d%(month)02d%(day)02d-1200.pfx2as.gz' % d
for team in range(1, 4):
print('team-%i' % team)
d['team'] = team
ark_root = 'data.caida.org/datasets/topology/ark/ipv4/probe-data/team-%(team)i/2014/cycle-%(year)d%(month)02d%(day)02d/' % d
if not os.path.exists(ark_root):
continue
for filename in os.listdir(ark_root):
print(filename)
ark_path = os.path.join(ark_root, filename)
monitor_name = filename.split('.')[-3].strip()
cmd = '%(bin)s %(routes)s %(warts)s' % {
'bin': BIN_PATH,
'routes': routing_path,
'warts': ark_path
}
r = envoy.run(cmd)
parse_ark(monitor_name, date(d['year'], d['month'], d['day']), r.std_out, db)
def parse_ark(monitor_name, probe_date, ark_text, db):
"""
Parse Ark text format and stream results into a CSV writer.
"""
cursor = db.cursor()
monitors = []
for line in ark_text.splitlines():
fields = line.strip().split('\t')
row = {
'monitor_name': monitor_name,
'probe_date': probe_date,
'monitor_ip': fields[0],
'monitor_as': fields[1],
'dest_ip': fields[2],
'dest_as': fields[3],
'country': None,
'subdivision': None,
'city': None,
'lat': None,
'lng': None,
'rtt': fields[4],
'ip_hops': 0,
'as_hops': 0,
'trace': ','.join(fields[5:]),
'geom': None
}
last_asn = None
for field in fields[5:]:
ip, asn = field.split(':')
row['ip_hops'] += 1
if asn in ['q', 'r', last_asn]:
continue
row['as_hops'] += 1
last_asn = asn
loc = MAXMIND.get(row['dest_ip'])
if loc:
if 'country' in loc:
row['country'] = loc['country']['names']['en']
if 'subdivisions' in loc:
row['subdivision'] = loc['subdivisions'][0]['names']['en']
if 'city' in loc:
row['city'] = loc['city']['names']['en']
if 'location' in loc:
row['lat'] = loc['location']['latitude']
row['lng'] = loc['location']['longitude']
row['geom'] = 'POINT(%s %s)' % (row['lng'], row['lat']) if row['lng'] and row['lat'] else None
cursor.execute('''
INSERT INTO traces (probe_date, monitor_name, monitor_ip, monitor_as, dest_ip, dest_as, country, subdivision, city, lat, lng, rtt, ip_hops, as_hops, trace, geom)
VALUES (%(probe_date)s, %(monitor_name)s, %(monitor_ip)s, %(monitor_as)s, %(dest_ip)s, %(dest_as)s, %(country)s, %(subdivision)s, %(city)s, %(lat)s, %(lng)s, %(rtt)s, %(ip_hops)s, %(as_hops)s, %(trace)s, ST_GeomFromText(%(geom)s, 4326));'''
, row)
db.commit()
if __name__ == '__main__':
main()