#!/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)