shell監控腳本-監控磁盤

shell監控腳本-監控磁盤

注意:請先參考 shell監控腳本-準備工作,監控腳本rhel5 下測試正常,其它版本的linux 系統請自行測試
#監控磁盤

  1. cat chk_df.sh

  2. #!/bin/bash

  3. #

  4. #script_name:chk_df.sh

  5. #check disk

  6. #

  7. #last update 20130320 by dongnan

  8. #bbs# http://bbs.ywwd.net/

  9. #blog# http://dngood.blog.51cto.com

  10. #

  11. #df -Th

  12. #Filesystem    Type    Size  Used Avail Use% Mounted on

  13. #/dev/sda1     ext3    9.7G  5.9G  3.4G  64% /

  14. #/dev/sdb1     ext3     79G   73G  2.2G  98% /data

  15. #variables

  16. ssh=/usr/bin/ssh

  17. sh_dir=/root/sh/

  18. crondir=${sh_dir}crontab

  19. source ${sh_dir}CONFIG

  20. hosts="$LINUX_WEB_HOSTS"

  21. user=`id -u`

  22. let df_limit=80

  23. #main

  24. #主循環遍歷機器

  25. for HOST in $hosts ;do

  26. flag_disk_file=$crondir/log/"$HOST".disk

  27. log=$crondir/log/disk_error.log

  28.    #執行ssh 命令

  29. capacity=`$ssh -o ConnectTimeout=2 root@$HOST "df" | grep "/dev/" | sed 's/\%//' | awk '{print $5}'`

  30.    let flags=0

  31.    #無法連接的主機,跳過本次循環

  32.    test -z "$capacity" && continue

  33.    #判斷ssh命令返回結果

  34.    for used in $capacity ;do

  35.        if [ $used -ge $df_limit  ];then

  36.            let flags=1

  37.            break

  38.        fi

  39.    done

  40.   #如果磁盤超過限制,則發送報警郵件

  41.    if [ "$flags" -eq 1 -a ! -f "$flag_disk_file" ];then

  42.        #sms

  43.        #for mobile in "$MOBILES";do

  44.            #echo "$HOST disk will full" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode

  45.        #done

  46.        #mail

  47.        for mail in $MAILS;do

  48.            echo "$HOST disk will full" | mail -s "$HOST disk will full" $mail

  49.        done

  50.        #log

  51.        date +'%F %T' >>$log

  52.        echo "$HOST disk will full" >> $log

  53.        #flag

  54.        echo "disk_error" >$flag_disk_file

  55.    fi

  56.    #如果磁盤正常,則發郵件解除報警郵件

  57.    if [ "$flags" -eq 0 -a  -f "$flag_disk_file" ];then

  58.        #sms

  59.        #for mobile in "$MOBILES";do

  60.            #echo "$HOST disk ok"|/usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode

  61.        #done

  62.        #mail

  63.        for mail in $MAILS;do

  64.            echo "$HOST disk ok" | mail -s "$HOST disk ok" $mail

  65.        done

  66.        #log

  67.        date +'%F %T' >>$log

  68.        echo "$HOST disk ok" >> $log

  69.        #flag

  70.        rm -f $flag_disk_file

  71.    fi

  72. done

#
結束
更多請:  
linux 系統運維  37275208  
vmware 虛擬化  166682360

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章