-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_collector.py
151 lines (118 loc) · 5.48 KB
/
stats_collector.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
#!/usr/bin/python
import json
import requests
import time
import threading
#dictionary of switches with key = dpid
global switch_dict
switch_dict = {}
#hello
#REST API call to get switch-details
req_get_switch_details = requests.get('http://127.0.0.1:8080/wm/core/controller/switches/json')
#parse json
switch_details_json = json.loads(req_get_switch_details.text)
#store switch details for each switch
for switch in switch_details_json:
switch_dict[switch['switchDPID']] = {'ports' : {}}
#get port details
port_details_req = requests.get('http://127.0.0.1:8080/wm/core/switch/'+switch['switchDPID']+'/port/json')
port_details_json = json.loads(port_details_req.text)
port_dict = {}
#print (port_details_json['port_reply'][0]['port'][0])
#store details for each port
for port in port_details_json['port_reply'][0]['port']:
port_dict[port['port_number']] = {'bandwidth_util' : 0, 'time' : int(time.time()), 'tx' : int(port['transmit_bytes']), 'rx' : int(port['receive_bytes'])}
switch_dict[switch['switchDPID']]['ports'].update(port_dict)
#get mac and ip for every port for each switch
r = requests.get('http://127.0.0.1:8080/wm/device/')
data = json.loads(r.text)
devices = data['devices']
for entity in devices:
#print(entity)
dpid = entity['attachmentPoint'][0]['switch']
port = entity['attachmentPoint'][0]['port']
ip = entity['ipv4'][0]
mac = entity['mac'][0]
print(dpid, port, ip, mac)
#store
d = {'mac':mac, 'ip':ip}
switch_dict[dpid]['ports'][port].update(d)
print(switch_dict)
#input params: vertex id of both switches
def create_flow(a, b, port_matrix, dpid_arr, isQoS):
#actual stuff comented out to test
dpid_a = '00:00:92:15:ed:4f:ff:49' #dpid_arr[a]
dpid_b = '00:00:92:15:ed:4f:ff:49' #dpid_arr[b]
egress_port = '2' #port_matrix[a][b] #egress on switch a
ingress_port = '1' #port_matrix[b][a] #ingress on switch b
dest_mac = switch_dict[dpid_b]['ports'][ingress_port]['mac']
forward_flow = '{"switch":"'+dpid_a + '","name":"' + "test" + '", "eth_dst":"' + dest_mac +'", "actions":"output=' + egress_port +'"}'
return forward_flow
flow = create_flow(1,3,[], [], False)
print flow
#computes bandwidth utilization and updates the value of 'bandwodth_utilization' in the switch_dict for every port of every switch
def computeStats(switch_dict):
dest_mac = switch_dict
curr_time = int(time.time())
for switch in switch_dict:
#store_port stats
print "-----------"
print "switch_dpid:", switch
port_stats = {}
#get current_port_stats
port_details_req = requests.get('http://127.0.0.1:8080/wm/core/switch/'+switch+'/port/json')
port_details_json = json.loads(port_details_req.text)
for port in port_details_json['port_reply'][0]['port']:
port_stats[port['port_number']] = {'tx' : int(port['transmit_bytes']), 'rx' : int(port['receive_bytes'])}
ports = switch_dict[switch]['ports']
for port in ports:
prev_tx = ports[port]['tx']
prev_rx = ports[port]['rx']
#calculate bw
bw = float(port_stats[port]['tx'] + port_stats[port]['rx'] - prev_tx - prev_rx) / (curr_time - ports[port]['time'])
#print(int(port_stats[port]['tx']))
#print(int(port_stats[port]['rx']))
#print(curr_time)
bw = float(bw/1000/125)
bw = int((bw * 100) + 0.5) / 100.0
ports[port]['bandwidth_util'] = bw
ports[port]['tx'] = port_stats[port]['tx']
ports[port]['rx'] = port_stats[port]['rx']
ports[port]['time'] = curr_time
#print "port id\t", port, "\tbandwidth_util\t", bw, "\tMbits/sec"
print "port id\t", port, "\tbandwidth_util\t", ports[port]['bandwidth_util'], "\tMbits/sec"
print "-----------"
class StatsManager(threading.Thread):
def __init__(self, delay):
threading.Thread.__init__(self)
self.delay = delay
def run(self):
count = 0
while True:
time.sleep(self.delay)
print(count)
computeStats(switch_dict)
count += 1
class FlowManager():
flow_dict = {}
def push_flow(self, flow):
#push a flow
r = requests.post('http://127.0.0.1:8080/wm/staticentrypusher/json', data = flow)
print r
def update_flow(self, list):
for flow in list:
push_flow(flow)
def clear_flow(self, switch):
#clear all flows on the switch
r = requests.get('http://127.0.0.1:8080/wm/staticentrypusher/clear/' + switch + '/json')
#stat_collector_thread = StatsManager(1)
#stat_collector_thread.daemon = True
#stat_collector_thread.start()
#while True:
# time.sleep(1)
flow_manager = FlowManager()
#flow = '{"switch":"00:00:92:15:ed:4f:ff:49","name":"flow-mod-1", "active":"true", "eth_dst":"fa:16:3e:00:4a:ca", "actions":"output=2"}'
switch = '00:00:92:15:ed:4f:ff:49'
#flow_manager.clear_flow(switch)
flow_manager.push_flow(flow)
#flow_manager.clear_flow(switch)