python 日志分析统计脚本
2012-10-19 15:58
711 查看
python 日志分析统计脚本
脚本类似shell的tail功能,用来统计每分钟的4xx、5xx的状态码数量,统计php执行时间小于1秒、1-5秒、5秒以上的数量,和每秒的并发请求。将结果放到/tmp目录下, 也可以用cacti将结果画图。#!/usr/bin/env python import time, os #----------------------------- log_file = '/var/log/httpd/cmi_access_log' #----------------------------- log_name = log_file.split('/')[-1] file = open(log_file, 'r') lt1,to1_5,gt5,status4,status5,concurrent = 0,0,0,0,0,0 Time2 = '0' st_results = os.stat(log_file) st_size = st_results[6] file.seek(st_size) size = os.path.getsize(log_file) while 1: Time = time.strftime("%Y_%m_%d",time.localtime(time.time() - 60)) where = file.tell() line = file.readline() L = line.split('"') Time1 = time.strftime("%Y_%m_%d_%H:%M",time.localtime(time.time() - 60)) if not line: size = os.path.getsize(log_file) if size < where: file = open(log_file, 'r') else: time.sleep(1) file.seek(where) else: try: Phptime = float(L[-2]) url = L[1] status = L[2][1:4] if '4' in status[0]: status4 += 1 elif '5' in status[0]: status5 += 1 if Time1 in Time2: if 'f5.php' not in url: concurrent += 1 if Phptime < 1: lt1 += 1 elif 1 <= Phptime <= 5: to1_5 += 1 else: gt5 += 1 else: concurrent = concurrent / 60 out = Time1 + "," + str(lt1) + "," + str(to1_5) + "," + str(gt5) + "," + str(status4) + "," + str(status5) + "," + str(concurrent) F = open('/tmp/' + log_name + '_' + Time + '.data', 'a') print >> F, out F.close() Time2 = Time1 lt1,to1_5,gt5,status4,status5,concurrent = 0,0,0,0,0,0 except: pass
原创文章,转载请注明: 转载自gjw_apparitor 博客
本文链接地址:
python 日志分析统计脚本
分享文章 | 打印文章 | 这篇文章由gjw_apparitor于2012 年 03 月 19 日 上午 3:17发表在Code。你可以订阅RSS 2.0 也可以发表评论或引用到你的网站。 |
#1
guorui
大约4月前
写的不详细。。。。。 rrdtool生成文件,和写rddtool文件,取数据 画图的没写。.. 批评一下。:)
相关文章推荐
- python 日志分析统计脚本
- Python脚本收集腾讯云CDN日志,并入ELK日志分析
- Python编写日志分析小脚本
- 【脚本语言系列】关于Python统计分析statsmodel,你需要知道的事
- python处理nginx日志,并统计分析---我这个写的处理时间效率不高,有好方法,请大家指正
- python脚本实现分析dns日志并对受访域名排行
- 使用python 分析统计nginx访问日志ip次数并且排序
- hadoop MR 统计分析日志脚本一例
- php日志分析小脚本,分析出每行包含相同字符串的统计数量(从大到小进行排序)
- python自动化工具日志查询分析脚本代码实现
- Python磁盘监控、日志分析监控脚本
- Python的一个增量读分析日志的脚本
- python 脚本统计squid日志中的IP访问数和URL访问数量
- 日志分析关键字检测微信报警python脚本
- Python脚本收集腾讯云CDN日志,并入ELK日志分析
- shell脚本-统计分析日志
- Python实现一个Git日志统计分析的小工具
- 大数据日志分析系统-python脚本利用es聚合计算
- python分析nignx访问日志脚本分享
- python分析apache访问日志脚本分享