且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

shell监控脚本-监控磁盘

更新时间:2022-10-02 22:47:46

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


本文转自 dongnan 51CTO博客,原文链接:http://blog.51cto.com/dngood/1163558