欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

linux Nginx 日志脚本

程序员文章站 2023-11-27 20:49:04
任务计划 crontab -l 1 15 * * * /home/dongnan/sh/split.sh >> /home/dongnan/sh/cron.lo...

任务计划
crontab -l
1 15 * * * /home/dongnan/sh/split.sh >> /home/dongnan/sh/cron.log 2>&

nginx 日志
ls /var/log/nginx/
20130730-access.log.gz  20130801-access.log.gz  20130803-access.log.gz
20130730-error.log.gz   20130801-error.log.gz   20130803-error.log.gz
20130731-access.log.gz  20130802-access.log.gz access.log
20130731-error.log.gz   20130802-error.log.gz   error.log

shell 脚本
cat split.sh

复制代码 代码如下:

#!/bin/bash
#script_name:nginx_log.sh
#description:nginx-log deleted/rotate/compress
#last_update:20130725 by zongming

#nginx
#signal action
#term, int terminate the server immediately
#quit stop the server
#hup configuration changes, start new workers, graceful stop of old workers
#usr1 reopen log files
#usr2 upgrade the server executable
#winch graceful stop (parent process advise the children to exit)

  
#variables
log_dir=/var/log/nginx/
log_date=$(date +"%y%m%d")
nginx_pid=/var/run/nginx.pid
keep_days=30

#old_log
find "$log_dir" -name "*\.log.gz" -type f -mtime +"${keep_days}" -exec rm -rf {} \;

#rename_log
for log_name in `ls "$log_dir" | awk '/.log$/'`;do
    if [ -e "${log_dir}${log_date}-${log_name}" ];then
        echo "${log_dir}${log_date}-${log_name} already exists" && continue
    else
        /bin/mv "${log_dir}${log_name}" "${log_dir}${log_date}-${log_name}"
        /bin/gzip "${log_dir}${log_date}-${log_name}"
    fi
done

#new_log
/bin/kill -usr1 $(cat $nginx_pid) && /bin/sleep 1

nginx日志切割脚本:

vi /root/cutlog.sh

复制代码 代码如下:

#!/bin/bash
i=`ps aux | grep nginx | grep root | grep -v 'grep nginx' | awk '{print $14}'`    #查找nginx进程
if [ $i == /usr/local/nginx/sbin/nginx ];then
acclog=`cat /usr/local/nginx/conf/nginx.conf | grep  ' access_log' | awk '{print $2}'`  #如果nginx进程在,就找到配置文件,读取accesslog路径
errlog=`cat /usr/local/nginx/conf/nginx.conf| grep  ^error  | awk '{print $2}'| cut  -d";" -f1`  #错误日志的路径
ls $acclog     #查看是否有此文件
if [ $? -eq 0 ];then    #如果有
mv $acclog  $acclog.`date -d "-1 day" +%f`  #重命名当前日志
mv $errlog $errlog.`date -d "-1 day" +%f`
touch $acclog    #创建空日志
touch $errlog
chown nginx:root  $acclog   #修改属主
chown nginx:root  $errlog
[ -f /usr/local/nginx/logs/nginx.pid ] && kill -usr1 `cat /usr/local/nginx/logs/nginx.pid`     #判断进程,并重新加载(这里的kill -usr1会使nginx将新产生的日志写到刚创建的新日志里面。)
/mnt/logs/checklog.sh $acclog.`date "-1 day" +%f` #这个是日志分析脚本
gzip $acclog.`date -d "-1 day" +%f`  #压缩日志
gzip $errlog.`date -d "-1 day" +%f`

mv  $acclog.`date -d "-10 day" +%f`.*  /mnt/history.nginx.log/   #将10天前的老日志清理到其他地方,(你们如果想删除的可以自己改成删除)
mv  $errlog.`date -d "-10 day" +%f`.*  /mnt/history.nginx.log/
fi
fi

nginx日志分析脚本:

vi /mnt/logs/checklog.sh

复制代码 代码如下:

#!/bin/bash
echo -e  "####################`date +%f`" >> /mnt/logs/400.txt
echo -e  "####################`date +%f`" >> /mnt/logs/url.txt
echo -e  "####################`date +%f`" >> /mnt/logs/ip.txt
cat $1 | wc -l >> /mnt/logs/ip.txt   #分析ip
cat  $1  | awk -f'"'  '{print $3}' | awk '{print $1}' | sort | uniq -c| sort -rn >  /mnt/logs/code.txt   #分析返回值
cat $1 |  awk   '{print $1}' |  sort | uniq -c| sort -rn | head -n20  >> /mnt/logs/ip.txt 
n=`cat /mnt/logs/code.txt | wc -l`
for i in $(seq 1 $n)
do
m=`head -n$i /mnt/logs/code.txt | tail -n1 | awk '{print $2}'`
if [ $m -ge 400 ]
then

echo "#####find $m###############">>/mnt/logs/400.txt   #分析错误请求
cat $1 | grep "\" $m "  | grep -v ' "-" "-" - ' | sort | awk '{print $1 $2 $3 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 $16 $17 $18 $19 $20 $21}' | sort | uniq -c  | sort -rn  | head -n5 >> /mnt/logs/400.txt
fi
done
cat  $1 | grep -v ' "-" "-" - ' | awk -f't' '{print $2}' | awk -f'?' '{print $1}' | sort |awk '{print $1}' | sed  's/\(\/review\/file\/download\/\).*/\1/g'   | sort | uniq -c | sort -rn | head -n20 >> /mnt/logs/url.txt