NGINX日志分析

需要在NGINX的nginx.conf中设置

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" $request_time';

然后需要在站点配置中开启

access_log /www_$server_port.log main;

一定要指定main这个格式名,因为上面定义了,不指定会使用默认的则没有输出时间。

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != “0” ]; then
echo “Error: You must be root to run this script, please use root.”
exit 1
fi

NginxLogFile=$1
if [ “${NginxLogFile}” = “” ]; then
echo “Error: must input a log’s path.”
exit 1
fi

FileName=$(basename ${NginxLogFile}|sed ‘s/\.log//’)
OutPutFileName=”Analysis_”${FileName}.txt
OutPutCSVFileName=”Analysis_”${FileName}.csv

echo “Analysis Nginx’s log : “${NginxLogFile}

# 获取最初的数据
# IP
cat ${NginxLogFile} | \
awk ‘{print $1}’ \
> a1.txt

# TIME
cat ${NginxLogFile} | \
awk ‘{print $4″]”}’ \
> a2.txt

# URL
cat ${NginxLogFile} | \
awk ‘{print $7}’ \
> a3.txt

# REQ TIME
cat ${NginxLogFile} | \
awk ‘{print $(NF)}’ \
> a4.txt

# 合并文件
paste -d ‘ ‘ a1.txt a2.txt a3.txt a4.txt > ${OutPutFileName}

# 删除临时文件
rm -rf a1.txt a2.txt a3.txt a4.txt

echo “Analysis is Success, output file name is “$(pwd)/${OutPutFileName}

# IP 访问时间 URL 响应时间
cat ${OutPutFileName} | sort | \
awk ‘$NF ~/^[0-9.]+$/ {print $1″,””\””$3″\”””,”$4}’ \
> ${OutPutCSVFileName}

echo “OK…CSV is “${OutPutCSVFileName}

发表评论