Demo script enables waypoint now. Fixed original analytics script so that it can...
[affinity.git] / scripts / analytics.py
1 #!/usr/local/bin/python
2
3 import httplib2
4 import json
5 import sys
6 import time
7
8 from stats import Stats
9 from subnet import SubnetControl
10 from affinity_control import AffinityControl
11
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
17 #    > h1 ping h3
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
22
23 def run_interactive_mode():
24
25     print "Usage: [host | link] [bytes | rate] [src dst | link-name]"
26
27     # Demo mode
28     while True:
29         request = raw_input("> ")
30         try:
31             request = request.split()
32             request_type = request[0]
33
34             if (request_type == "quit"):
35                 sys.exit()
36
37             if (request_type == "host"):
38                 action = request[1]
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))
45                 else:
46                     raise Exception
47
48             elif (request_type == "link"):
49                 action = request[1]
50                 link = request[2]
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))
56                 else:
57                     raise Exception
58
59             elif (request_type == "prefix"):
60                 prefix = request[1]
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"
68             else:
69                 raise Exception
70         except Exception as e:
71             print "Error"
72             print e
73
74 def main():
75
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")
79
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]" )
86
87     run_interactive_mode()
88
89 if __name__ == "__main__":
90     main()