我的一個學生在運維工作中寫的自動日誌清理腳本程序

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

本文是我的一個學生在運維工作中寫的自動日誌清理腳本程序,我這裡不評價該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}

本文出自 「老男孩的linux博客」 博客,請務必保留此出處http://oldboy.blog.51cto.com/2561410/557667





[火星人 ] 我的一個學生在運維工作中寫的自動日誌清理腳本程序已經有296次圍觀

http://coctec.com/docs/linux/show-post-48784.html