#!/usr/bin/env python import sys import datetime import socket from file_backwards import * MONTH = { ‘Jan‘:1, ‘Feb‘:2, ‘Mar‘:3, ‘Apr‘:4, ‘May‘:5, ‘Jue‘:6, ‘Jul‘:7, ‘Aug‘:8, ‘Sep‘:9, ‘Oct‘:10, ‘Nov‘:11, ‘Dec‘:12, } def parse_apache_date(datestr): day, 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] += 1 else: d[k] = 1 def 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 result if __name__ == ‘__main__‘: now = datetime.datetime.now() timedelta = datetime.timedelta(minutes=10) ten_m_ago = now - timedelta key = ‘http.count‘ data = parse_apache_log(sys.argv[1], ten_m_ago) sock = socket.socket() sock.connect((‘127.0.0.1‘, 2003)) print data for k, v in data.items(): sock.send("%s %d %s\n" % (key, v, k))
本文出自 “muzinan的技术博客” 博客,谢绝转载!
原文地址:http://muzinan110.blog.51cto.com/684213/1558810