Merge "Merge commit 'refs/changes/43/1843/1' of https://git.opendaylight.org/gerrit...
authorSuchi Raman <suchi.raman@plexxi.com>
Wed, 16 Oct 2013 17:35:43 +0000 (17:35 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 16 Oct 2013 17:35:43 +0000 (17:35 +0000)
1  2 
scripts/analytics.py

diff --combined scripts/analytics.py
index 4e8e592b7121962182729e61d87dfa05ac03db5c,de20742e201f350cd8a6968246af895c310f3030..ccf327fece8835e4e2e9f43f454cd1347e0c8da7
@@@ -44,14 -44,6 +44,14 @@@ class Stats
          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"):
@@@ -60,7 -52,6 +60,7 @@@
              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:
@@@ -184,38 -174,46 +184,46 @@@ def run_interactive_mode()
              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():
@@@ -259,9 -257,9 +267,9 @@@ def main()
      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()