zj2092 发表于 2017-4-24 10:03:30

python 分析日志

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

import re
r = re.compile("\d+")
f = open("log","r")
head = re.compile("HEAD /*")
options = re.compile("OPTIONS /*")
post = re.compile(" /*")
hc = re.compile("\" \d*")
ma = re.compile(":7199 *")
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()).strip()
if not url and head.search(read):
url = str(head.search(read).group()).strip()
url =r.sub("0",url)
if url!='/':
url = url+'/'
match = hc.search(read)
if match:
http_code = str(match.group())
match = ma.search(read)
if match:
http_time = str(match.group())
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,"http_time":http_time,"http_count":1,"http_code":{http_code:1}}
else:
if not http_time:
http_time=0
if result["http_code"].get(http_code) is None:
result["http_code"] = 1
else:
result["http_code"] = result["http_code"]+1
result["http_count"] = result["http_count"]+1
ht = result["http_time"] +http_time
result["http_time"]= ht
list = []
for r in result:
http_count =result["http_count"]
result["http_time"] = result["http_time"]/http_count
list.append(result)
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 - "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 - "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 - "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 - "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}|
页: [1]
查看完整版本: python 分析日志