awk分析日志常用高级使用命令方法
分析访问日志(Nginx为例)
日志格式:
‘$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"‘
统计访问IP次数:
# awk ‘{a[$1]++}END{for(i in a)print v,a[i]}‘ access.log
统计访问访问大于100次的IP:
# awk ‘{a[$1]++}END{for(i in a){if(a[i]>100)print i,a[i]}}‘ access.log
统计访问IP次数并排序取前10:
# awk ‘{a[$1]++}END{for(i in a)print i,a[i]|"sort -k2 -nr |head -10"}‘ access.log
统计时间段访问最多的IP:
# awk‘$4>="[02/Jan/2017:00:02:00" && $4<="[02/Jan/2017:00:03:00"{a[$1]++}END{for(i in a)print i,a[i]}‘access.log
统计上一分钟访问量:
# date=$(date -d ‘-1 minute‘+%d/%d/%Y:%H:%M)
# awk -vdate=$date ‘$4~date{c++}END{printc}‘ access.log
统计访问最多的10个页面:
# awk ‘{a[$7]++}END{for(i in a)print i,a[i]|"sort -k1 -nr|head -n10"}‘ access.log
统计每个URL数量和返回内容总大小:
# awk ‘{a[$7]++;size[$7]+=$10}END{for(i in a)print a[i],i,size[i]}‘ access.log
统计每个IP访问状态码数量:
# awk ‘{a[$1" "$9]++}END{for(i in a)print i,a[i]}‘ access.log
统计访问IP是404状态次数:
# awk ‘{if($9~/404/)a[$1" "$9]++}END{for(i in a)print i,a[i]}‘ access.log
附: sort -k 的用法,-k 则是按照第几列进行排序输出,可按照数字进行选择
命令分析nginx访问日志的用法
原文地址:https://www.cnblogs.com/yangsirs/p/11286768.html
时间: 2024-10-21 15:11:34