1 #!/usr/local/bin/python
8 from stats import Stats
9 from subnet import SubnetControl
10 from affinity_control import AffinityControl
12 # 1. Start the controller
13 # 2. On the local machine (e.g., your laptop), start this script.
14 # > python analytics.py
15 # 3. On the mininet VM, run:
16 # > sudo mn --controller=remote,ip=192.168.56.1 --topo tree,2
18 # 4. Give commands to analytics.py. For instance:
19 # > host bytes 10.0.0.1 10.0.0.3
20 # (There is a usage prompt that prints at the beginning of analytics.py)
21 # 5. Type 'quit' to exit analytics.py
23 def run_interactive_mode():
25 print "Usage: [host | link] [bytes | rate] [src dst | link-name]"
29 request = raw_input("> ")
31 request = request.split()
32 request_type = request[0]
34 if (request_type == "quit"):
37 if (request_type == "host"):
39 src, dst = request[2:4]
40 host_stat = Stats(Stats.TYPE_HOST, src=src, dst=dst)
41 if (action == "bytes"):
42 print("%d bytes between %s and %s" % (host_stat.get_bytes(), src, dst))
43 elif (action == "rate"):
44 print("%f bit/s between %s and %s" % (host_stat.get_bit_rate(), src, dst))
48 elif (request_type == "link"):
51 link_stat = Stats(Stats.TYPE_AL, al=link)
52 if (action == "bytes"):
53 print("%d bytes on %s" % (link_stat.get_bytes(), link))
54 elif (action == "rate"):
55 print("%f bit/s on %s" % (link_stat.get_bit_rate(), link))
59 elif (request_type == "prefix"):
61 h = httplib2.Http(".cache")
62 h.add_credentials("admin", "admin")
63 url_prefix = "http://localhost:8080/affinity/nb/v2/analytics/default/prefixstats/"
64 resp, content = h.request(url_prefix + prefix, "GET")
65 if (resp.status == 200):
66 data = json.loads(content)
67 print data['byteCount'], "bytes"
70 except Exception as e:
76 # Default subnet is required for the host tracker to work.
77 subnet_control = SubnetControl()
78 subnet_control.add_subnet("defaultSubnet", "10.0.0.254/8")
80 # Set up an affinity link
81 affinity_control = AffinityControl()
82 affinity_control.add_affinity_group("testAG1", ips=["10.0.0.1", "10.0.0.2"])
83 affinity_control.add_affinity_group("testAG2", ips=["10.0.0.3", "10.0.0.4"])
84 affinity_control.add_affinity_link("testAL", "testAG1", "testAG2")
85 raw_input("[Press enter to continue]" )
87 run_interactive_mode()
89 if __name__ == "__main__":