我的一个学生在运维工作中写的自动日志清理脚本程序

       本文是我的一个学生在运维工作中写的自动日志清理脚本程序,我这里不评价该shell脚本写的好与坏,只是发出来,和大家做一个分享,如果能给大家带来一点点思路上的参考就够了。

自动日志清理脚本程序

#!/bin/sh
##########################################################
#created by ydds      #
#date 2011-4-25       #
#function log clear default save 7 days logs   #
#when hard disk space achieve 80%, automatic save 3 days #
#when hard disk space achieve 90%,save only LOG today   #
#when only today log hard disk space achieve >95%  #
#   clear today log      #
##########################################################
set -x

oldboyWarnValue=80
oldboyCriticalValue=90
oldboyCC=95
nohupFlag=0

# $1 must be directory
if [ $# -ne 2 ]||[  -z $1 ]||[ ! -d $1 ];then
        echo "Uage:logClear /home/oldboy/apachelogs /dev/xvda1"
 exit 1
fi

#clear log path
logPath=$1

devstr=$(df|awk '/\//{print $1}'|sed -n '/\//p')
echo "${devstr}"|grep -q "$2"
[ $? -ne 0 ]&&exit 1

# hard disk name
oldboyName=$2

scriptLog=/server/script/clearLog.log

#clear nohuplog
clearNohupLog()
{
 if [ ${nohupFlag} -ne 1 ];then
   # check nulup.log >1G clear
         fileNohupLog="${logPath}/../nohup.log"
         NohupSize=$(du ${fileNohupLog}|awk '{print $1/1024/1024}'|awk -F "." '{print $1}')
  echo "check nohup.log size: $(du -h ${fileNohupLog}|awk '{print $1}')">>${scriptLog}
         if [ -f ${fileNohupLog} ];then
                 if [ ${NohupSize} -ge 1 ]||[ $1 -eq 1 ];then
                         echo "">${fileNohupLog}
    if [ $? -eq 0 ];then
     echo "clear nohup.log ok!">>${scriptLog}
    fi 
                 fi
         fi
 fi
}

#hard disk used
oldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')

echo "[$(date +%Y-%m-%d\ %H:%M:%S)]check hard disk used start....">>${scriptLog}
echo "check current hard disk space used ${oldboyUsed}%">>${scriptLog}

#clear nohup.log >warning
if [ ${oldboyUsed} -ge ${oldboyWarnValue} ];then
 echo "hard disk used ${oldboyUsed}% > ${oldboyWarnValue}%,start clear nohup.log..">>${scriptLog}
 clearNohupLog 1
 nohupFlag=1
fi

if [ ${nohupFlag} -eq 1 ];then
 #hard disk used
 echo "after deal nohup.log ,check hard disk again...">>${scriptLog}
 oldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')
fi

#save 7,3 days logs
clearnDayLogs()
{
 # cd tmp directory
 cd /tmp

 if [ $1 -ne 1 ];then
  if [ -d ${logPath} ];then
   cd ${logPath}
   find . -type f -mtime +$1 |xargs rm -f
   
   if [ $? -eq 0 ];then
                         echo " clear ${logPath} logs $1 ago days logs successfully!">>${scriptLog}
                 else
                         echo " clear ${logPath} logs $1 ago days logs fail!">>${scriptLog}
                 fi 
  fi
 else
  #clear nohup.log
         clearNohupLog 1

  clearFileNameArr=($(ls ${logPath}|awk -F "." 'NF==3{print $0}'))
  # clear file name is not blank
  if [ -n "${clearFileNameArr}" ];then
   echo "|">>${scriptLog}
   for((i=0;i<${#clearFileNameArr[@]};i++))
          do
    if [ ${i} -eq 0 ];then
     echo "clear file------------------start--------------------------->">>${scriptLog}
    fi
    
    # cd  this directory
                  cd ${logPath}
    if [ -f "${logPath}/${clearFileNameArr[$i]}" ];then
                   find . -type f -name ${clearFileNameArr[$i]} |xargs rm -f
    fi

    if [ $? -eq 0 ];then
     echo "${clearFileNameArr[$i]}">>${scriptLog}
    fi
          done
   echo "clear file------------------------- end -------------------->">>${scriptLog}
   echo "|">>${scriptLog}
  else
   echo "☆☆☆☆☆☆ Not files 1 day ago ☆☆☆☆☆☆">>${scriptLog}
  fi
  afterClearoldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')
  echo "clear ${logPath} logs ${1} ago days logs successfully!">>${scriptLog}
 fi
}

 

 如果你把握不好该脚本带来的误删除的风险,请慎用。


#when today log save ,check hard disk used >80%,so  clear today logs
clearTodayLog()
{
 oldboyUsedTmp=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')
 echo "clear today log start ...">>${scriptLog}
 #todayFileArr=($(ls ${logPath}))
 todayFileArr=($(find ${logPath} -type f|awk -F "/" '{print $NF}' ))
 echo "start save current logs tail 300 ..">>${scriptLog}
 echo "|">>${scriptLog}
 echo "dealing currnet logs name*********start*************>>>>">>${scriptLog}
 for((i=0;i<${#todayFileArr[@]};i++))
 do
   cd ${logPath}
    
   #get tail 300 --> tmp file
   tail -300 ${todayFileArr[$i]}>/tmp/tmpydds
  
   #check file exist again save tail 300 --- this file

   if [ -f "${logPath}/${todayFileArr[$i]}"  ];then
           find . -type f -name ${todayFileArr[$i]} -exec cat /tmp/tmpydds >${logPath}/${todayFileArr[$i]} \;
    if [ $? -eq 0 ];then
     echo " ${todayFileArr[$i]}----- ok!">>${scriptLog}
    else
    echo " ${todayFileArr[$i]}----- fail!">>${scriptLog}
    fi
   fi
 done 
 echo "dealing currnet logs name********* end *************>>>>">>${scriptLog}
 echo "|">>${scriptLog}
 if [ $? -eq 0 ];then
   echo "clear today log successfully!">>${scriptLog}
  
 else 
  echo "clear today log fail!">>${scriptLog}
 fi
}


wflag=0
cflag=0
flag=0
count=0


#clear nohup.log
#clearNohupLog 0

while true
do
        oldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')
 
 #not first and hard disk < warning value  break
        [ ${count} -ne 0 ]&&[ ${oldboyUsed} -lt ${oldboyWarnValue} ]&&break

 count=1
 
################next
        if [ ${oldboyUsed} -ge ${oldboyWarnValue} ];then
  case ${cflag} in
                        1)
                                clearTodayLog
                                cflag=2
                        ;;
                        2)
                                echo "baojing---------->">>${scriptLog}
                                break
                        ;;
                esac

                case ${wflag} in
                        1)
                                clearnDayLogs 1
                                wflag=2
                        ;;
                        2)
                                clearTodayLog
                                wflag=3
                        ;;
                        3)
    echo "baojing---------->">>${scriptLog}
                                break
                        ;;
                esac 
        fi

######################first
        if [ ${wflag} -eq 0 ]&&[ ${cflag} -eq 0 ];then

  #when >95% clear today
  if [ ${oldboyUsed} -ge ${oldboyCC} ];then

   #clear 1 ago log
   clearnDayLogs 1

   #clear today log
   clearTodayLog  
   break  

                #when hard disk is used 90% is Critical  today log only save
                elif [ ${oldboyUsed} -ge ${oldboyCriticalValue} ];then
                        echo "${oldboyName} current used ${oldboyUsed}%>= ${oldboyCriticalValue}% is Critical!">>${scriptLog}
                        cflag=1
                        clearnDayLogs 1

                #when hard disk is used ${oldboyWarnValue}% is Warning automatic save 3 days
                elif [ ${oldboyUsed} -ge ${oldboyWarnValue} ];then
                        echo "${oldboyName} current used ${oldboyUsed}%>= ${oldboyWarnValue}% is Warnning!">>${scriptLog}
                        wflag=1
                        clearnDayLogs 3

                #when hard disk is used lt 80% the logs oldboy is normal.
                else
                        echo "${oldboyName} current used ${oldboyUsed}% is Normal!">>${scriptLog}
                        clearnDayLogs 7
                        break
                fi
        fi

        flag=1
done

#when clear all log oldboy used
oldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')


#after deal log check disk used

if [ ${oldboyUsed} -ge ${oldboyCC} ];then
 echo "oldboy used ${oldboyUsed}% is Critical Critical!">>${scriptLog}

elif [ ${oldboyUsed} -ge ${oldboyCriticalValue} ];then
  echo "oldboy used ${oldboyUsed}% is Critical!">>${scriptLog}

elif [ ${oldboyUsed} -ge ${oldboyWarnValue} ];then
  echo "oldboy used ${oldboyUsed}% is Warning!">>${scriptLog}

else
 echo "oldboy used ${oldboyUsed}% is Normal!"
fi

echo "check hard disk used end!">>${scriptLog}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值