Linux/Unix shell 腳本監控磁盤可用空間


 Linux下監控磁盤的空閑空間的shell腳本,對於系統管理員或DBA來說,必不可少。下面是給出的一個監控磁盤空間空間shell腳本的樣本,供大家參考。

 

1、監控磁盤的空閑空間shell腳本

[python]  view plain  copy
 
 print?
  1. robin@SZDB:~/dba_scripts/custom/bin> more ck_fs_space.sh   
  2. #!/bin/bash  
  3. # ------------------------------------------------------------------------------+  
  4. #                  CHECK FILE SYSYTEM SPACE BY THRESHOLD                        |  
  5. #   Filename: ck_fs_space.sh                                                    |  
  6. #   Desc:                                                                       |  
  7. #       The script use to check file system space by threshold                  |  
  8. #       Once usage of the disk beyond the threshold, a mail alert will be sent. |     
  9. #       Deploy it by crontab. e.g. per 15 min                                   |    
  10. #   Usage:                                                                      |  
  11. #       ./ck_fs_space.sh <percent> </filesystem [/filesystem2]>                 |    
  12. #                                                                               |  
  13. #   Author : Robinson                                                           |   
  14. #   Blog   : http://blog.csdn.net/robinson_0612                                 |  
  15. # ------------------------------------------------------------------------------+  
  16. #  
  17. # -------------------------------  
  18. #  Set environment here   
  19. # ------------------------------  
  20.   
  21. if [ -f ~/.bash_profile ]; then  
  22.     . ~/.bash_profile  
  23. fi  
  24.   
  25. export host=`hostname`  
  26. export mail_dir=/users/robin/dba_scripts/sendEmail-v1.56  
  27. export mail_list='Robinson.cheng@12306.com'  
  28. export mail_fm='oracle@szdb.com'  
  29. tmpfile=/tmp/ck_fs_space.txt  
  30. alert=n  
  31.   
  32. # --------------------------------  
  33. #  Check the parameter  
  34. # --------------------------------  
  35.   
  36. max=$1  
  37.   
  38. if [ ! ${2} ]; then  
  39.     echo "No filesystems specified."  
  40.     echo "Usage: ck_fs_space.sh 90 / /u01"  
  41.     exit 1  
  42. fi  
  43.   
  44. # --------------------------------  
  45. #  Start to check the disk space  
  46. # --------------------------------  
  47.   
  48. while [ "${2}" ]  
  49.     do  
  50.         percent=`df -P ${2} | tail -1 | awk '{print $5 }' | cut -d'%' -f1`  
  51.         if [ "${percent}" -ge "${max}" ]; then  
  52.             alert=y  
  53.             break  
  54.         fi;  
  55.         shift  
  56.     done;  
  57.   
  58. # ------------------------------------------------------------------------  
  59. #  When a partition was above the threshold then send mail with df output  
  60. # ------------------------------------------------------------------------  
  61.   
  62. if [ ! "${alert}" = 'n' ];then  
  63.     df -h >$tmpfile  
  64.     mail_sub="Disk usage beyond the threshold ${max} on ${host}."  
  65.     $mail_dir/sendEmail -u ${mail_sub} -f $mail_fm -t $mail_list -o message-file=${tmpfile}  
  66. fi;  
  67.   
  68. exit;  

2、腳本說明
   a、該腳本使用了 sendEmail 工具來發送郵件。
   b、使用方式為"Usage: ck_fs_space.sh 90 / /u01" 。
   c、腳本中使用了一個while循環來逐個判斷所有的指定分區的空閑空間是否超出闕值。
   d、對於超出闕值的情形發送郵件並且附上當前服務器上磁盤空間的使用情況。

轉:http://blog.csdn.net/leshami/article/details/8893943


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM