python,日志分析脚本
2012-03-06 20:28
323 查看
做运维的朋友刚开始写python,就用这段分析日志代码算作入门吧
下面是日志格式
输出结果
import re tudou@Gyyx f=open("/tmp/a.log","r") arr={} lines = f.readlines() for line in lines: ipaddress=re.compile(r'^#(((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?))') match=ipaddress.match(line) if match: ip = match.group(1) if(arr.has_key(ip)): arr[ip]+=1 else: arr.setdefault(ip,1) f.close() for key in arr: print key+"->"+str(arr[key])
下面是日志格式
#111.172.249.84 - - [12/Dec/2011:05:33:36 +0800] "GET /images/i/goTop.png HTTP/1.0" 200 486 "http://wh.xxxx.com/" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" #111.172.249.84 - - [12/Dec/2011:05:33:36 +0800] "GET /images/i/goTop.png HTTP/1.0" 200 486 "http://wh.xxxx.com/" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" #111.172.249.85 - - [12/Dec/2011:05:33:36 +0800] "GET /images/i/goTop.png HTTP/1.0" 200 486 "http://wh.xxxx.com/" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" #111.172.249.86 - - [12/Dec/2011:05:33:36 +0800] "GET /images/i/goTop.png HTTP/1.0" 200 486 "http://wh.xxxx.com/" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"
输出结果
111.172.249.86->1 111.172.249.84->2 111.172.249.85->1
相关文章推荐
- 分析nginx日志脚本之python
- python实时分析日志的一个小脚本分享
- Python脚本收集腾讯云CDN日志,并入ELK日志分析
- python 日志分析统计脚本
- Python脚本收集腾讯云CDN日志,并入ELK日志分析
- Python脚本收集腾讯云CDN日志,并入ELK日志分析
- Python编写日志分析小脚本
- Python 脚本学习(三),日志分析脚本,文件差异对比,HTTP状态检测
- python脚本实现分析dns日志并对受访域名排行
- python分析apache访问日志脚本分享
- 一天,python搞个分析NGINX日志的脚本
- python 日志分析统计脚本
- python分析nignx访问日志脚本分享
- python分析日志脚本
- SSH登录日志分析脚本(Python)
- python自动化工具日志查询分析脚本代码实现
- python分析nignx访问日志脚本分享
- Python的一个增量读分析日志的脚本
- Python磁盘监控、日志分析监控脚本
- python自动化工具日志查询分析脚本代码实现