在平常的運維工做中,咱們常常會對服務器的磁盤使用狀況進行巡檢,以防止磁盤爆滿致使的業務故障。若是能編寫一個合理完善的監控腳本,當磁盤使用率達到咱們設置的閥值時,就自動發送報警郵件,以便咱們及時獲悉到快爆滿的磁盤狀況!html
下面分享一個腳本:
監控本機的根磁盤和home盤,當根磁盤使用率達到90%和home磁盤使用率達到95%的時候,發報警郵件至wangshibo@kevin.cn和liugang@kevin.cnvim
[root@kevin ~]# 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@kevin ~]# /bin/df -h|grep /dev/mapper/VolGroup-lv_root|awk -F" " '{print $5}'|cut -d"%" -f1 90 取home盤當前利用率的百分值 [root@kevin ~]# /bin/df -h|grep /dev/mapper/VolGroup-lv_home|awk -F" " '{print $5}'|cut -d"%" -f1 95 編寫郵件報警腳本 [root@kevin ~]# 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@kevin.cn -t wangshibo@kevin.cn -s smtp.kevin.cn -u " The ROOT_DISK of $SERVER_IP-$HOSTNAME is warning!" -o message-content-type=html -o message-charset=utf8 -xu ops@kevin.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@kevin.cn -t liugang@kevin.cn -s smtp.kevin.cn -u " The ROOT_DISK of $SERVER_IP-$HOSTNAME is warning!" -o message-content-type=html -o message-charset=utf8 -xu ops@kevin.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@kevin.cn -t wangshibo@kevin.cn -s smtp.kevin.cn -u " The HOME_DISK of $SERVER_IP-$HOSTNAME is warning!" -o message-content-type=html -o message-charset=utf8 -xu ops@kevin.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@kevin.cn -t liugang@kevin.cn -s smtp.kevin.cn -u " The HOME_DISK of $SERVER_IP-$HOSTNAME is warning!" -o message-content-type=html -o message-charset=utf8 -xu ops@kevin.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@kevin ~]# crontab -e */30 * * * * /bin/bash -x /root/root_disk.sh > /dev/null 2>&1
========================================================================================
上面腳本中的郵件報警用的是sendemail,須要提早安裝sendemail環境,安裝操做以下:bash
1)先下載安裝包到本地,解壓。 [root@kevin ~]# cd /usr/local/src/ [root@kevin src]# wget -c http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz [root@kevin src]# tar -zvxf sendEmail-v1.56.tar.gz [root@kevin src]# cd sendEmail-v1.56 [root@kevin sendEmail-v1.56]# cp -a sendEmail /usr/local/bin/ [root@kevin sendEmail-v1.56]# chmod +x /usr/local/bin/sendEmail [root@kevin sendEmail-v1.56]# file /usr/local/bin/sendEmail /usr/local/bin/sendEmail: a /usr/bin/perl -w script text executable 2)安裝下依賴 [root@kevin sendEmail-v1.56]# yum install perl-Net-SSLeay perl-IO-Socket-SSL -y [root@kevin sendEmail-v1.56]# /usr/local/bin/sendEmail -f from@kevin.cn -t to@kevin.cn -s smtp.kevin.cn -u "我是郵件主題" -o message-content-type=html -o message-charset=utf8 -xu from@kevin.cn -xp zh@123bj -m "我是郵件內容" 命令說明: /usr/local/bin/sendEmail #命令主程序 -f from@kevin.cn #發件人郵箱 -t to@kevin.cn #收件人郵箱 -s smtp.huanqi.cn #發件人郵箱的smtp服務器 -u "我是郵件主題" #郵件的標題 -o message-content-type=html #郵件內容的格式,html表示它是html格式 -o message-charset=utf8 #郵件內容編碼 -xu from@kevin.cn #發件人郵箱的用戶名 -xp zh@123bj #發件人郵箱密碼 -m "我是郵件內容" #郵件的具體內容 例如: [root@kevin alertscripts]# /usr/local/bin/sendEmail -f ops@kevin.cn -t wangshibo@kevin.cn -s smtp.kevin.cn -u "我是郵件主題" -o message-content-type=html -o message-charset=utf8 -xu ops@kevin.cn -xp zh@123bj -m "我是郵件內容" Oct 14 19:38:29 kevin sendEmail[65454]: Email was sent successfully!
登錄wangshibo@kevin.cn郵箱,發現已經收到了上面發送的郵件:服務器