pythonログ分析統計スクリプト

2777 ワード

http://www.apparitor.info/2012/03/19/python-%E6%97%A5%E5%BF%97%E5%88%86%E6%9E%90%E7%BB%9F%E8%AE%A1%E8%84%9A%E6%9C%AC/
スクリプトはshellのtail機能に似ており、毎分4 xx、5 xxのステータスコードの数、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