Shell script to handle Nginx site logs

Source: Internet
Author: User

In the Nginx site, access and error are using the default log format, with the log file named as follows:

Www.xxxx.com.access.log

Www.xxxx.com.error.log

The script processes the previous day's log records, re-places the logs in compressed files, and stores them by month, as follows:

201611/www.xxxx.com.access.20161102.log.zip

201611/www.xxxx.com.error.20161102.log.zip

The script is as follows:

#!/bin/bash# description: deal nginx access and error log dailyyes_day _a= ' date +%d/%b/%y -d -1day ' yes_day_a1= ' date +%d '/'%b ' \ '%y -d -1day ' yes_day_E= ' Date +%y/%m/%d -d -1day ' yes_day_e1= ' date +%y '/'%m ' \ '%d -d -1day ' yes_day= ' date  +%y%m%d -d -1day ' mon= ' date +%y%m ' file_list= ' find /etc/nginx/ -type f  -name  "*.conf"  -print0 | xargs -0 egrep  ' ^ (\s|\t) *access_log|error_log ' |awk   ' {print $3} ' |tr  '; "   " " |tr  ' \ n '   '   ' for file in  $file _listdo         site= ' echo  $file  |awk -f "log$"   ' {print $1} '          count=0        if [ -f   $file  ];THEN            &Nbsp;   echo  "Log file found: $file"                  path= ' dirname  $file '                  cd  $path  && find  ./ -name  "*[0-9].log.zip"  -mtime +30 -delete                 test ! -d  $mon  & & mkdir  $mon                                  if  [[  $file  =~ .*acc.* ]];then                         date_fmt= "\[$yes _day _a: "                         date_str= "\[$yes _day_a1:"                          echo -e  "\taccess_file: $file"                           #continue                  elif [[  $file  =~ .*err.* ]];then                          date_fmt= "^ $yes _day_e"                          date_str= "^ $yes _day_e1"                          echo -e  "\terror_file: $file"                           #continue                  fi                                  dst_file= "${site}${yes_day}.log"                  grep  "$date _fmt"   $file  >$ dst_file                [  $? -ne 0 ] && let count=count+1                 sleep 5                                  dfile_size= ' ls -l  $dst _file |awk  ' {print $5} '                  if [  $dfile _size  -gt 1024 ];then                         zip -m -r ${dst_file}.zip   $DST _file                         mv ${dst_file}.zip  $mon/                 elif [  $dfile _size  -le 10 ];then                         rm -f  $DST _file                 else                         mv  $DST _file $ mon/                fi                                [ $? -ne 0 ] & & let count=count+1                 sleep 5                                 if [  $count  -eq 0 ];then                         sed -i  "/$date _str/d"   $file                          sed -i  "/^$/d"   $file                  fi         Fidonenginx -t[ $? -eq 0 ] && nginx -s reload


Where a pit is encountered: the SED command deletes the file, the content that is about to be deleted is temporarily placed in the current directory in the file that begins with SED, the contents of the source file are deleted, and the temporary file is finally deleted. If an exception occurs in the middle (the temporary file causes insufficient disk space), the delete operation is terminated, the source file is not changed, and the temporary file is not processed automatically.

This article from the "11414123" blog, reproduced please contact the author!

Shell script to handle Nginx site logs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.