设为首页 收藏本站
查看: 438|回复: 0

[经验分享] python 分析日志

[复制链接]

尚未签到

发表于 2017-4-24 10:03:30 | 显示全部楼层 |阅读模式
使用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}|

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-368496-1-1.html 上篇帖子: Python网页爬虫 下篇帖子: Python zipfile简介
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表