nginx日志分析
Tutorial: 运维相关
Category: Shell
Published: 2026-04-07 13:58:25
Views: 23
Likes: 0
Comments: 0
nginx 日志分析
public() {
echo ""
read -p "请输入要分析的访问日志: " log_file
echo ""
if [ ! -f $log_file ]; then
echo "未找到: ${log_file}"
exit 1
fi
if [ ! -s $log_file ]; then
echo "${log_file}是空文件"
exit 1
fi
top_num=10
input_file=$(echo $log_file | awk -F '/' '{print $(NF)}')
analyze_dir=/tmp/nginx/nginx-$(date +%F).log
top_ip_file=$analyze_dir/ngx_log_top_ip_${input_file}.txt
top_src_url_file=$analyze_dir/ngx_log_top_src_url_${input_file}.txt
top_dest_url_file=$analyze_dir/ngx_log_top_dest_url_${input_file}.txt
top_code_file=$analyze_dir/ngx_log_top_code_${input_file}.txt
top_terminal_file=$analyze_dir/ngx_log_top_terminal_${input_file}.txt
mkdir -p $analyze_dir
start_time=$(head -1 $log_file | awk '{print $4}' | cut -d "[" -f2)
end_time=$(tail -1 $log_file | awk '{print $4}' | cut -d "[" -f2)
total_nums=$(wc -l $log_file | awk '{print $1}')
size=$(du -sh $log_file | awk '{print $1}')
echo "访问起始时间: $start_time ; 截止时间: $end_time"
echo "共访问 $total_nums 次 ; 日志大小: $size"
cat $log_file | awk '{print $1}' | sort | uniq -c | sort -rn | head -${top_num} >$top_ip_file
cat $log_file | awk '{print $11}' | sort | uniq -c | sort -rn | head -${top_num} >$top_src_url_file
cat $log_file | awk '{print $7}' | sort | uniq -c | sort -rn | head -${top_num} >$top_dest_url_file
cat $log_file | awk '{print $9}' | sort | uniq -c | sort -rn | head -${top_num} >$top_code_file
cat $log_file | awk '{print $13}' | sort | uniq -c | sort -rn | head -${top_num} >$top_terminal_file
}
simple() {
echo "+-+-+-+-+-+- 下面是分析内容 +-+-+-+-+-+-"
printf "最活跃的前${top_num}个访问IP: \n"
cat $top_ip_file
echo ""
printf "访问来源最多的前${top_num}个url: \n"
cat $top_src_url_file
echo ""
printf "请求最多的前${top_num}个url: \n"
cat $top_dest_url_file
echo ""
printf "返回最多的前${top_num}个状态码: \n"
cat $top_code_file
echo ""
printf ""
printf "返回最多的前${top_num}个终端号: \n"
cat $top_terminal_file
echo ""
printf ""
printf "返回最多的前${top_num}个IP所属城市(查询时间有点慢, 耐心等待!): \n"
echo ''
printf "%-15s %-15s %-30s\n" "访问次数" " IP地址" " 归属地"
echo '-----------------------------------------------'
a=0
cat $analyze_dir/ngx_log_top_ip_${input_file}.txt | while read line; do
ip=$(echo $line | cut -d ' ' -f2)
count=$(echo $line | cut -d ' ' -f1)
printf "%-10s %-15s %-30s\n" $count $ip $(curl -s "502 Bad Gateway(echo $line | cut -d ' ' -f2)" | awk -F '\"' {'print $2"--"$4"--"$6'})
echo '-----------------------------------------------'
let a=a+1
done
echo ""
printf ""
}
case $1 in
help)
echo ""
echo -e $"Usage: $0 enter a log file \n"
;;
*)
public
simple
;;
esac
exit 0