-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbgpstream_stability.py
More file actions
187 lines (134 loc) · 5.29 KB
/
bgpstream_stability.py
File metadata and controls
187 lines (134 loc) · 5.29 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
#!/usr/bin/env python
#
# Copyright (C) 2015
# Authors: Nathan Owens & Andrew Mulhern
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
#
import sys
import json
import copy
import math
import urllib
import multiprocessing
from _pybgpstream import BGPStream, BGPRecord, BGPElem
from collections import defaultdict
from datetime import datetime
def deal_with_time_bucket_junk(prefix, timestamp):
if prefix not in raw_bgp_stream_data:
newBuckets = copy.deepcopy(buckets)
raw_bgp_stream_data[prefix] = newBuckets
duration = timestamp - stream_start
bucket = int(duration / 300)
try:
raw_bgp_stream_data[prefix][bucket]["count"] += 1
except:
pass
def create_time_buckets(start, end):
time_step = 300 # 5 multiprocessing
buckets = []
for x in xrange(start, end, time_step):
new_end = x + 300
window = {"start": x, "end": new_end, "count": 0}
buckets.append(window)
return buckets
def get_ripe_probes(prefix_list):
def get_probe_list(ip_proto, prefix_data, return_dict):
prefix = prefix_data[0]
count = prefix_data[1]
bucket_data = prefix_data[2]
url = "https://atlas.ripe.net/api/v1/probe/?format=json&prefix_%s=%s" % (ip_proto, prefix)
probe_data = json.loads(urllib.urlopen(url).read())
probe_count = probe_data["meta"]["total_count"]
probe_ids = []
if probe_count > 0:
for probe in probe_data["objects"]:
probe_id = probe["id"]
probe_ids.append(probe_id)
if len(probe_ids) > 0:
return_dict[prefix] = {"count": count, "bucket_data": bucket_data, "probe_count": probe_count, "probe_ids": probe_ids}
return
jobs = []
manager = multiprocessing.Manager()
return_dict = manager.dict()
for prefix_data in prefix_list:
prefix = prefix_data[0]
if "." in prefix:
job = multiprocessing.Process(target=get_probe_list, args=("v4", prefix_data, return_dict))
elif ":" in prefix:
job = multiprocessing.Process(target=get_probe_list, args=("v6", prefix_data, return_dict))
jobs.append(job)
job.start()
for job in jobs:
job.join()
return dict(return_dict)
if __name__ == "__main__":
try:
stream_start = int(sys.argv[1])
stream_end = int(sys.argv[2])
out_file_name = sys.argv[3]
except:
print "Usage: %s [start time] [end time] [output file name]" %(sys.argv[0])
exit()
#stream_start = 1454284800
#stream_end = 1454288400
buckets = create_time_buckets(stream_start, stream_end)
prefixList = []
raw_bgp_stream_data = {}
stream = BGPStream()
rec = BGPRecord()
stream.add_filter('collector', 'rrc06')
stream.add_filter('record-type', 'updates')
stream.add_interval_filter(stream_start, stream_end)
stream.start()
while(stream.get_next_record(rec)):
elem = rec.get_next_elem()
while(elem):
prefix = elem.fields.get("prefix", "")
time_stamp = rec.time # unix epoc timestamp 1427846670
if prefix != "":
deal_with_time_bucket_junk(prefix, time_stamp)
elem = rec.get_next_elem()
for prefix in list(raw_bgp_stream_data):
for bucket in list(raw_bgp_stream_data[prefix]):
if bucket["count"] < 3:
raw_bgp_stream_data[prefix].remove(bucket)
for prefix in raw_bgp_stream_data:
index = 0
max_index = 0
max_val = 0
last_val = 0
for bucket in raw_bgp_stream_data[prefix]:
curr = bucket["count"]
if curr > last_val:
max_val = curr
index += 1
last_val = curr
if raw_bgp_stream_data[prefix]:
prefixList.append((prefix, max_val, raw_bgp_stream_data[prefix][max_index]))
prefixListWithProbes = get_ripe_probes(prefixList)
import get_probe_data
results = []
for prefix, values in prefixListWithProbes.iteritems():
start_time = values["bucket_data"]["start"]
end_time = values["bucket_data"]["end"]
count = values["count"]
probe_list = values["probe_ids"]
for probe in probe_list:
packet_loss = get_probe_data.get_packet_loss(probe, start_time, end_time)
results.append({"prefix":prefix, "count":count, "start_time":start_time, "end_time":end_time, "probe":probe, "packet_loss":packet_loss})
sorted_results = list(sorted(results, reverse=True, key = lambda x: (x["count"], x["packet_loss"])))
out_file = open(out_file_name, "w")
out_file.write(json.dumps(sorted_results, indent=3))
out_file.close()