Read the Apache access log to see the number of bytes received per individual client connection

Source: Internet
Author: User
Tags apache access log

The apache2 log files in Ubuntu are located in:

/var/log/apache2

Code:

#Coding=utf-8ImportSYS" "data 127.0.0.1--[10/jan/2017:10:08:16 +0800] "post/cgi-bin/login.py http/1.1" 335 "-" curl/7.35.0 "" "defDictify_logline (line): Split_line=Line.split ()return {        "remote_address": split_line[0],"Status": split_line[8],        "bytes_sent": split_line[9]    }defGenerate_log_report (logfile): Report_dict= {}     forLineinchlogfile:line_dict=Dictify_logline (line)Printline_dictTry: Bytes_send= Int (line_dict["bytes_sent"])        exceptValueError:Continue        #Count each IP, corresponding to the number of bytes sentReport_dict.setdefault (line_dict["remote_address"], []). Append (bytes_send)returnreport_dictif __name__=="__main__":    if  notLen (SYS.ARGV) > 1: Sys.exit (1) Infile_name= Sys.argv[1]    Try: infile= Open (Infile_name,'R')    exceptIOError:Print 'You must specify a valid file to parse'Sys.exit (1) Log_report=Generate_log_report (infile)Printlog_report infile.close ()#Regular Expressions Extract dataImportRelog_line_re= Re.compile (" "(? p<remote_address>\s+) #IP address\s+ #whitespace \s+ #remote logname\s+ #whitepsace \s+ #remote user\s+ #whitespa Ce\[[^\[\]]+\] #time \s+ #whitespace "[^"]+ "#first line of request\s+ #whitesapce (? p<status>\d+) \s+ #whitespace (? p<bytes_sent>-|\d+) \s*" ", Re. VERBOSE)defDictify_logline (line): M=Log_line_re.match (line)ifm:groupdict=m.groupdict ()ifgroupdict['bytes_sent'] =='-': groupdict['bytes_sent'] ='0'        returngroupdictElse:        return {            "remote_address": None,"Status": None,"bytes_sent":"0"        }

Effect:

Read the Apache access log to see the number of bytes received per independent client connection

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.