linux Nginx 日誌指令碼_linux shell

來源:互聯網
上載者:User

任務計劃
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.