Apache log analysis (python)

Source: Internet
Author: User
Tags apache log timedelta

#!/usr/bin/env pythonimport sysimport datetimeimport socketfrom file_backwards  import *month = {' Jan ': 1, ' Feb ': 2, ' Mar ': 3, ' APR ': 4, ' May ': 5, ' Jul ': 6, ' Jue ': 7, ' SEP ': 8, ' Oct ' : Ten, ' Nov ': One, ' Dec ': 12,}def parse_apache_date (datestr):d ay, month, yearandtime =  Datestr.split ('/') year, hour, minute,second= yearandtime.split (': ') return  Datetime.datetime (int (year), Month[month],int (day), int (hour), int (minute)) def countdict (d, k):if  K in d:d[k] += 1else:d[k] = 1def parse_apache_log (logfile,ten_m): Result  = {}with open (logfile)  as fd:for line in filerev (FD): Splited_line  = line.split () Datestr = splited_line[3][1:]apache_date = parse_apache_date ( DATESTR) if apache_date > ten_m:countdict (result, apache_date.strftime ('%s ')) Else:return  resultif __name__ ==  ' __Main__ ': Now = datetime.datetime.now () Timedelta = datetime.timedelta (minutes=10) Ten_m_ago  = now - timedeltakey =  ' Http.count ' Data = parse_apache_log (sys.argv[1 ], ten_m_ago) Sock = socket.socket () Sock.connect ((' 127.0.0.1 ',  2003)) print datafor  k, v in data.items (): Sock.send ("%s %d %s\n"  %  (key, v, k))


This article from "Muzinan Technology blog" blog, declined reprint!

Apache log analysis (python)

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.