python 分析日志



使用python写了一个分析URL请求时间的脚本
import re

r = re.compile("\d+")

f = open("log","r") 
head = re.compile("HEAD /[a-zA-Z/]*")
options = re.compile("OPTIONS /[a-zA-Z/0-9?=&]*")
post = re.compile("[POST|GET] /[a-zA-Z0-9/]*")
hc = re.compile("[HTTP/1.1|HTTP/1.0]\" \d*")
ma = re.compile(":7199 [0-9.]*")
result = {}


for read in f.readlines():
	url=http_code=http_time = None
	
	if options.search(read):
		#print options.search(read).group()
		continue
	match =  post.search(read)	
	if match:
		url = str(match.group())[2:].strip()
	if not url and head.search(read):
		url = str(head.search(read).group())[5:].strip()		
	url =  r.sub("0",url)
	if url[len(url)-1:]!='/':
		url = url+'/'
	match = hc.search(read)
	if match:
		http_code = str(match.group())[3:]
	match = ma.search(read)
	if match:
		http_time = str(match.group())[6:]
		if len(http_time)==0:
		   http_time = 0
		else:
		   http_time = float(http_time)

	if not result.get(url):
		if not http_time:
			http_time=0
		result[url] = {"url":url,"http_time":http_time,"http_count":1,"http_code":{http_code:1}}
	else:
		if not http_time:
			http_time=0
	
		if result[url]["http_code"].get(http_code) is None:
			result[url]["http_code"][http_code] = 1
		else:
			result[url]["http_code"][http_code] = result[url]["http_code"][http_code]+1
		
		result[url]["http_count"] = result[url]["http_count"]+1
		ht = result[url]["http_time"] +http_time
		result[url]["http_time"]  = ht

list = []
for r in result:
    http_count =  result[r]["http_count"] 	
    result[r]["http_time"] = result[r]["http_time"]/http_count
    list.append(result[r])

list.sort(cmp=lambda x,y : cmp(x["http_count"], y["http_count"]),reverse=True)  
for x in list:
    print "|%s |%s |%s |%s|" %(x["url"],x["http_count"],x["http_time"],x["http_code"])


日志:
175.145.249.158 - null - null - [01/Jun/2012:14:59:09 +0800]"GET /object/comments/1/20/?comment_message_id=8719418&1338533947531 HTTP/1.1" 200 365 "http://www.duitang.com/people/mblog/8719418/detail/?next=8719523" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5" 0.047 192.168.172.8:8080 0.047 .
203.145.159.148 - null - null - [01/Jun/2012:14:59:10 +0800]"GET /album/470553/masn/p/2/24/ HTTP/1.1" 200 15751 "http://www.duitang.com/album/470553/?from=detail_right" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7" 0.607 192.168.172.8:8080 0.607 .
219.229.109.49 - ymylcf - 2e3ee5b52a33eb2f8badf49f2be60fb6 - [01/Jun/2012:14:59:10 +0800]"GET /hot/masn/?page=4&page_size=24&_type=&begin_time=1338533938 HTTP/1.1" 200 19434 "http://www.duitang.com/topics/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19" 1.619 192.168.172.10:8080 1.619 .
218.249.50.150 - %E8%8C%B92805819 - a6b134828a8489862de77668876840d4 - [01/Jun/2012:14:59:11 +0800]"GET /people/663035/ HTTP/1.1" 302 5 "http://www.duitang.com/myhome/" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19 QIHU 360EE" 0.006 192.168.172.7:8080 0.006 .

输出:
|/blog/unread/ |11782 |0.0367470718044 |{'200': 11623, '302': 146, '502': 1, '499': 1, '500': 11}|
|/people/mblog/0/detail/ |9720 |0.314687345679 |{'200': 8870, '301': 187, '302': 52, '404': 529, '403': 54, '499': 24, '502': 4}|
|/blogs/tag/hot/0/ |8613 |0.406990711715 |{'301': 4310, '499': 6, '200': 4297}|
|/api/blog/detail/ |2103 |0.0471583452211 |{'200': 2034, '499': 68, '500': 1}|
|/album/0/masn/p/0/0/ |2081 |0.847947621336 |{'200': 2062, '499': 8, '301': 11}|
|/search/ |1785 |0.793348459384 |{'200': 1783, '499': 2}|
|/hot/masn/ |1620 |1.07875864198 |{'200': 1618, '499': 2}|

猜你喜欢

转载自san-yun.iteye.com/blog/1626653