Python Analytics Apache access log script sharing

Source: Internet
Author: User
#!/usr/bin/env python# coding=utf-8 #------------------------------------------------------# name:apache Log Parsing script # Pur Pose: This script is only used to parse Apache access log # version:2.0# author:leo# created:2013-4-26# modified:2013-5-4# Copyright: (c) L EO 2013#------------------------------------------------------Import sysimport time #该类是用来打印格式class DisplayFormat (  Object): Def format_size (self,size): "Formatted traffic unit '" KB = 1024x768 = 1048576 GB = 1073741824 TB =    1099511627776 if size >= tb:size = str (SIZE/TB) + ' T ' elif size < kb:size = str (size) + ' B '  Elif size >= GB and size < Tb:size = str (SIZE/GB) + ' G ' elif size >= MB and size < Gb:size = str (SIZE/MB) + ' M ' else:size = str (size/kb) + ' K ' return size formatstring = '%-15s%-10s%-12s%8s %10s%10s%10s%10s%10s%10s%10s ' def transverse_line (self): ' Output horizontal line ' ' Print self.formatstring% '-'-' *15, '-' * * 0, '-' *12, '-' *12, '-' *10, '-' *10, '-' *10, '-' *10, '-' *10, '-' *10, '-' *10 ' def head (self): ' Output header info ' ' Print self.formatstring% (' IP ', ' traffic ', ' Times ', ' times% ', ' $ ', ' 404 ', ' $ ', ' 403 ', ' 302 ', ' 304 ', ' 503 ') def error_print (self): ' Output error message ' ' Print print ' Usage: '     + sys.argv[0] + ' Apachelogfilepath [number] ' Print sys.exit (1) def execut_time (self): ' Output script execution time ' ' Print Print "Script Execution time:%.3f second"% Time.clock () print #该类是用来生成主机信息的字典class Hostinfo (object): Host_inf o = [' $ ', ' 404 ', ' $ ', ' 302 ', ' 304 ', ' 503 ', ' 403 ', ' Times ', ' size '] def __init__ (self,host): Self.host = host = {}.fromkey S (self.host_info,0) def increment (self,status_times_size,is_size): "This method is used to add 1" if status_times_s to each value in the Host_info ize = = ' Times ': self.host[' times ' + = 1 elif is_size:self.host[' size '] = self.host[' size ' + status_times_si Ze else:self.host[status_times_size] + = 1 def get_value (self,value): "The method is to take the corresponding value in each host information" ' return self. Host[value] #该类是用来分析文件clFileanalysis (object): Def __init__ (self): "Initialize an empty dictionary" "self.report_dict = {} self.total_request_times,self . total_traffic,self.total_200, self.total_404,self.total_500,self.total_403,self.total_302, self.total_304, self.total_503 = 0,0,0,0,0,0,0,0,0 def split_eachline_todict (self,line): "Splits each line in the file and returns a dictionary" split_line = lines . Split () Split_dict = {' Remote_host ': split_line[0], ' status ': Split_line[-2], ' bytes_sent ': Split_line[-1],} return SPL      It_dict def generate_log_report (self,logfile): "reads the file, parses the Split_eachline_todict method generated by the dictionary" ' for line in logfile: Try:line_dict = Self.split_eachline_todict (line) host = line_dict[' remote_host '] status = Line_dict [' status '] except valueerror:continue except indexerror:continue if host not in Self.rep Ort_dict:host_info_obj = Hostinfo (host) self.report_dict[host] = Host_info_obj Else:host_inf  O_obj = Self.report_dict[host]     Host_info_obj.increment (' Times ', False) if status in Host_info_obj.host_info:host_info_obj.increment (      Status,false) try:bytes_sent = Int (line_dict[' bytes_sent ')) except valueerror:bytes_sent = 0 Host_info_obj.increment (Bytes_sent,true) return self.report_dict def return_sorted_list (self,true_dict): "' Calculation      Number of States, total amount of traffic, number of requests, and calculate the total amount of each state and generate a true dictionary, convenient to sort ' for host_key in true_dict:host_value = True_dict[host_key] Times = Host_value.get_value (' times ') Self.total_request_times = Self.total_request_times + times size = Hos      T_value.get_value (' size ') self.total_traffic = self.total_traffic + Size o200 = Host_value.get_value (' 200 ')      o404 = host_value.get_value (' 404 ') o500 = host_value.get_value (' + ') o403 = Host_value.get_value (' 403 ')       o302 = Host_value.get_value (' 302 ') o304 = Host_value.get_value (' 304 ') o503 = Host_value.get_value (' 503 ') True_dict[host_key] = {' $ ': o200, ' 404 ': o404, ' $ ': o500, ' 403 ': o403, ' 302 ': o302, ' 304 ': o304, ' 503 ': o503, ' times ': times, ' Size ': size} self.total_200 = self.total_200 + o200 self.total_404 = self.total_404 + o404 self.total_500 = self.total _500 + o500 self.total_302 = self.total_302 + o302 self.total_304 = self.total_304 + o304 self.total_503 =    self.total_503 + o503 sorted_list = sorted (True_dict.items (), Key=lambda t: (t[1][' times '],t[1][' size '), reverse=true) Return Sorted_list class Main (object): Def Main (self): "' keynote function ' Display_format = DisplayFormat () arg_lengt      h = Len (sys.argv) If arg_length = = 1:display_format.error_print () elif arg_length = = 2 or Arg_length = = 3:  Infile_name = sys.argv[1] try:infile = open (Infile_name, ' R ') if Arg_length = = 3:lines = Int (sys.argv[2]) else:lines = 0 except ioerror,e:print print e Display_form At.error_print () excEPT valueerror:print print "Please Enter A volid number!!" Display_format.error_print () else:display_format.error_print () Fileanalysis_obj = Fileanalysis () not_true _dict = Fileanalysis_obj.generate_log_report (infile) Log_report = Fileanalysis_obj.return_sorted_list (not_true_dict ) total_ip = Len (log_report) If Lines:log_report = Log_report[0:lines] Infile.close () print total_t Raffic = Display_format.format_size (fileanalysis_obj.total_traffic) total_request_times = FileAnalysis_obj.total_ Request_times print ' Total IP:%s All traffic:%s Total Request times:%d '% (total_ip,total_traffic,total_r Equest_times) Print display_format.head () Display_format.transverse_line () for host in Log_report:time s = host[1][' times '] times_percent = (float (times)/float (fileanalysis_obj.total_request_times)) * Print di Splay_format.formatstring% (host[0], display_forMat.format_size (host[1][' size '), Times,str (times_percent) [0:5], host[1][' "],host[1][' 404 '", host[1]['],host[1][' 403 '], host[1][' 302 '],host[1 [' 304 '],host[1][' 503 ']) if (not lines) or total_ip = = Lines:display_format.transverse_ Line () print display_format.formatstring% (total_ip,total_traffic, total_request_times, ' 100                         % ', fileanalysis_obj.total_200, fileanalysis_obj.total_404, fileanalysis_obj.total_500, fileanalysis_obj.total_403, Fileana lysis_obj.total_302, fileanalysis_obj.total_304, fileanalysis_obj.total_5 Display_format.execut_time () if __name__ = = ' __main__ ': main_obj = Main () Main_obj.main ()
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.