利用awk做nginx的日志分析

nginx的日志用来记录网站的访问记录
nginx日志的存放目录在/var/log/nginx/
nginx的默认日志格式

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' ##定义日志格式
                   '$status $body_bytes_sent "$http_referer" '
                   '"$http_user_agent" "$http_x_forwarded_for"'

$remote_addr 显示用户访问源IP地址信息

$remote_user 显示认证的用户信息

[$time_local] 显示访问网站时间

$request 请求保温的请求行信息

$status 用户访问网站状态码信息

$body_bytes_sent 显示响应的数据尺寸信息

$http_referer 记录调用网站资源的链接地址信息(防止用户盗链)

$http_user_agent 记录用户使用什么客户端软件进行访问页面的 (谷歌 火狐 iphone)
$http_x_forwarded_for 当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置

统计某天的PV量

[root@web02 nginx]# grep -c '10/May/2020' www_access.all 
802

统计某个时间段的PV量

[root@web02 nginx]# awk '$4>="[10/May/2020:10:00:00" && $4<="[10/May/2020:19:00:00 "' www_access.all   | wc -l  ##统计2020年早上10到晚上19的PV量
368

统计2020年5月14日访问次数最多的5个ip(top5)

[root@czq nginx]# awk '/14\/May\/2020/{ips[$1]++} END{for(i in ips){print i,ips[i]}}' access.log | sort -k2rn | head -5  ##k2就是按第2列排序
115.87.98.91 4
139.205.177.98 3
196.42.38.245 2
80.82.70.187 2
101.251.242.238 1

统计2020年5月14日访问次数大于100的ip地址

[root@czq nginx]# awk '/14\/May\/2020/{ips[$1]++} END{for(i in ips){if (ips[i]>100){print i,ips[i] }}}' access.log 

统计2020年5月14日访问前10的url

[root@czq nginx]# awk '/14\/May\/2020/{url[$7]++} END{for(i in url){print i,url[i] }}' access.log  | sort -k2rn | head
/ 26
/operator/basic.shtml?id=1337 3
400 2
/cgi-bin/mainfunction.cgi 2
http://www.baidu.com/cache/global/img/gs.gif 2
7 1
/app/common.js?version=2.4.43&build=1563436823 1
/cloud-app/include/css/uncall.css 1
/ctrlt/DeviceUpgrade_1 1
G?\xC7)J\x08\xB5+^9\x16j" 1

统计每个UR访问内容总大小($body_bytes_sent)

[root@czq nginx]# awk '/14\/May\/2020/{size[$7]+=$10} END{for(i in size){print i,size[i] }}' access.log  
/index.php 555
/ctrlt/DeviceUpgrade_1 157
/cloud-app/include/css/uncall.css 555
/czq.html 322
/recordings/misc/recording_popup.php 555
7 0

统计每个IP访问状态码数量($status)

[root@czq nginx]# awk '/14\/May\/2020/{ip_code[$1" "$9]++} END{for(i in ip_code){print i,ip_code[i]}}' access.log | sort -k1rn | head
208.91.109.50 404 1
202.21.119.162 200 1
196.52.43.57 200 1
196.42.38.245 400 1
196.42.38.245 408 1
194.156.108.13 200 1
185.216.140.17 157 1
185.168.173.151 200 1
179.213.215.231 400 1
177.52.26.50 200 1

统计出现404状态码的ip

[root@czq nginx]# awk '/14\/May\/2020/{if($9=="404"){ip_code[$1" "$9]++}} END{for(i in ip_code){print i,ip_code[i]}}' access.log | sort -k3rn | head
117.141.205.10 404 3
139.205.177.98 404 3
115.87.98.91 404 2
80.82.70.187 404 2
114.35.90.88 404 1
118.24.105.14 404 1
173.242.182.42 404 1
208.91.109.50 404 1

统计前一分钟的pv量

[root@czq nginx]# date=`date -d '1 minute' +%d/%b/%Y:%H:%m` ##格式化前一分钟的date命令并赋值给date
[root@czq nginx]# awk -v a=$date '$0 ~ a {i++} END{print i}' access.log

统计某一时间段的pv量

[root@czq nginx]# awk '$4>="[14/May/2020:08:00:56" && $4<="[14/May/2020:16:07:00"{if($9=="404"){ip_code[$1" "$9]++}} END{for(i in ip_Code){print i,ip_code[i] }}' access.log

统计各种状态码出现数量

[root@czq nginx]# awk '/14\/May\/2020/{code[$9]++} END{for(i in code){print i,code[i]}}' access.log ##单纯打印数量
408 1
"-" 2
157 2
304 3
400 13
403 1
200 18
404 15
SP1 1
[root@czq nginx]# awk '/14\/May\/2020/{code[$9]++;total++} END{for(i in code){printf i"\t";printf code[i]"\t"; printf "%.2f%\n",code[i]/total*100}}' access.log ##打印数量和百分比
408 1   1.79%
"-" 2   3.57%
157 2   3.57%
304 3   5.36%
400 13  23.21%
403 1   1.79%
200 18  32.14%
404 15  26.79%
SP1 1   1.79%
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。