#日志格式:
'$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "
$http_referer" "$http_user_agent" "$http_x_forwarded_for"'
#統(tǒng)計(jì)訪問(wèn) IP 次數(shù):
awk '{a[$1]++}END{for(v in a)print v,a[v]}' access.log
#統(tǒng)計(jì)訪問(wèn)訪問(wèn)大于 100 次的 IP:
awk '{a[$1]++}END{for(v in a){if(a[v]>100)print v,a[v]}}' access.log
#統(tǒng)計(jì)訪問(wèn) IP 次數(shù)并排序取前 10:
awk '{a[$1]++}END{for(v in a)print v,a[v] |"sort -k2 -nr |head -10"}' access.log
#統(tǒng)計(jì)時(shí)間段訪問(wèn)最多的 IP:
awk '$4>="[02/Jan/2017:00:02:00" && $4<="[02/Jan/2017:00:03:00"{a[$1]++}END{for(v in a)print v,a[v]}' access.log
#統(tǒng)計(jì)上一分鐘訪問(wèn)量:
date=$(date -d '-1 minute' +%d/%d/%Y:%H:%M)
awk -vdate=$date '$4~date{c++}END{print c}' access.log
#統(tǒng)計(jì)訪問(wèn)最多的 10 個(gè)頁(yè)面:
awk '{a[$7]++}END{for(v in a)print v,a[v] |"sort -k1 -nr|head -n 10"}' access.log
#統(tǒng)計(jì)每個(gè) URL 數(shù)量和返回內(nèi)容總大小:
awk '{a[$7]++;size[$7]+=$10}END{for(v in a)print a[v],v,size[v]}' access.log
#統(tǒng)計(jì)每個(gè) IP 訪問(wèn)狀態(tài)碼數(shù)量:
awk '{a[$1" "$9]++}END{for(v in a)print v,a[v]}' access.log
#統(tǒng)計(jì)訪問(wèn) IP 是 404 狀態(tài)次數(shù):
awk '{if($9~/404/)a[$1" "$9]++}END{for(i in a)print v,a[v]}' access.log
#統(tǒng)計(jì)訪問(wèn)URL統(tǒng)計(jì)PV:
awk '{print $7}' /usr/local/Nginx/logs/access.log|wc -l
#根據(jù)訪問(wèn)IP統(tǒng)計(jì)UV:
awk '{print $1}' /usr/local/nginx/logs/access.log|sort | uniq -c |wc -l
#查詢?cè)L問(wèn)最頻繁的IP
awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|head -n 50
#查詢?cè)L問(wèn)最頻繁的URL
awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|head -n 50
#nginx當(dāng)前并發(fā)數(shù)
netstat -ant | grep :80 | awk '{print $5}' | awk -F":" '{print $1}'| sort | uniq -c | sort -r -n | awk 'BEGIN{total=0}{total+=$1}END{print total}'
#查詢時(shí)間段內(nèi)的訪問(wèn)日志,統(tǒng)計(jì)出訪問(wèn)IP及其訪問(wèn)次數(shù)
tac access.log | grep "HTTP/1.1" 404" | awk -v st="15/Oct/2018:14:58:47" -v et="15/Oct/2018:14:59:47" '{t=substr($4,RSTART+2,21);if(t>st && t<=et) {print $0}}' | awk '{print $1}' | sort | uniq -c | sort -nr