日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/20 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 我的一个学生在运维工作中写的自动日志清理脚本程序 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?????? 本文是我的一個學(xué)生在運維工作中寫的自動日志清理腳本程序,我這里不評價該shell腳本寫的好與壞,只是發(fā)出來,和大家做一個分享,如果能給大家?guī)硪稽c點思路上的參考就夠了。

自動日志清理腳本程序

#!/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
}

?

?如果你把握不好該腳本帶來的誤刪除的風(fēng)險,請慎用。


#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}
?

總結(jié)

以上是生活随笔為你收集整理的我的一个学生在运维工作中写的自动日志清理脚本程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。