|
shell脚本-Nginx访问日志分析
1.原理
可以通过/usr/local/nginx/logs/access.log 文件-查看nginx的日志- [root@localhost scripts]# tail -f /usr/local/nginx/logs/access.log
- 192.168.70.1 - [17/Jul/2023:17:35:21 +0800] "GET / HTTP/1.1" 200 173833 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82" "-"
- 192.168.70.1 - [17/Jul/2023:20:53:37 +0800] "GET / HTTP/1.1" 200 173833 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82" "-"
复制代码 /usr/local/nginx/conf/nginx.conf 文件-定义了日志输出的格式
可以通过awk命令来取出所需要的数据- [root@localhost scripts]# awk '{print $0}' /usr/local/nginx/logs/access.log
- 192.168.70.1 - [17/Jul/2023:17:35:21 +0800] "GET / HTTP/1.1" 200 173833 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82" "-"
- 192.168.70.1 - [17/Jul/2023:20:53:37 +0800] "GET / HTTP/1.1" 200 173833 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82" "-"
- [root@localhost scripts]# awk '{print $1}' /usr/local/nginx/logs/access.log
- 192.168.70.1
- 192.168.70.1
复制代码 2.shell脚本
Nginx访问日志分析脚本- #!/bin/bash
- # 1.访问最多的IP
- # 2.根据时间段来访问最多的IP
- # 3.访问量超过2次的页面
- # 4.访问页面状态码数量
- LOG_FILE=$1
- echo "统计访问最多的10个IP"
- awk '{a[$1]++}END{print "UV:",length(a);for(v in a)print a[v] v}' $LOG_FILE | sort -k1 -nr | head -10
- echo "----------------------------------------"
- echo "统计一个时间段访问最多的10个IP"
- awk '$3>="[16/Jul/2023:17:35:21" && $3<="[17/Jul/2023:18:00:00"{a[$1]++}END{print "UV:",length(a);for(v in a)print a[v] v}' $LOG_FILE | sort -k1 -nr | head -10
- echo "----------------------------------------"
- echo "统计访问量超过2次的页面"
- awk '{a[$7]++}END{print "PV:",length(a);for(v in a){if(a[v]>2)print a[v],v}}' $LOG_FILE | sort -k1 -nr
- echo "----------------------------------------"
- echo "统计访问页面状态码数量"
- awk '{a[$7" "$8]++}END{for(v in a)print a[v],v}' $LOG_FILE | sort -k1 -nr
复制代码- [root@localhost scripts]# bash 13.sh /usr/local/nginx/logs/access.log
- 统计访问最多的10个IP
- 2192.168.70.1
- UV: 1
- ----------------------------------------
- 统计一个时间段访问最多的10个IP
- 1192.168.70.1
- UV: 1
- ----------------------------------------
- 统计访问量超过2次的页面
- PV: 1
- ----------------------------------------
- 统计访问页面状态码数量
- 2 HTTP/1.1" 200
复制代码
来源:https://www.cnblogs.com/xuxuxuxuxu/p/17561246.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|