zoukankan      html  css  js  c++  java
  • 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 "33[33mzookeeper snapshot is bigger!Begin clean it which delete with number!33[0m"
        cd $dir
        CalSnapshotNum
        DiskUseRate
        if [ $SPACE -ge $threshold ];then
            delete_num=$(expr $total_num - $retain_num)
            echo -e "Delete number:33[31m33[01m33[05m$delete_num33[0m.Retain number is 33[31m33[01m33[05m$retain_num33[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:33[31m33[01m33[05m$delete_num33[0m.Retain number is 33[31m33[01m33[05m$least_retain_num33[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

      

  • 相关阅读:
    如何配置任意目录下Web应用程序
    Eclipse配置class文件输出目录
    window下搭建Python3.7+selenium3.1.1+pycharm环境
    如何把本地项目上传到GitHub
    Git 如何把master的内容更新到分支
    更新远程代码到本地库
    如何解决本地仓库和远程仓库的冲突(Conflict)
    如何使用Java创建Excel(.xls 和 .xlsx)文件 并写入数据
    Configure Tomcat 7 to run Python CGI scripts in windows(Win7系统配置tomcat服务器,使用python进行cgi编程)
    2019年春季学期第三周作业
  • 原文地址:https://www.cnblogs.com/itcomputer/p/10074805.html
Copyright © 2011-2022 走看看