背景:避免双十一磁盘被打爆,本想通过crontab执行,但是删除需要密码,所以用作当机器磁盘高于摸个阈值,进行无关性日志强删
#!/bin/sh
#use
#sh clean.sh wmporder_prehost 38(说明:磁盘超过38%,则进行删除日志)
pgm -A `armory -leg $1` 'df -h' > alldf.log
cat alldf.log | grep -A2 "SUCCESS" > onedf.txt
pre=$2
index=0
ip=""
cat onedf.txt | while read line
do
#get one size
succ=`[[ $line =~ "SUCCESS" ]] && echo "1" || echo "0"`
if [ $succ -eq 1 ];then
index=1
ip=`echo $line | awk '{print $4}'`
else
(( index++ ))
fi
if [ $index -eq 3 ];then
echo $line | awk '{print $5}' | awk -v var=$pre -v ip=$ip -F '%' '{if($1>var) {print ip}}' >> overload.txt
fi
done
rm -rf alldf.log onedf.txt
echo `cat overload.txt`
if [ ! -f "overload.txt" ];then
echo "文件不存在"
else
pgm -A -b -p 10 -f overload.txt 'sudo -u admin rm -rf /home/admin/*/logs/*/metaq/*'
echo "delete"
fi
rm -rf overload.txt