Nginx 的日志压缩处理

nginx 日志切割压缩脚本

#!/bin/bash
# date: 2015-03-18
#################################
# nginx log rotate

cdate=`date +%Y%m%d%H%M%S`
ctime=`date +"%F %X"`

start() {
    echo '==============' >> $1
    echo "start $ctime" >> $1
}

nginx_log_rotate() {
    log_path=$1
    ngx_log="${log_path}/access.log"
    mv $ngx_log $ngx_log-$cdate
    if [ $? -eq 0 ];then
        kill -USR1 `cat /letv/nginx/logs/nginx.pid ` 
        find ${log_path} -name access.log-* -ctime 3 -exec rm -f {} \;
        gzip $ngx_log-$cdate
    fi

}

#################################
#disk clear

disk_clear() {
    log='/tmp/disk_clear.tmp'
    start $log

    #define fs mount point var
    role_var[0]='/var/log/messages'
    role_var[1]='/var/log/secure'
    role_var[2]='/var/log/cron'
    role_var[3]='/var/log/maillog'
    role_var[4]='/var/log/yum.log'
    role_var[5]='/var/log/audit/audit.log'
    role_var[10]='/var/crash/'


    if [ 80 -lt `df -h /var |sed 1,2d|awk -F'[ %]' '{print $(NF-2)}'` ];then
        for i in ${role_var[@]};do
            for f in `find ${i}* -type f -size +200M`;do
                file_size=`du -sh $f`
                echo "file: $f   $file_size   this file too big.....execute clean " >> $log
                echo > $f
            done
        done
    fi

    #define fs mount point /tmp
    role_tmp[0]='/tmp/' 

    if [ 80 -lt `df -h /tmp |sed 1,2d|awk -F'[ %]' '{print $(NF-2)}'` ];then
        for i in ${role_tmp[@]};do
            for f in `find ${i} -name '*.tmp' -type f -size +100M`;do
                file_size=`du -sh $f`
                echo "file: $f   $file_size   this file too big.....execute clean " >> $log
                echo > $f
            done
        done
    fi
}

zabbix_zx_check() {
   ps aux|grep 'zabbix_agentd_zx'|grep -v 'grep'|awk '{print $2}'|xargs kill -9 &> /dev/null
   /etc/init.d/zabbix_agentd status || /etc/init.d/zabbix_agentd start
}


tomcat_log() {
        cdate=`date +"%Y-%m-%d"`
        catalina_log='catalina.*.log'
        host_manager_log='host-manager.*.log'
        localhost_log='localhost.*.log'
        manager_log='manager.*.log'
    localhost_access_log='localhost_access_log.*.txt'
        iptv='iptv.log.*'


        cd /letv/www/tomcat7/logs
        ls $catalina_log 2> /dev/null |grep -v $cdate | xargs gzip &> /dev/null
        ls $host_manager_log 2> /dev/null  |grep -v $cdate | xargs gzip &> /dev/null
        ls $localhost_log 2> /dev/null |grep -v $cdate | xargs gzip &> /dev/null
        ls $manager_log 2> /dev/null |grep -v $cdate | xargs gzip &> /dev/null
    ls $localhost_access_log 2> /dev/null | grep -v $cdate | xargs gzip &> /dev/null 

    if [ -d '/letv/logs/iptv' ];then
        cd /letv/logs/iptv
        ls $iptv|grep -v '.gz' | grep -v $cdate | xargs gzip &> /dev/null 
    fi

        letv_usage=`df -h /letv/|awk '{print $(NF-1)}'|awk -F\% 'END{print $1}'`
        if [ $letv_usage -gt 85 ];then
                find /letv/www/tomcat7/logs -name '*.gz' -mtime +2 -exec rm -f {} \;
                letv_usage=`df -h /letv/|awk '{print $(NF-1)}'|awk -F\% 'END{print $1}'`
                if [ $letv_usage -gt 85 ];then
                        for i in `find /letv/www/tomcat7/logs -size +500M`;do 
                                echo > $i
                        done
                fi
        fi
}

cibn_tomcat_log() {
        cdate=`date +"%Y-%m-%d"`
        catalina_log='catalina.*.log'
        host_manager_log='host-manager.*.log'
        localhost_log='localhost.*.log'
        manager_log='manager.*.log'
        localhost_access_log='localhost_access_log.*.txt'
    iptv='iptv.log.*'

        cd /cibn/www/tomcat7/logs
        ls $catalina_log 2> /dev/null |grep -v $cdate | xargs gzip &> /dev/null
        ls $host_manager_log 2> /dev/null  |grep -v $cdate | xargs gzip &> /dev/null
        ls $localhost_log 2> /dev/null |grep -v $cdate | xargs gzip &> /dev/null
        ls $manager_log 2> /dev/null |grep -v $cdate | xargs gzip &> /dev/null
        ls $localhost_access_log 2> /dev/null | grep -v $cdate | xargs gzip &> /dev/null

    if [ -d '/cibn/logs/iptv' ];then
        cd /cibn/logs/iptv
        ls $iptv|grep -v '.gz' | grep -v $cdate | xargs gzip &> /dev/null 
    fi

        letv_usage=`df -h /cibn/|awk '{print $(NF-1)}'|awk -F\% 'END{print $1}'`
        if [ $letv_usage -gt 85 ];then
                find /cibn/www/tomcat7/logs -name '*.gz' -mtime +2 -exec rm -f {} \;
                letv_usage=`df -h /cibn/|awk '{print $(NF-1)}'|awk -F\% 'END{print $1}'`
                if [ $letv_usage -gt 85 ];then
                        for i in `find /cibn/www/tomcat7/logs -size +500M`;do
                                echo > $i
                        done
                fi
        fi
}

$1 $2

将该脚本添加到定时即可
0 */1 * * * /usr/local/bin/check_init.sh nginx_log_rotate /letv/nginx/logs &> /dev/null

你可能感兴趣的:(软件)