#!/usr/bin/python
-import httplib2
-import json
+import analytics
'''
Class for keeping track of host statistics
if stat_type == Stats.TYPE_HOST:
self.src = kwargs['src']
self.dst = kwargs['dst']
- self.url_prefix = "http://localhost:8080/affinity/nb/v2/analytics/default/hoststats/"
elif stat_type == Stats.TYPE_AL:
self.al = kwargs['al']
- self.url_prefix = "http://localhost:8080/affinity/nb/v2/analytics/default/affinitylinkstats/"
elif stat_type == Stats.TYPE_SUBNET:
self.subnet = kwargs['subnet']
- self.url_prefix = "http://localhost:8080/affinity/nb/v2/analytics/default/subnetstats/"
else:
print "incorrect stat type", stat_type
self.stats = {}
+ self.protocol_stats = {}
self.rate_ewma = None
self.large_flow_threshold = 5 * 10**6 # in bytes
- self.http = httplib2.Http(".cache")
- self.http.add_credentials('admin', 'admin')
def __str__(self):
if (self.stat_type == Stats.TYPE_HOST):
# Refresh statistics
def refresh(self):
if (self.stat_type == Stats.TYPE_HOST):
- resp, content = self.http.request(self.url_prefix + self.src + "/" + self.dst, "GET")
+ self.stats = analytics.stats_hosts(self.src, self.dst, False)
+ self.protocol_stats = analytics.all_stats_hosts(self.src, self.dst, False)
elif (self.stat_type == Stats.TYPE_AL):
- resp, content = self.http.request(self.url_prefix + self.al, "GET")
+ self.stats = analytics.stats_link(self.al, False)
+ self.protocol_stats = analytics.all_stats_link(self.al, False)
elif (self.stat_type == Stats.TYPE_SUBNET):
- resp, content = self.http.request(self.url_prefix + "null/null/" + self.subnet, "GET")
+ self.stats = analytics.stats_subnet("null/null", self.subnet, False)
+ self.protocol_stats = analytics.all_stats_subnet("null/null", self.subnet, False)
try:
- self.stats = json.loads(content)
is_fast = self.handle_rate_ewma()
is_big = self.check_large_flow()
return [is_fast, is_big]
- except Exception as e:
- print "error: ", e
+ except:
return [False, False]
def set_large_flow_threshold(self, s):
# Return all hosts that transferred a particular percentage of data into this entity. Right now only supports subnets.
def get_large_incoming_hosts(self):
if (self.stat_type == Stats.TYPE_SUBNET):
- resp, content = self.http.request(self.url_prefix + "incoming/" + self.subnet, "GET")
- data = json.loads(content)
+ data = analytics.incoming_hosts(self.subnet, False)
if (data == {}): return []
- data = data['stats']
ips = []
total_bytes_in = self.get_bytes()
n = len(data)
if (bytes_from_ip >= total_bytes_in / float(n)):
ips.append(ip)
return ips
-
else:
print "Stat type not supported for incoming hosts"
return []
return False
# Bytes
- def get_bytes(self):
+ def get_bytes(self, protocol=None):
try:
- bytes = long(self.stats["byteCount"])
+ if (protocol == None):
+ bytes = long(self.stats["byteCount"])
+ else:
+ bytes = long(self.protocol_stats[protocol]["byteCount"])
except Exception as e:
- print "exception: ", e
bytes = 0
return bytes
+ # Packets
+ def get_packets(self, protocol=None):
+ try:
+ if (protocol == None):
+ packets = long(self.stats["packetCount"])
+ else:
+ packets = long(self.protocol_stats[protocol]["byteCount"])
+ except Exception as e:
+ packets = 0
+ return packets
+
# Bit Rate
def get_bit_rate(self):
try: