1 #!/usr/local/bin/python
9 from threading import Thread
11 from affinity_control import AffinityControl
12 from subnet import SubnetControl
13 from stats import Stats
15 # If True, SIG_INT has been captured
20 def signal_handler(signal, frame):
25 class WaypointMonitor(Thread):
27 def __init__(self, monitor_type, **kwargs):
29 self.stat = Stats(monitor_type, **kwargs)
30 self.stat_type = monitor_type
31 self.waypoint_address = None
33 def set_waypoint(self, waypoint_ip):
34 self.waypoint_address = waypoint_ip
35 print "Registered waypoint for %s. Any large flows will be redirected to %s." % (self.stat, waypoint_ip)
41 _, is_big = self.stat.refresh()
42 if is_big and not did_waypoint:
43 print "Large flow detected"
44 ac = AffinityControl()
45 # First AG: Sources sending data into this subnet
46 src_ag_name = "sources"
47 src_ips = self.stat.get_incoming_hosts()
48 ac.add_affinity_group(src_ag_name, ips=src_ips)
49 # Second AG: This entity
50 dst_ag_name = "client"
51 if (self.stat_type == Stats.TYPE_PREFIX):
52 ac.add_affinity_group(dst_ag_name, subnet=self.stat.subnet)
53 elif (self.stat_type == Stats.TYPE_HOST):
56 print "type", self.stat_type, "not supported for redirection"
59 ac.add_affinity_link(link_name, src_ag_name, dst_ag_name)
60 ac.add_waypoint(link_name, self.waypoint_address)
61 ac.enable_waypoint(link_name)
67 # Default subnet is required for the host tracker to work.
68 subnet_control = SubnetControl()
69 subnet_control.add_subnet("defaultSubnet", "10.0.0.254/8")
71 m = WaypointMonitor(Stats.TYPE_PREFIX, subnet="10.0.0.0/31")
72 m.set_waypoint("10.0.0.2")
75 # Register signal-handler to catch SIG_INT
76 signal.signal(signal.SIGINT, signal_handler)
79 # join() won't return until SIG_INT has been captured
82 if __name__ == "__main__":