Linux下监控磁盘使⽤量并在超过阀值后⾃动发送报警邮件脚本思路:
⽤df命令查看磁盘使⽤量信息,超过脚本设置的阀值(critical=90)90%,就会⾃动⽤sendEmail命令发送报警邮件(邮件smtp、email等需⾃⾏修改成真实存在的)。
[root@rsync-server opt]# ll
total 36
-rwxr-xr-x 1 root root 888 Mar 15 14:22 disk_check.sh
drwxr-xr-x 2 root root 101 Sep 30 2009 sendEmail-v1.56
-rw-r--r-- 1 root root 29740 Mar 15 14:13 sendEmail-v1. 软件下载:pan.baidu/s/1mgjxRtu
#脚本和sendmail程序,都上传到opt⽬录下⾯。
[root@rsync-server opt]# cat disk_check.sh
#!/bin/bash
partition_list=(`df -h | awk 'NF>3&&NR>1{sub(/%/,"",$(NF-1));print $NF,$(NF-1)}'`)
critical=50
notification_email()
{
emailuser='huzh@hotdoor'
emailpasswd='lenovoe42'
emailsmtp='loud'
sendto='huzh@hotdoor'
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
#授权
[root@rsync-server opt]# chmod a+x disk_check.sh
#执⾏结果
[root@rsync-server opt]# sh disk_check.sh
OK! / used 6%
OK! /dev used 0%
OK! /dev/shm used 0%
OK! /run used 12%
OK! /sys/fs/cgroup used 0%
OK! /boot used 27%
OK! /run/user/0 used 0%
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.
Mar 15 14:19:19 rsync-server sendEmail[25064]: Message input complete.
Mar 15 14:19:19 rsync-server sendEmail[25064]: Email was sent successfully!
#效果:
linux磁盘管理#最后,可以设置⼀个cron计划任务,让该脚本每隔1⼩时运⾏⼀次[root@webmail /]# crontab -e
在cron⾥添加下⾯⼀⾏即可
*/60 * * * * /opt/disk_check.sh
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论