Peter Nixon avatar Peter Nixon committed 787153e

Add UnisonWebLogCounter class and bump file version number

Comments (0)

Files changed (2)

bmutils/__init__.py

-from bmutils import UnisonWebLogParser
+from bmutils import UnisonWebLogCounter, UnisonWebLogParser
 
 __author__ = "Peter Nixon"
 __copyright__ = "Copyright (C) 2012 Peter Nixon"
 __license__ = "Public Domain"
-__version__ = "1.0"
+__version__ = "1.1"

bmutils/bmutils.py

 __author__ = "Peter Nixon"
 __copyright__ = "Copyright (C) 2012 Peter Nixon"
 __license__ = "Public Domain"
-__version__ = "1.0"
+__version__ = "1.1"
 
 import re
 import time
 from urlparse import urlparse
 import tldextract
 
+try:
+    from collections import defaultdict, Counter
+except:
+    from collections27 import defaultdict, Counter
+
 DEBUG = False
 
 logging.basicConfig()
         if (process_hostnames == True):     # This is slower which is why it's not on by default
             o = self.getdomainname()
         
-        return True
+        return True
+
+class UnisonWebLogCounter:
+    ''' ByteMobile Unison Web Access Logfile Parser '''
+
+    def __init__(self):
+        self.status_dict = defaultdict(lambda: Counter())
+        self.status_counter = Counter()
+        self.host_counter = Counter()
+        self.msisdn_counter = Counter()
+        self.total_requests = 0
+        self.bypassed_requests = 0
+        self.malformed_lines = 0
+    
+    def add(self, domainname, status, msisdn):
+        self.total_requests += 1
+        if status == 0:
+            self.bypassed_requests += 1
+        else:
+            self.host_counter[domainname] += 1
+            self.status_counter[status] +=1
+            self.msisdn_counter[msisdn] += 1
+            self.status_dict[domainname][status] +=1
+
+    def dump(self):
+        proxy_fail_percent = float(self.status_counter[504]) / float(self.total_requests) * 100
+        print "Total Requests:", self.total_requests
+        print "Failed Requests:", self.status_counter[504]
+        print "HTTP Failure Rate:", proxy_fail_percent
+        print "Effected Subscribers:", len(self.msisdn_counter)
+        print "Effected Domains:", len(self.host_counter)
+        self.host_report()
+        self.msisdn_report()
+    
+    def msisdn_report(self):
+        n = 1
+        print "Top MSISDN (Total: %d)" % (len(self.msisdn_counter))
+        for (key, num) in self.msisdn_counter.most_common(25):
+            print n, key, num
+            n += 1
+
+    def host_report(self):
+        n = 1
+        print "Top Hosts"
+        for (key, num) in self.host_counter.most_common(100):
+            print n, key, num
+            #for (key2, num2) in self.status_dict[key].most_common(1):
+            #    print key2
+            n += 1
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.