伺服器磁碟監控指令碼分享(含報警郵件)

散盡浮華發表於2016-10-17

 

在日常的運維工作中,我們經常會對伺服器的磁碟使用情況進行巡檢,以防止磁碟爆滿導致的業務故障.
如果能編寫一個合理完善的監控指令碼,當磁碟使用率達到我們設定的閥值時,就自動傳送報警郵件,以便我們及時獲悉到快爆滿的磁碟情況!

下面分享一個指令碼:
監控本機的根磁碟和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

#!/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郵箱,發現已經收到了上面傳送的郵件:

相關文章