用Python分析Apache等Web日志


1 分析日志的python框架awk.py

  1. #  
  2. # Custom awk.py module  
  3. #  
  4.   
  5.   
  6. class controller:  
  7.   
  8.     def __init__(self, f):  
  9.         self.m_file = f  
  10.         self.m_handlers = []  
  11.   
  12.   
  13.     def subscribe(self, o):  
  14.         self.m_handlers.append(o)  
  15.   
  16.     def run(self):  
  17.   
  18.         for o in self.m_handlers:  
  19.             o.begin()  
  20.   
  21.         s = self.m_file.readline()  
  22.   
  23.         while s != "":  
  24.   
  25.             for o in self.m_handlers:  
  26.                 o.process_line(s)  
  27.   
  28.             s = self.m_file.readline()  
  29.   
  30.   
  31.         for o in self.m_handlers:  
  32.             o.end()  
  33.   
  34.   
  35.     def print_results(self):  
  36.   
  37.         print  
  38.         print "Results:"  
  39.         print  
  40.   
  41.         for o in self.m_handlers:  
  42.             print "------------------------------------------------------"  
  43.             print o.description()  
  44.             print "------------------------------------------------------"  
  45.             print o.result()  
统计日志的点击量count_line.py
  1. # Standard sys module  
  2. import sys  
  3.   
  4. # Custom awk.py module  
  5. import awk  
  6.   
  7. class count_lines:  
  8.   
  9.     def begin(self):  
  10.         self.m_count = 0  
  11.   
  12.     def process_line(self, s):  
  13.         self.m_count += 1  
  14.   
  15.     def end(self):  
  16.         pass  
  17.   
  18.     def description(self):  
  19.         return "# of lines in the file"  
  20.   
  21.     def result(self):  
  22.         return self.m_count  
  23.   
  24.   
  25. #  
  26. # Step 1: Create the Awk controller  
  27. #  
  28. ac = awk.controller(sys.stdin)  
  29.   
  30. #  
  31. # Step 2: Subscribe the handler  
  32. #  
  33. ac.subscribe(count_lines())  
  34.   
  35. #  
  36. # Step 3: Run  
  37. #  
  38. ac.run()  
  39.   
  40. #  
  41. # Step 4: Print the results  
  42. #  
  43. ac.print_results()  

使用方法是shell中执行

# cat apachelog.log|python count_lines.py


统计浏览次数超过n次的访问者  visitors.py

How many people have returned to the site more than N times?

  1. import re;  
  2. import sys  
  3. imort awk  
  4.   
  5. class return_visitors:  
  6.   
  7.     def __init__(self, n):  
  8.         self.m_n = n;  
  9.         self.m_ip_days = {};  
  10.   
  11.     def begin(self):  
  12.         pass;  
  13.   
  14.     def process_line(self, s):  
  15.   
  16.         try:  
  17.             array = s.split();  
  18.             ip = array[0];  
  19.             day = array[3][1:7];  
  20.   
  21.             if self.m_ip_days.has_key(ip):  
  22.   
  23.                 if day not in self.m_ip_days[ip]:  
  24.                     self.m_ip_days[ip].append(day);  
  25.   
  26.             else:  
  27.                 self.m_ip_days[ip] = [];  
  28.                 self.m_ip_days[ip].append(day);  
  29.   
  30.         except IndexError:  
  31.             pass;  
  32.   
  33.   
  34.   
  35.     def end(self):  
  36.   
  37.         ips = self.m_ip_days.keys();  
  38.         count = 0;  
  39.   
  40.         for ip in ips:  
  41.   
  42.             if len(self.m_ip_days[ip]) > self.m_n:  
  43.                 count += 1;  
  44.   
  45.         self.m_count = count;  
  46.   
  47.   
  48.     def description(self):  
  49.         return "# of IP addresses that visited more than %s days" % self.m_n;  
  50.   
  51.     def result(self):  
  52.         return self.m_count;  
  53. ac = awk.controller(sys.stdin)  
  54. ac.subscribe(return_visitors(2))  
  55. ac.run()  
  56. ac.print_results()  

# cat apachelog.log|python visitors.py

按照域名统计访问量domain.py
  1. import re;  
  2. import sys  
  3. imort awk  
  4.   
  5. class referring_domains:  
  6.   
  7.     def __init__(self):  
  8.         self.m_domains = {};  
  9.   
  10.     def begin(self):  
  11.         pass;  
  12.   
  13.     def process_line(self, line):  
  14.   
  15.         try:  
  16.             array = line.split();  
  17.             referrer = array[10];  
  18.   
  19.             m = re.search('//[a-zA-Z0-9\-\.]*\.[a-zA-z]{2,3}/',  
  20.                       referrer);  
  21.   
  22.             lenlength = len(m.group(0));  
  23.             domain = m.group(0)[2:length-1];  
  24.   
  25.             if self.m_domains.has_key(domain):  
  26.                 self.m_domains[domain] += 1;  
  27.             else:  
  28.                 self.m_domains[domain] = 1;  
  29.   
  30.         except AttributeError:  
  31.             pass;  
  32.         except IndexError:  
  33.             pass;  
  34.   
  35.   
  36.     def end(self):  
  37.         pass;  
  38.   
  39.   
  40.     def description(self):  
  41.         return "Referring domains";  
  42.   
  43.   
  44.     def sort(self, key1, key2):  
  45.         if self.m_domains[key1] > self.m_domains[key2]:  
  46.             return -1;  
  47.         elif self.m_domains[key1] == self.m_domains[key2]:  
  48.             return 0;  
  49.         else:  
  50.             return 1;  
  51.   
  52.   
  53.     def result(self):  
  54.   
  55.         s = "";  
  56.         keys = self.m_domains.keys();  
  57.         keys.sort(self.sort);  
  58.   
  59.         for domain in keys:  
  60.             s += domain;  
  61.             s += " ";  
  62.             s += str(self.m_domains[domain]);  
  63.             s += "\n";  
  64.   
  65.         s += "\n\n";  
  66.   
  67.         return s;  
  68. ac = awk.controller(sys.stdin)  
  69. ac.subscribe(referring_domains())  
  70. ac.run()  
  71. ac.print_results()  
# cat apachelog.log|python domain.py

相关内容