服务器磁盘监控脚本分享(含报警邮件)

在日常的运维工作中,我们经常会对服务器的磁盘使用情况进行巡检,以防止磁盘爆满导致的业务故障.
如果能编写一个合理完善的监控脚本,当磁盘使用率达到我们设置的阀值时,就自动发送报警邮件,以便我们及时获悉到快爆满的磁盘情况!

下面分享一个脚本:
监控本机的根磁盘和home盘,当根磁盘使用率达到90%和home磁盘使用率达到95%的时候,发报警邮件至wangshibo@huanqiu.cn和liugang@huanqiu.cn

[root@haunqiu-beta ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   50G   46G   12G  90% /
tmpfs                          32G   68K   32G   1% /dev/shm
/dev/sda1                     485M   40M  421M   9% /boot
/dev/mapper/VolGroup-lv_home  836G  795G  673G  95% /home

 

取根磁盘当前利用率的百分值
[root@haunqiu-beta ~]# /bin/df -h|grep /dev/mapper/VolGroup-lv_root|awk -F" " '{print $5}'|cut -d"%" -f1
90

取home盘当前利用率的百分值
[root@haunqiu-beta ~]# /bin/df -h|grep /dev/mapper/VolGroup-lv_home|awk -F" " '{print $5}'|cut -d"%" -f1
95


编写邮件报警脚本
[root@haunqiu-beta ~]# vim /root/root_disk.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
SERVER_IP=` ifconfig | grep  192.168.1| awk  -F ":"  '{print $2}' | cut  -d " "  -f1`
ROOT_DISK=` /bin/df  -h| grep  /dev/mapper/VolGroup-lv_root | awk  -F " "  '{print $5}' | cut  -d "%"  -f1`
HOME_DISK=` /bin/df  -h| grep  /dev/mapper/VolGroup-lv_home | awk  -F " "  '{print $5}' | cut  -d "%"  -f1`
 
if  [ $ROOT_DISK - ge  90 ]; then
/usr/local/bin/sendEmail  -f ops@huanqiu.cn -t wangshibo@huanqiu.cn -s smtp.huanqiu.cn -u  " The ROOT_DISK of $SERVER_IP-$HOSTNAME is warning!"  -o message-content- type =html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -m  "The ROOT_DISK of $SERVER_IP-$HOSTNAME,now use% is 90%,please deal with it as soon as possible"
/usr/local/bin/sendEmail  -f ops@huanqiu.cn -t liugang@huanqiu.cn -s smtp.huanqiu.cn -u  " The ROOT_DISK of $SERVER_IP-$HOSTNAME is warning!"  -o message-content- type =html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -m  "The ROOT_DISK of $SERVER_IP-$HOSTNAME,now use% is 90%,please deal with it as soon as possible"
else
echo  "The ROOT_DISK of $SERVER_IP-$HOSTNAME is Enough to use"
fi
 
sleep  5
 
if  [ $HOME_DISK - ge  95 ]; then
/usr/local/bin/sendEmail  -f ops@huanqiu.cn -t wangshibo@huanqiu.cn -s smtp.huanqiu.cn -u  " The HOME_DISK of $SERVER_IP-$HOSTNAME is warning!"  -o message-content- type =html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -m  "The HOME_DISK of $SERVER_IP-$HOSTNAME,now use% is 95%,please deal with it as soon as possible"
/usr/local/bin/sendEmail  -f ops@huanqiu.cn -t liugang@huanqiu.cn -s smtp.huanqiu.cn -u  " The HOME_DISK of $SERVER_IP-$HOSTNAME is warning!"  -o message-content- type =html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -m  "The HOME_DISK of $SERVER_IP-$HOSTNAME,now use% is 95%,please deal with it as soon as possible"
else
echo  "The ROOT_DISK of $SERVER_IP-$HOSTNAME is Enough to use"
fi

设置计划任务
[root@haunqiu-beta ~]# crontab -e
*/30 * * * * /bin/bash -x /root/root_disk.sh > /dev/null 2>&1

-----------------------------------------------------------------------------------------------------------------
上面脚本中的邮件报警用的是sendemail,需要提前安装sendemail环境,安装操作如下:
1)先下载安装包到本地,解压。
[root@haunqiu-beta ~]# cd /usr/local/src/
[root@haunqiu-beta src]# wget -c http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
[root@haunqiu-beta src]# tar -zvxf sendEmail-v1.56.tar.gz
[root@haunqiu-beta src]# cd sendEmail-v1.56
[root@haunqiu-beta sendEmail-v1.56]# cp -a sendEmail /usr/local/bin/
[root@haunqiu-beta sendEmail-v1.56]# chmod +x /usr/local/bin/sendEmail
[root@haunqiu-beta sendEmail-v1.56]# file /usr/local/bin/sendEmail
/usr/local/bin/sendEmail: a /usr/bin/perl -w script text executable

2)安装下依赖
[root@haunqiu-beta sendEmail-v1.56]# yum install perl-Net-SSLeay perl-IO-Socket-SSL -y

[root@haunqiu-beta sendEmail-v1.56]# /usr/local/bin/sendEmail -f from@huanqiu.cn -t to@huanqiu.cn -s smtp.huanqiu.cn -u "我是邮件主题" -o message-content-type=html -o message-charset=utf8 -xu from@huanqiu.cn -xp zh@123bj -m "我是邮件内容"

命令说明:
/usr/local/bin/sendEmail                            #命令主程序
-f from@uhanqiu.cn                                  #发件人邮箱
-t to@huanqiu.cn                                      #收件人邮箱
-s smtp.huanqi.cn                                     #发件人邮箱的smtp服务器
-u "我是邮件主题"                                       #邮件的标题
-o message-content-type=html                #邮件内容的格式,html表示它是html格式
-o message-charset=utf8                        #邮件内容编码
-xu from@huanqiu.cn                              #发件人邮箱的用户名
-xp zh@123bj                                    #发件人邮箱密码
-m "我是邮件内容"                                    #邮件的具体内容

例如:
[root@haunqiu-beta alertscripts]# /usr/local/bin/sendEmail -f ops@huanqiu.cn -t wangshibo@huanqiu.cn -s smtp.huanqiu.cn -u "我是邮件主题" -o message-content-type=html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -m "我是邮件内容"

Oct 14 19:38:29 haunqiu-beta sendEmail[65454]: Email was sent successfully!
[root@haunqiu-beta alertscripts]#

登陆wangshibo@huanqiu.cn邮箱,发现已经收到了上面发送的邮件:

***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************
分类:  Shell, 监控系统
本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/5970305.html ,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值