self.http.add_credentials('admin', 'admin')
self.refresh()
+ def __str__(self):
+ if (self.stat_type == "host"):
+ return "host pair %s -> %s" % (self.src, self.dst)
+ elif (self.stat_type == "affinityLink"):
+ return "AffinityLink %s" % self.al
+ else:
+ return "Unknown Stats type"
+
# Refresh statistics
def refresh(self):
if (self.stat_type == "host"):
resp, content = self.http.request(self.url_prefix + self.al, "GET")
self.stats = json.loads(content)
self.handle_rate_ewma()
+ self.check_large_flow()
# EWMA calculation for bit rate
def handle_rate_ewma(self):
else:
new_rate_ewma = alpha * new_bitrate + (1 - alpha) * self.rate_ewma
if (self.rate_ewma > 0 and new_rate_ewma > anomaly_threshold * self.rate_ewma):
- if (self.stat_type == "host"):
- print "!! Anomaly detected between %s and %s" % (self.src, self.dst)
- elif (self.stat_type == "affinityLink"):
- print "!! Anomaly detected on AffinityLink %s" % (self.al)
+ print "!! Anomaly detected on %s" % self
print "!! Rate rose from %1.1f Mbit/s to %1.1f Mbit/s" % ((self.rate_ewma/10**6), (new_rate_ewma/10**6))
self.rate_ewma = new_rate_ewma
+ def check_large_flow(self):
+ if (self.get_bytes() > 5 * (10**6)):
+ print "!! Large flow detected on %s" % self
+
# Bytes
def get_bytes(self):
try:
if (request_type == "quit"):
sys.exit()
- action = request[1]
-
if (request_type == "host"):
+ action = request[1]
src, dst = request[2:4]
+ host_stat = Stats("host", src=src, dst=dst)
if (action == "bytes"):
- host_stat = Stats("host", src=src, dst=dst)
print("%d bytes between %s and %s" % (host_stat.get_bytes(), src, dst))
elif (action == "rate"):
- host_stat = Stats("host", src=src, dst=dst)
print("%f bit/s between %s and %s" % (host_stat.get_bit_rate(), src, dst))
else:
+ print "wrong action"
raise Exception
- # TODO: Change this to use AffinityLinkStats
elif (request_type == "link"):
+ action = request[1]
link = request[2]
- h = httplib2.Http(".cache")
- h.add_credentials("admin", "admin")
- resp, content = h.request("http://localhost:8080/affinity/nb/v2/analytics/default/affinitylinkstats/" + link, "GET")
- al_stats = json.loads(content)
-
+ link_stat = Stats("affinityLink", al=link)
if (action == "bytes"):
- print("%d bytes on %s" % (long(al_stats["byteCount"]), link))
+ print("%d bytes on %s" % (link_stat.get_bytes(), link))
elif (action == "rate"):
- print("%f bit/s on %s" % (float(al_stats["bitRate"]), link))
+ print("%f bit/s on %s" % (link_stat.get_bit_rate(), link))
else:
+ print "wrong action 2"
raise Exception
+ elif (request_type == "prefix"):
+ prefix = request[1]
+ h = httplib2.Http(".cache")
+ h.add_credentials("admin", "admin")
+ url_prefix = "http://localhost:8080/affinity/nb/v2/analytics/default/prefixstats/"
+ resp, content = h.request(url_prefix + prefix, "GET")
+ if (resp.status == 200):
+ data = json.loads(content)
+ print data['byteCount'], "bytes"
+
else:
+ print "something else"
raise Exception
except Exception as e:
print "Error"
+ print e
def get_all_hosts():
affinity_control.add_affinity_group("testAG1", ["10.0.0.1", "10.0.0.2"])
affinity_control.add_affinity_group("testAG2", ["10.0.0.3", "10.0.0.4"])
affinity_control.add_affinity_link("testAL", "testAG1", "testAG2")
- raw_input("[Press enter to continue] ")
+ raw_input("[Press enter to continue]" )
- interactive_mode = False
+ interactive_mode = True
if interactive_mode:
run_interactive_mode()