-
Notifications
You must be signed in to change notification settings - Fork 0
/
atlas_outage_plot.py
130 lines (115 loc) · 4.66 KB
/
atlas_outage_plot.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
#!/root/anaconda3/bin/python
import json
import requests
import time
import sys
from ripe.atlas.cousteau import ProbeRequest
from collections import defaultdict
from bisect import bisect_right, bisect_left
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
def get_probes(cc):
filters = {}
filters['country_code'] = cc
probes = ProbeRequest(**filters)
probe_id = {p["id"] for p in probes}
return probe_id
def get_events(cc, start_timestamp, end_timestamp):
disco_event=defaultdict(list)
conn_event=defaultdict(list)
probe_id = get_probes(cc)
#probe_id = [34212]
api_url = "https://atlas.ripe.net/api/v2/measurements/7000/results?start={}&end={}&format=json"\
.format(start_timestamp,end_timestamp)
response = requests.get(api_url)
if response.status_code == 200:
event_json = json.loads(response.content.decode('utf-8'))
for item in event_json:
if (item['prb_id'] in probe_id):
if(item['event'] == 'connect'):
conn_event[item['prb_id']].append(item['timestamp'])
else:
disco_event[item['prb_id']].append(item['timestamp'])
return(conn_event,disco_event)
else:
return None
def analyze_events(cc, start_time, end_time):
disco_duration=defaultdict(list)
conn_event, disco_event = get_events(cc,start_time,end_time)
for probe_id in conn_event:
for conn_time in conn_event[probe_id]:
# Locate the the lowest nearest disconnection
nearest_disco = bisect_right(disco_event[probe_id], conn_time)
if nearest_disco == 0:
nearest_disco_time = int(start_time)
disco_event[probe_id].insert(0,int(start_time))
else:
nearest_disco_time = int(disco_event[probe_id][nearest_disco-1])
disco_duration[probe_id].append(int(conn_time)-nearest_disco_time)
for disco_time in disco_event[probe_id]:
# Locate the the highest nearest connection
nearest_conn = bisect_left(conn_event[probe_id], disco_time)
if nearest_conn == len(conn_event[probe_id]):
disco_duration[probe_id].append(int(end_time)-disco_time)
return(conn_event, disco_event, disco_duration)
def plot_disco_duration(disco_duration,cc):
merge_disco_duration = []
for d in disco_duration.values():
merge_disco_duration = merge_disco_duration + d
fig, ax = plt.subplots()
ax.violinplot(np.log10(merge_disco_duration), showmedians=True)
#ax.set_yscale("log", nonposy='clip')
ax.grid(True)
ax.set_xticklabels([])
ax.set_xticklabels([])
plt.ylabel('log10(Disconnection duration in seconds)')
fig.savefig('disco_duration_dist_{}.png'.format(cc))
plt.close(fig)
def plot_disco_time(disco_event):
merge_disco_time=[]
merge_disco_hour=[]
for d in disco_event.values():
merge_disco_time = merge_disco_time + d
for e in d:
merge_disco_hour.append(datetime.fromtimestamp(e).hour)
fig, ax = plt.subplots()
merge_disco_hour = np.array(merge_disco_hour)
ax.hist(merge_disco_hour, bins=np.arange(merge_disco_hour.min(), merge_disco_hour.max()+1))
#ax.set_yscale("log", nonposy='clip')
ax.grid(True)
plt.ylabel('Disconnection time')
fig.savefig('disco_time_dist_{}.png'.format(cc))
plt.close(fig)
def plot_disco_occurence(disco_duration):
disco_num = [len(d) for d in disco_duration.values()]
disco_avg = [sum(d)/len(d) for d in disco_duration.values()]
fig, ax = plt.subplots()
ax.set_xscale("log")
ax.grid(True)
ax.plot(disco_avg,disco_num,'x')
plt.xlabel('Disconnection duration in seconds')
plt.ylabel('Number of disconnections')
fig.savefig('disco_occurence_{}.png'.format(cc))
plt.close(fig)
if __name__ == "__main__":
cc = str(sys.argv[1])
#start_time = '1520630104'
start_time = '1523905107'
end_time = time.time()
conn_event, disco_event, disco_duration = analyze_events(cc,start_time,end_time)
conn_event_json = json.dumps(conn_event)
disco_event_json = json.dumps(disco_event)
disco_duration_json = json.dumps(disco_duration)
plot_disco_duration(disco_duration,cc)
plot_disco_time(disco_event)
plot_disco_occurence(disco_duration)
f = open("conn_event_{}_{}_{}.json".format(cc,start_time,end_time),"w")
f.write(conn_event_json)
f.close()
f = open("disco_event_{}_{}_{}.json".format(cc,start_time,end_time),"w")
f.write(disco_event_json)
f.close()
f = open("disco_duration_{}_{}_{}.json".format(cc,start_time,end_time),"w")
f.write(disco_duration_json)
f.close()