python 日志分析统计脚本

释放双眼,带上耳机,听听看~!

python 日志分析统计脚本

脚本类似shell的tail功能,用来统计每分钟的4xx、5xx的状态码数量,统计php执行时间小于1秒、1-5秒、5秒以上的数量,和每秒的并发请求。将结果放到/tmp目录下, 也可以用cacti将结果画图。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
1#!/usr/bin/env python
2import time, os
3
4#-----------------------------
5log_file = '/var/log/httpd/cmi_access_log'
6#-----------------------------
7
8log_name = log_file.split('/')[-1]
9file = open(log_file, 'r')
10lt1,to1_5,gt5,status4,status5,concurrent = 0,0,0,0,0,0
11Time2 = '0'
12st_results = os.stat(log_file)
13st_size = st_results[6]
14file.seek(st_size)
15size = os.path.getsize(log_file)
16
17while 1:
18        Time = time.strftime("%Y_%m_%d",time.localtime(time.time() - 60))
19        where = file.tell()
20        line = file.readline()
21        L = line.split('"')
22        Time1 = time.strftime("%Y_%m_%d_%H:%M",time.localtime(time.time() - 60))
23        if not line:
24            size = os.path.getsize(log_file)
25            if size < where:
26                file = open(log_file, 'r')
27            else:
28                time.sleep(1)
29                file.seek(where)
30        else:
31            try:
32                Phptime = float(L[-2])
33                url = L[1]
34                status = L[2][1:4]
35                if '4' in status[0]:
36                    status4 += 1
37                elif '5' in status[0]:
38                    status5 += 1
39                if Time1 in Time2:
40                    if 'f5.php' not in url:
41                        concurrent += 1
42                        if Phptime < 1:
43                                lt1 += 1
44                        elif 1 <= Phptime <= 5:
45                                to1_5 += 1
46                        else:
47                                gt5 += 1
48                else:
49                    concurrent = concurrent / 60
50                    out = Time1 + "," + str(lt1) + "," + str(to1_5) + "," + str(gt5) + "," + str(status4) + "," + str(status5) + "," + str(concurrent)
51                    F = open('/tmp/' + log_name + '_' + Time + '.data', 'a')
52                    print >> F, out
53                    F.close()
54                    Time2 = Time1
55                    lt1,to1_5,gt5,status4,status5,concurrent = 0,0,0,0,0,0
56            except:
57                pass
58

原创文章,转载请注明: 转载自gjw_apparitor 博客

本文链接地址: python 日志分析统计脚本

分享文章 打印文章 这篇文章由gjw_apparitor于2012 年 03 月 19 日 上午 3:17发表在Code。你可以订阅RSS 2.0 也可以发表评论或引用到你的网站。

1
1
  • 评论(1)

python 日志分析统计脚本

#1 guorui
大约4月前

写的不详细。。。。。 rrdtool生成文件,和写rddtool文件,取数据 画图的没写。.. 批评一下。:)

给TA打赏
共{{data.count}}人
人已打赏
安全技术

c++ vector

2022-1-11 12:36:11

安全经验

WordPress 5.0.1 安全更新版本发布,建议升级

2018-12-15 11:12:22

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索