分析nginx日誌指令碼之python

2022-09-04 04:36:10 字數 1291 閱讀 4981

為了對每個月的切割過的30個日誌檔案統計出訪問最多的ip位址進行排序,整理了下面的指令碼,主要思路是處理每乙個日誌檔案的ip排序,最後進行字典合併,計算出月ip排序。
#

!/usr/bin/env python

import

osimport

sysimport

commands

import

reipsort ={}

defipsort(logfile):

ipadd = r'

\.'.join([r'

\d']*4)

re_ip =re.compile(ipadd)

for line in

open(logfile):

match =re_ip.match(line)

ifmatch:

ip =match.group( )

ipsort[ip] = ipsort.get(ip, 0) + 1

#return ipsort

#logfile=ipsort

#print logfile,"\n",ipsort,"\n"

#ipnum[logfile] = ipsort

defreadfile():

filedir = raw_input("

enter the path>

").strip()

filelist =os.listdir(filedir)

filenum =0

for line in

filelist:

if line[-4:] == '

.txt':

ipsort(line)

#s,r = commands.getstatusoutput("cat %s/%s |awk wc -l" %(filedir,line))

def mergeipnum(*ipns):

_keys = set(sum([ipn.keys() for ipn in

ipns],))

#print _keys,

_ipnum_dic ={}

for _key in

_keys:

#pass

#print _key

_ipnum_dic[_key] = sum([ipn.get(_key,0) for ipn in

ipns])

print

_ipnum_dic,

readfile()

mergeipnum(ipsort)

nginx日誌分析指令碼

訪問最多的ip 訪問最多的頁面 cc攻擊 訪問頁面狀態碼數量 根據時間段來訪問最多的ip 統計某列ip數量 awk end usr local nginx logs access.log bin bash log file 1echo 統計訪問最多的10個ip awk end log file so...

分析 Nginx 日誌

日誌格式 統計訪問 ip 次數 awk end access.log 統計訪問訪問大於 100 次的 ip awk end access.log 統計訪問 ip 次數並排序取前 10 awk end access.log 統計時間段訪問最多的 ip awk 4 02 jan 2017 00 02 0...

NGINX日誌分析

stub status模組主要用於檢視nginx的一些狀態資訊,例如統計nginx的訪問量,首先我們得檢視該模組有沒有安裝,如果沒有安裝,得先安裝,安裝好後,修改nginx的配置檔案,開啟該模組,然後就可以使用以下命令來進行統計,如 1.根據訪問ip統計uv awk access.log sort ...