-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent.py
354 lines (306 loc) · 10.4 KB
/
agent.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
#!/usr/bin/env python
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import cgi, subprocess, requests, thread, argparse, time, re, threading
import iptables, iperf
def send_probe():
snd_cmd = ["/home/haoxian/pathload_1.3.2/pathload_snd"]
subprocess.call(snd_cmd,shell=True);
def inform_dst_host(src_addr,dst_addr):
port_number = "8000"
url = "http://"+dst_addr+":"+port_number
data = {"type":"rcv_req","src_addr":src_addr,'dst_addr':dst_addr}
data['update_addr'] = 'sonic3.cs.cornell.edu'
data['update_port'] = 8080
response = requests.post(url,data=data)
return response.text
def receive_probe(post_msg):
from datetime import datetime
rcv_cmd = ["/home/haoxian/pathload_1.3.2/pathload_rcv -s "+post_msg["src_addr"]]
output=subprocess.check_output(rcv_cmd,shell=True)
[bw_min, bw_max] = parse_output(output)
result={}
result['type'] = "bw_update"
result['time'] = datetime.now().time()
result['src_addr'] = post_msg['src_addr']
result['dst_addr'] = post_msg['dst_addr']
result['minimum bandwidth']=bw_min
result['maximum bandwidth']=bw_max
url = "http://"+post_msg['update_addr']+":"+post_msg['update_port']
requests.post(url,data=result)
return result
def parse_output(output):
result_line = re.findall("range : [0-9]+\.[0-9]+ - [0-9]+\.[0-9]+", output);
result_str = result_line.pop()
[bw_min, bw_max] = re.findall("[0-9]+\.[0-9]+", result_str)
return [bw_min, bw_max]
def do_bw_estimate(post_msg):
src_addr = post_msg["src_addr"]
dst_addr = post_msg["dst_addr"]
if "cycle" in post_msg:
cycle = post_msg["cycle"]
else:
cycle = 20 # default update circle is 20 seconds
if "duration" in post_msg:
duration = int(post_msg["duration"])
else:
duration = 60 # default update duration is 60 seconds
if "method" in post_msg:
method = post_msg["method"]
else:
method = "path_load" # default method to estimate bandwidth is path_load
if(method == "path_load"):
for i in range(1,duration/cycle):
thread.start_new_thread(send_probe,())
time.sleep(1)
inform_dst_host(src_addr,dst_addr)
if(method == "sonic"):
sonic.configure(post_msg)
return
def send_to_monitor(args):
port = 9090
url = 'http://127.0.0.1:' + str(port)
response = requests.post(url, data=args)
return response
def recursive_query(args):
[pkts_num,bytes_num] = iptables.read_counter(args)
response = {}
for a in leaf_agent:
url = 'http://' + a + ':8000'
r = requests.post(url, data=args)
if r.status_code == 200:
# parse output
result = re.findall('[0-9]+',r.text)
if len(result) > 1:
[leaf_pkt, leaf_byte] = [int(result[0]),int(result[1])]
pkts_num = pkts_num + leaf_pkt
bytes_num = bytes_num + leaf_byte
else:
response['status_code'] = r.status_code
response['data'] = r.text
response['status_code'] = 200
response['data'] = [pkts_num,bytes_num]
return response
def recursive_query_sketch(args):
r = send_to_monitor(args)
print args
response = {}
if r.status_code == 200:
print r.text
count = int(r.text)
else:
response['status_code'] = r.status_code
response['data'] = r.text
return response
for addr in leaf_agent:
url = 'http://' + addr + ':8000'
r = requests.post(url,data=args)
if r.status_code == 200:
leaf_count = int(r.text)
count = count + leaf_count
else:
response['status_code'] = r.status_code
response['data'] = r.text
return response
response['status_code'] = 200
response['data'] = count
return response
def query_leaf_haevy_hitter(addr, args):
from collections import Counter
import ast
global g_h_hitter, response_counter
url = 'http://' + addr + ':8000'
r = requests.post(url, data=args)
print 'querying ' + addr
try:
r = send_to_monitor(args)
if r.status_code == 200:
leaf_hitter = ast.literal_eval(r.text)
mutex.acquire()
g_h_hitter = Counter(g_h_hitter) + Counter(leaf_hitter)
response_counter = response_counter + 1
mutex.release()
except Exception as x:
print x
print 'finished query ' + addr
def recursive_query_heavy_hitter(args):
from collections import Counter
import ast
global g_h_hitter, response_counter, leaf_agent
# reset the global variable before use
g_h_hitter = 0
response_counter = 0
response = {'status_code':200,'data':{}}
r = send_to_monitor(args)
if r.status_code == 200:
g_h_hitter = ast.literal_eval(r.text)
else:
response['status_code'] = r.status_code
response['data'] = r.text
return response
threads = []
for addr in leaf_agent:
print 'try to query ' + addr
t = threading.Thread(target = query_leaf_haevy_hitter,args = (addr,args))
t.setName(addr)
t.daemon = True
t.start()
threads.append(t)
for t in threads:
t.join(0.3)
if t.isAlive():
print 'warning: timeout happened in querying' + t.getName()
print 'query from ' + str(response_counter) + 'agents'
print dict(g_h_hitter)
response['data'] = dict(g_h_hitter)
return response
def send_to_leaf(addr,args):
print 'configuring ' + str(addr)
url = 'http://' + str(addr) + ':8000'
try:
r = requests.post(url,data=args)
if r.status_code != 200:
print "error: " + r.text
except Exception as x:
print x
def recursive_config(args):
global leaf_agent
# reset the global variable before use
response = {'status_code':200,'data':{}}
try:
r = send_to_monitor(args)
if r.status_code == 200:
response['data'] = 'configuration done.'
except Exception as x:
response['data'] = 'configuration failed.'
print x
threads = []
for addr in leaf_agent:
print 'try to config' + addr
t = threading.Thread(target = send_to_leaf,args = (addr,args))
t.setName(addr)
t.daemon = True
t.start()
threads.append(t)
for t in threads:
t.join(0.3)
if t.isAlive():
print 'warning: timeout happened in configuring' + t.getName()
return response
class MyHandler(BaseHTTPRequestHandler):
def do_POST(self):
global leaf_agent
self.query_string = self.rfile.read(int(self.headers['Content-Length']))
self.args = dict(cgi.parse_qsl(self.query_string))
status_code = 400
response = "error: message not parsed"
if 'type' in self.args:
msg_type = self.args['type']
else:
self.send_response(400)
self.end_headers()
self.wfile.write("No message type indicated.")
return
if msg_type == 'est_req':
thread.start_new_thread(do_bw_estimate,(self.args,))
response = "Started Bandwidth Estimation"
if msg_type == 'rcv_req':
response = receive_probe(self.args)
if msg_type == 'query bw':
r = iperf.query_bw(self.args)
status_code = r['status_code']
response = r['data']
if msg_type == 'query jitter':
r = iperf.query_jitter(self.args)
status_code = r['status_code']
response = r['data']
if msg_type == 'config iperf server':
if 'server_mode' in self.args:
mode = self.args['server_mode']
if iperf.start_server(mode) == 0:
status_code = 200
response = "iperf server started"
else:
status_code = 500
response = "error: server failed"
else:
status_code = 400
response = "bad requst: missing server mode"
# handle traffic monitoring messages
if msg_type == 'config counter':
status_code = 200
# send out configuration message to leaf agents
for i,addr in enumerate(leaf_agent):
url = 'http://' + str(addr) + ':8000'
r = requests.post(url,data=self.args)
if r.status_code != 200:
status_code = r.status_code
response = iptables.install_rules(self.args)
if msg_type == 'query counter':
# recursive querying leaf agents
r = recursive_query(self.args)
status_code = r['status_code']
response = r['data']
if msg_type == 'config sketch':
# send out configuration message to leaf agents
for i,addr in enumerate(leaf_agent):
thread.start_new_thread(send_to_leaf,(addr,self.args))
r = send_to_monitor(self.args)
status_code = r.status_code
response = r.text
if msg_type == 'config sketch counter':
# send out configuration message to leaf agents
for i,addr in enumerate(leaf_agent):
# to config global counters on distributed end-hosts, may need to look up sketch id on each end-host, although in experiments they are the same
thread.start_new_thread(send_to_leaf,(addr,self.args))
r = send_to_monitor(self.args)
status_code = r.status_code
response = r.text
if msg_type == 'query sketch':
r= recursive_query_sketch(self.args)
status_code = r['status_code']
response = r['data']
if (msg_type == 'query heavy hitters') or (msg_type == 'query real time counter'):
r = recursive_query_heavy_hitter(self.args)
status_code = r['status_code']
response = r['data']
if msg_type == 'add leaf agent':
if 'agent_addr' in self.args:
leaf_agent.append(self.args['agent_addr'])
status_code = 200
response = 'leaf agent added'
if msg_type == 'query leaf agents':
status_code = 200
response = leaf_agent
if status_code != 200:
print 'error: ' + str(response)
self.send_response(status_code)
self.end_headers()
self.wfile.write(str(response))
return
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write("This is SoNIC server")
def run(addr,port):
print('http server is starting...')
#ip and port of servr
#by default http server port is 8000
server_address = (addr,port)
print server_address
httpd = HTTPServer(server_address, MyHandler)
print('server is listenning on host '+addr+':'+str(port))
httpd.serve_forever()
# gloabal vriable: for storing the ip of leaf notes
leaf_agent = []
# for multithread syncronization
response_counter = 0
g_h_hitter = {}
mutex = threading.Lock()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='This is SoNIC server')
parser.add_argument('-p','--port',help='server listen port',default=8000)
parser.add_argument('-a','--address',help='server address', required=True)
parser.add_argument('-l','--leaf',help='leaf host id',default=[])
args = parser.parse_args()
leaf_agent = leaf_agent + re.findall('[0-9]+.[0-9]+.[0-9]+.[0-9]+',str(args.leaf))
run(args.address,int(args.port))