首页 > 代码库 > apache日志分析(python)
apache日志分析(python)
#!/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的技术博客” 博客,谢绝转载!
apache日志分析(python)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。