每个公司的NginxWeb服务器日志格式是不一样的,但是正则改一下就可以用,统计状态码及域名的发生次数,注意:注释部分为同一条url 同一个客户端ip及状态码访问过来的,生产环境这样的也不是很多,所以注释了,代码如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
import sys

def Return():
    print('\t-c 指定状态码 (2xx 3xx 4xx 5xx)')

class Match():
    def __init__(self,code):
        self.code = code
        self.List = []
    def Find(self):
        for i in open('/bbd/logs/nginx_user_access.log','r').readlines():
            try:
            #   Ip   = re.search("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}",i).group(0)
                #Url  = re.search('request(.*)protocol',i).group(1).split('\"')[2]
                Domain = 'http://' + re.search('domain:?(.*)request:?',i).group(1).split('\"')[2] #+ Url
                Code = re.search("http_status((.*)%s+(.*))body_bytes_sent" % self.code,i).group(1).split(':')[1][1:4]
                self.List.append((Domain,Code))
            except Exception as e:
                 pass
        return self.List

class Result():
    def __init__(self):
        self.data = Match(str(code)).Find()
        self.Filter = []
        self.Key = {}
    def GetLengh(self):
        for i in self.data:
            if i not in  self.Key.keys():
                self.Key[self.data.count(i)] =  i 
            else:
                pass
        return self.Key
    def GetResult(self):
        result = Result().GetLengh()
        result = sorted(result.items(), key=lambda result:result[0], reverse = True)
        result = result[0:11]
        for k,v in result:
            print('个数:%s 域名:%s ' % (k,v))


if __name__ == '__main__':
    try:
        if sys.argv[1] == '-c':
            if sys.argv[2] == '2xx':
                code = '20'
                value = Result()
                value.GetResult()
            elif sys.argv[2] == '3xx':
                code = '30'
                value = Result()
                value.GetResult()
            elif sys.argv[2] == '4xx':
                code = '4'
                value = Result()
                value.GetResult()
            elif sys.argv[2] == '5xx':
                code = '5'
                value = Result()
                value.GetResult()
        else:
            Return()
    except Exception as e:
        Return()
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐