Code for demo-ing AffinityLink statistics
[affinity.git] / scripts / stats.py
1 import httplib2
2 import json
3  
4 h = httplib2.Http(".cache")
5 h.add_credentials('admin', 'admin')
6
7 resp, content = h.request('http://localhost:8080/controller/nb/v2/statistics/default/flowstats', "GET")
8 allFlowStats = json.loads(content)
9 flowStats = allFlowStats['flowStatistics']
10 print flowStats
11
12 # These JSON dumps were handy when trying to parse the responses 
13 #print json.dumps(flowStats[0]['flowStat'][1], indent = 2)
14 #print json.dumps(flowStats[4], indent = 2)
15 for fs in flowStats:
16     print "\nSwitch ID : " + fs['node']['@id']
17     print '{0:8} {1:8} {2:5} {3:15}'.format('Count', 'Action', 'Port', 'DestIP')
18     for aFlow in fs['flowStat']:
19         count = aFlow['packetCount']
20 actions = aFlow['flow']['actions'] 
21 actionType = ''
22 actionPort = ''
23 #print actions
24 if(type(actions) == type(list())):
25     actionType = actions[1]['@type']
26     actionPort = actions[1]['port']['@id']
27 else:
28     actionType = actions['@type']
29     actionPort = actions['port']['@id']
30 dst = aFlow['flow']['match']['matchField'][0]['value']
31 print '{0:8} {1:8} {2:5} {3:15}'.format(count, actionType, actionPort, dst)