031_按照阈值、文件数量自动磁盘清理
背景:线上zk都部分机器老是磁盘慢,故写了个监控磁盘阈值进行自动清理的脚本,本脚本可以调节磁盘上限和保留文件数量.
#!/bin/bash
#monitor available disk space
dir='/data/zookeeper/data/version-2/'
least_retain_date=20
threshold=85
retain_num=500
least_retain_num=400
function cur_date(){
cur_date=$(date "+%Y-%m-%d %H:%M:%S")
}
function DiskUseRate(){
SPACE=$(df -P| sed -n '/\/data$/p' | gawk '{print $5}' | sed 's/%//')
}
function CalDiffDay(){
cd $dir
oldest_file=$(ls -alt snapshot.*| tail -1| gawk '{print $9}')
oldest_date=$(stat $oldest_file| grep ^Modify| gawk '{print$2}'| sed 's/-//g')
cur_time=$(date +%s)
oldest_time=$(date -d ${oldest_date} +%s)
diff_time=$(($cur_time - $oldest_time))
diff_date=$(($diff_time / 86400))
}
function CalSnapshotNum(){
cd $dir
total_num=$(ls -lt|egrep -i "snapshot"|wc -l)
}
function DeleteNum(){
echo -e "\033[33mzookeeper snapshot is bigger!Begin clean it which delete with number!\033[0m"
cd $dir
CalSnapshotNum
DiskUseRate
if [ $SPACE -ge $threshold ];then
delete_num=$(expr $total_num - $retain_num)
echo -e "Delete number:\033[31m\033[01m\033[05m$delete_num\033[0m.Retain number is \033[31m\033[01m\033[05m$retain_num\033[0m."
for each_snapshot in `ls -lt|egrep -i "snapshot"|tail -n $delete_num|awk '{print $NF}'`;do
sudo rm -rf $each_snapshot
done
fi
DiskUseRate
if [ $SPACE -ge $threshold ];then
CalSnapshotNum
delete_num=$(expr $total_num - $least_retain_num)
echo -e "Delete number:\033[31m\033[01m\033[05m$delete_num\033[0m.Retain number is \033[31m\033[01m\033[05m$least_retain_num\033[0m."
for each_snapshot in `ls -lt|egrep -i "snapshot"|tail -n $delete_num|awk '{print $NF}'`;do
sudo rm -rf $each_snapshot
done
fi
}
cur_date_d=`cur_date`
echo "==========$cur_date_d start=========="
DiskUseRate
if [ $SPACE -ge $threshold ];then
CalDiffDay
if [ $diff_date -ge $least_retain_date ];then
remain_date=$least_retain_date
fi
cmd_num=`sudo find $dirsnapshot* -mtime +$remain_date|wc -l`
CalSnapshotNum
delete_least_num=$(expr $total_num - $least_retain_num)
if [ $cmd_num -ge $delete_least_num ];then
cmd="sudo find $dirsnapshot* -mtime +$remain_date -exec rm -rvf {} \;"
echo "Space is use more than $threshold%! Rmove some files!"
echo $cmd
echo "---detail---"
eval $cmd
fi
DiskUseRate
if [ $SPACE -ge $threshold ];then
DeleteNum
fi
else
echo "$SPACE% used!"
fi
echo "=======end======="
#disk clean
*/30 * * * * /opt/ops/disk_clean.sh >> disk_auto_clean.log

浙公网安备 33010602011771号