centos下磁盘监控告警并发出邮件

周末发现一台应用服务器应用服务器磁盘满了,之前由于太忙还来不及做,现在要做一个监控告警并发出邮件。

1、直接到/opt下vi disk_check.sh编辑文件

#!/bin/bash
partition_list=(`df -h | awk 'NF>3&&NR>1{sub(/%/,"",$(NF-1));print $NF,$(NF-1)}'`)
critical=$1
echo $1
echo $2
product=$2
dtimes=`date +%Y%m%d%H%M`
notification_email()
{
    emailuser='[email protected]'
    emailpasswd='passwd'
    emailsmtp='smtp.163.com'
    sendto='[email protected]'
    title=$product$dtimes' Disk Space Alarm'
    /opt/sendEmail-v1.56/sendEmail -f $emailuser -t $sendto -s $emailsmtp -u $title -xu $emailuser -xp $emailpasswd
}
crit_info=""
for (( i=0;i<${#partition_list[@]};i+=2 ))
do
    if [ "${partition_list[((i+1))]}" -lt "$critical" ];then
        echo "OK! ${partition_list[i]} used ${partition_list[((i+1))]}%"
    else
            if [ "${partition_list[((i+1))]}" -gt "$critical" ];then
                crit_info=$crit_info"Warning! ${partition_list[i]} used ${partition_list[((i+1))]}%\n"
            fi
    fi
done
if [ "$crit_info" != "" ];then
    ips=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
    echo -e $ips" "$crit_info | notification_email
fi

Esc :wq保存退出

2、给disk_check.sh添加可执行权限

#chmod +x disk_check.sh

3、下载sendEmail邮件程序命令(点击下载),上传到服务器的/opt目录下

解压文件

#tar -zxvf /opt/sendEmail-v1.56.tar.gz

4、测试脚本报警邮件是否运行正常

#sh disk_check.sh  20 test

[root@chtpub opt]# sh disk_check.sh 20 test
20
test
OK! / used 6%
OK! /dev used 0%
OK! /dev/shm used 1%
OK! /run used 1%
OK! /sys/fs/cgroup used 0%
OK! /usr used 8%
OK! /var used 6%
OK! /opt used 8%
OK! /tmp used 1%
OK! /home used 1%
OK! /boot/efi used 5%
OK! /data used 1%
OK! /run/user/0 used 0%
OK! /run/user/42 used 1%
Reading message body from STDIN because the '-m' option was not used.
If you are manually typing in a message:
  - First line must be received within 60 seconds.
  - End manual input with a CTRL-D on its own line.
Jan 25 16:55:32 chtpub sendEmail[30496]: Message input complete.
Jan 25 16:55:34 chtpub sendEmail[30496]: Email was sent successfully!

centos下磁盘监控告警并发出邮件_第1张图片

5、最后设置一个可执行计划任务,计划每五分钟执行一次,超过80%发出邮件告警

#crontab -e

*/5 * * * * /opt/disk_check.sh 80 test

保存退出即可!

你可能感兴趣的:(linux,sendmail)