Linux监控磁盘超过阈值执行对应的命令

#!/bin/bash
    ########################                   
    #    date=2019/4/24    #          
    #     version=1.0      #     
    #  author=changzhenxi  #  
    ########################

#定义日志目录
LOG_DIR="/opt/log"

#判读日志目录是否存在,如果不存在,新建日志目录。
if [ ! -d $LOG_DIR ]; then
	mkdir -p $LOG_DIR
fi

#修改日志目录权限 
 chmod -R 750 $LOG_DIR

if [ ! -f $LOG_DIR/clear.log ]; then
    touch $LOG_DIR/clear.log
    chmod 750 $LOG_DIR/clear.log
fi

for d in `df -P | grep /dev/vda1 | awk '{print $5}' | sed 's/%//g'` ;
do
        if [ $d -gt 6 ]; then
                echo `date "+%C%y-%m-%d %H:%M:%S"` "/dev/vda1/ Disk usage exceeds 5%" |tee -a $LOG_DIR/clear.log
                echo `date "+%C%y-%m-%d %H:%M:%S"` "Start cleaning up disk space..." |tee -a $LOG_DIR/clear.log
                echo `date "+%C%y-%m-%d %H:%M:%S"` "rm -rf /opt/lib" |tee -a $LOG_DIR/clear.log
                rm -rf /opt/lib
        echo `date "+%C%y-%m-%d %H:%M:%S"` "Checking disk space usage..." |tee -a $LOG_DIR/clear.log
        for disk in `df -P | grep /dev/vda1 | awk '{print $5}' | sed 's/%//g'` ;
        do
                if [ $disk -lt 6 ]; then
                                echo `date "+%C%y-%m-%d %H:%M:%S"` "/dev/vda1/ Disk usage less than 6%" |tee -a $LOG_DIR/clear.log
                fi
        done
            echo `date "+%C%y-%m-%d %H:%M:%S"` "clear is over,/dev/vda1/ Disk usage less than 6%" |tee -a $LOG_DIR/clear.log
    else
            echo `date "+%C%y-%m-%d %H:%M:%S"` "/dev/vda1/ Disk usage less than 6%" |tee -a $LOG_DIR/clear.log
            exit 0;
        fi
done

你可能感兴趣的:(LIUNX)