admin

用python分析服务器访问日志
在服务器运维中,日志分析对于访客分析、错误检查无疑都相当重要,日志目录/var/log 进入后会发现一些比较常见的...
扫描右侧二维码阅读全文
30
2018/06

用python分析服务器访问日志

在服务器运维中,日志分析对于访客分析、错误检查无疑都相当重要,

日志目录/var/log 进入后会发现一些比较常见的如下载目录、httpd日志目录

进入httpd目录后以对access目录分析为例:

d =  {}  
with open('access_log','rb') as f:
        for line in f:
                key = line.split()[8]
                d.setdefault(key,0)
                d[key] += 1

        error_requests = 0 
        sum_requests = 0 
        try:
                for key, val in d.items():
                        if int(key) >= 400:
                                error_requests += val 
                        sum_requests += val 
        except:
                print("error")
        finally:
                print('The error rate is {0:.2f}%'.format(error_requests*100.0/sum_requests))

效果如下图:

Last modification:March 13th, 2019 at 07:06 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment