983  
查询码:00000100
案例五:监控磁盘使用率脚本
作者: wyasw 于 2020年03月29日 发布在分类 / Linux / 常用shell脚本 下,并于 2020年03月29日 编辑
shell

案例五 : 监控磁盘使用率脚本

要求:

1.       每分钟检测一次磁盘。

2.       当磁盘空间使用超过90%,需要告警。

3.       统计使用率超过90%分区所有子目录的大小,并把排名前三的写到邮件内发送。

4.       第一次警告,没有及时处理则需要每30分钟告警一次。

5.       每分钟脚本执行时,需要检测脚本是否执行完,如果没有完成则本次不执行。

脚本:

#!/bin/bash

##监控磁盘使用情况,并做告警收敛(30分钟发一次邮件)

##作者:阿铭

##日期:2018-09-25

##版本:v0.1

#把脚本名字存入变量s_name

s_name=`echo $0|awk -F '/' '{print $NF}'`

#定义收件人邮箱

mail_user=admin@admin.com

#定义检查磁盘空间使用率函数

chk_sp()

{

    df -m|sed '1d' |awk -F '%| +' '$5>90 {print $7,$5}'>/tmp/chk_sp.log

    n=`wc -l /tmp/chk_sp.log|awk '{print $1}'`

    if [ $n -gt 0 ]

    then

        tag=1

        for d in `awk '{print $1}' /tmp/chk_sp.log`

        do

            find $d -type d |sed '1d'|xargs du -sm |sort -nr |head -3

        done > /tmp/most_sp.txt

    fi

}

#定义检查inode使用率函数

chk_in()

{

    df -i|sed '1d'|awk -F '%| +' '$5>90 {print $7,$5}'/tmp/chk_in.log

        n=`wc -l /tmp/chk_in.log|awk '{print $1}'`

        if [ $n -gt 0 ]

        then

                tag=2

        fi

}

#定义告警函数(这里的mail.py是案例二中那个脚本)

m_mail() {

    log=$1

    t_s=`date +%s`

    t_s2=`date -d "1 hours ago" +%s`

    if [ ! -f /tmp/$log ]

    then

        #创建$log文件

        touch /tmp/$log

        #增加a权限,只允许追加内容,不允许更改或删除

        chattr +a /tmp/$log

        #第一次告警,可以直接写入1小时以前的时间戳

        echo $t_s2 >> /tmp/$log

    fi

    #无论$log文件是否是刚刚创建,都需要查看最后一行的时间戳

    t_s2=`tail -1 /tmp/$log|awk '{print $1}'`

    #取出最后一行即上次告警的时间戳后,立即写入当前的时间戳

    echo >> $t_s/tmp/$log

    #取两次时间戳差值

    v=$[$t_s-$t_s2]

    #如果差值超过1800,立即发邮件

    if [ $v -gt 1800 ]

    then

        #发邮件,其中$2mail函数的第二个参数,这里为一个文件

        python mail.py $mail_user "磁盘使用率超过90%" "`cat $2`"  2/dev/null  

        #定义计数器临时文件,并写入0        

        echo "0" > /tmp/$log.count

    else

        #如果计数器临时文件不存在,需要创建并写入0

        if [ ! -f /tmp/$log.count ]

        then

            echo "0"  /tmp/$log.count

        fi

        nu=`cat /tmp/$log.count`

        #30分钟内每发生1次告警,计数器加1

        nu2=$[$nu+1]

        echo $nu2>/tmp/$log.count

        #当告警次数超过30次,需要再次发邮件

        if [ $nu2 -gt 30 ]

        then

             python mail.py $mail_user "磁盘使用率超过90%持续30分钟了" "`cat $2`" 2/dev/null 

             #第二次告警后,将计数器再次从0开始         

             echo "0" > /tmp/$log.count

        fi

    fi

}

#把进程情况存入临时文件,如果加管道求行数会有问题

ps aux |grep "$s_name" |grep -vE "$$|grep" >/tmp/ps.tmp

p_n=`wc -l /tmp/ps.tmp|awk '{print $1}'`

#当进程数大于0,则说明上次的脚本还未执行完

if [ $p_n -gt 0 ]

then

    exit

fi

chk_sp

chk_in

if [ $tag == 1 ]

then

    m_mail chk_sp /tmp/most_sp.txt

elif [ $tag == 2 ]

then

        m_mail chk_in /tmp/chk_in.log

fi

访问权限


 推荐知识

 历史版本

修改日期 修改人 备注
2020-03-29 21:07:38[当前版本] wyasw 创建版本

  目录
    文艺知识分享平台 -V 4.9.5 -wcp
    京公网安备100012199188号 京ICP备2021030911号