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

来源:互联网 发布:10的阶乘java编程 编辑:程序博客网 时间:2024/05/17 02:50

NGINX简单access日志查询分析

#coding = utf8#__author__ = 'yxcc''''use for nginx access log2016-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 timelogfile = 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 0