sh 用于解析Nginx访问日志的Bash脚本:https://sysadmins.co.za/bash-script-to-parse-and-analyze-nginx-access-logs/
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 用于解析Nginx访问日志的Bash脚本:https://sysadmins.co.za/bash-script-to-parse-and-analyze-nginx-access-logs/相关的知识,希望对你有一定的参考价值。
#!/bin/bash
# variables
LOGFILE="/var/log/nginx/access.log"
LOGFILE_GZ="/var/log/nginx/access.log.*"
RESPONSE_CODE="200"
# functions
filters(){
grep $RESPONSE_CODE \
| grep -v "\/rss\/" \
| grep -v robots.txt \
| grep -v "\.css" \
| grep -v "\.jss*" \
| grep -v "\.png" \
| grep -v "\.ico"
}
filters_404(){
grep "404"
}
request_ips(){
awk '{print $1}'
}
request_method(){
awk '{print $6}' \
| cut -d'"' -f2
}
request_pages(){
awk '{print $7}'
}
wordcount(){
sort \
| uniq -c
}
sort_desc(){
sort -rn
}
return_kv(){
awk '{print $1, $2}'
}
request_pages(){
awk '{print $7}'
}
return_top_ten(){
head -10
}
## actions
get_request_ips(){
echo ""
echo "Top 10 Request IP's:"
echo "===================="
cat $LOGFILE \
| filters \
| request_ips \
| wordcount \
| sort_desc \
| return_kv \
| return_top_ten
echo ""
}
get_request_methods(){
echo "Top Request Methods:"
echo "===================="
cat $LOGFILE \
| filters \
| request_method \
| wordcount \
| return_kv
echo ""
}
get_request_pages_404(){
echo "Top 10: 404 Page Responses:"
echo "==========================="
zgrep '-' $LOGFILE $LOGFILE_GZ\
| filters_404 \
| request_pages \
| wordcount \
| sort_desc \
| return_kv \
| return_top_ten
echo ""
}
get_request_pages(){
echo "Top 10 Request Pages:"
echo "====================="
cat $LOGFILE \
| filters \
| request_pages \
| wordcount \
| sort_desc \
| return_kv \
| return_top_ten
echo ""
}
get_request_pages_all(){
echo "Top 10 Request Pages from All Logs:"
echo "==================================="
zgrep '-' --no-filename $LOGFILE $LOGFILE_GZ \
| filters \
| request_pages \
| wordcount \
| sort_desc \
| return_kv \
| return_top_ten
echo ""
}
# executing
get_request_ips
get_request_methods
get_request_pages
get_request_pages_all
get_request_pages_404
以上是关于sh 用于解析Nginx访问日志的Bash脚本:https://sysadmins.co.za/bash-script-to-parse-and-analyze-nginx-access-logs/的主要内容,如果未能解决你的问题,请参考以下文章
nginx 日志切割
nginx访问日志,日志切割,静态文件不记录日志和过期时间
Nginx 脚本自动进行日志切割
sh 用于安装没有root访问权限的tmux的bash脚本
sh 用于安装没有root访问权限的tmux的bash脚本
sh 用于安装没有root访问权限的tmux的bash脚本