**NGINX简单access日志查询分析**

NGINX简单access日志查询分析

#coding = utf8
#__author__ = 'yxcc'
'''
use for nginx access log
2016-07-27
'''
'''
log format:
$remote_addr;; $time_local;; $request;; $status;;
$body_bytes_sent;; bytes_sent;; $connection;; $connection_requests;;
$http_referer;; $http_user_agent;; $request_length;; $request_time;;
'''
#import time
logfile = raw_input('access log name:')

def handles(col):
    views = {}
    f = open(logfile)
    for line in f:
        try:
            cols = line.strip().split(';;')[col]
        except:
            pass
        if cols in views.keys():
            views[cols] += 1
        else:
            views[cols] = 1
    f.close()
    return views, len(views), sum(views.values())


while True:
    print('you can chose ip explor or requests')
    chosefile = raw_input(r'what your choise:')
    if chosefile =='ip':
        colnu=0
    elif chosefile =='explor':
        colnu=-3
    elif chosefile =='requests':
        colnu=2
    elif chosefile =='x':
        break
    else:
        print('wrong input')
    #print(time.asctime())
    dicts, len_v, sum_v = handles(colnu)
    #print(time.asctime())
    print('There are \033[36m%d\033[0m different \033[36m%s\033[0m and all count are \033[36m%d\033[0m' % (len_v, chosefile, sum_v))
    lines = input('how many lines do u want to list:')
    s = sorted(dicts.items(), key = lambda a:a[1], reverse = True)
    for b in range(int(lines)):
                    print('\033[33m%s\033[0m\t\t\033[32m%s\033[0m' % (str(s[b][1]), str(s[b][0])))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值