Linux下监控磁盘使用量并在超过阀值后自动发送报警邮件
最近Linux服务器磁盘使用量经常到100%,直到影响到正常服务出现故障才会去注意,做不到防患于未然,今天在网上搜集了资料,加上自己修改,写了一个shell脚本用于实时监控磁盘使用量并在超过阀值后自动发送报警邮件。
脚本简单说明:用df命令查看磁盘使用量信息,超过脚本设置的阀值(critical=90)90%,就会自动用sendEmail命令发送报警邮件(邮件smtp、email等需自行修改成真实存在的)。
1、首先复制下面脚本代码存为disk_check.sh文件
emailuser=发件人邮件
emailpasswd=发件人邮件密码
emailsmtp=邮件主题
sendto=收件人邮件,多人用,分开
title=邮件主题
#!/bin/bash
partition_list=(`df -h | awk 'NF>3&&NR>1{sub(/%/,"",$(NF-1));print $NF,$(NF-1)}'`)
critical=90
notification_email()
{
[email protected]'
emailpasswd='password'
emailsmtp='smtp.domain.com'
[email protected]'
title='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
echo -e $crit_info | notification_email
fi
2、上传disk_check.sh文件到linux服务器的/opt目录下
3、给disk_check.sh添加可执行权限
[[email protected] /]# chmod 755 /opt/disk_check.sh
4、下载sendEmail邮件程序命令(点击下载),上传到服务器的/opt目录下
5、解压/opt/sendEmail-v1.56.tar.gz
[[email protected] /]# tar xvf /opt/sendEmail-v1.56.tar.gz
6、测试脚本/报警邮件是否运行正常,可以看到,有2个分区是正常的,显示OK!,没有超过设置的阀值,有一个分区使用量超过阀值了,并自动成功发送了报警邮件,再去我设置的收件邮箱,可以看到已经收到了这封报警邮件:
[[email protected] /]# sh /opt/disk_check.sh
OK! /dev/shm used 1%
OK! /boot used 12%
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.
Jul 01 17:32:44 localhost sendEmail[5585]: Message input complete.
Jul 01 17:32:44 localhost sendEmail[5585]: Email was sent successfully!
disk_check
7、最后,可以设置一个cron计划任务,让该脚本每隔1小时运行一次
[[email protected] /]# crontab -e
在cron里添加下面一行即可
*/60 * * * * /opt/disk_check.sh
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/6373.html