温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python日志分析统计脚本是什么

发布时间:2021-10-14 15:02:29 来源:亿速云 阅读:112 作者:柒染 栏目:编程语言

这期内容当中小编将会给大家带来有关python日志分析统计脚本是什么,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

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

python日志分析统计脚本是什么

#!/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

上述就是小编为大家分享的python日志分析统计脚本是什么了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI