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

文章可以转载,必须以链接形式标明出处。

本文转自 张冲andy 博客园博客,原文链接:http://www.cnblogs.com/andy6/p/5877498.html    ,如需转载请自行联系原作者
相关文章
|
4天前
|
Shell Linux Perl
Linux|如何允许 awk 使用 Shell 变量
Linux|如何允许 awk 使用 Shell 变量
13 2
|
4天前
|
网络协议 Shell Linux
LabVIEW 在NI Linux实时设备上访问Shell
LabVIEW 在NI Linux实时设备上访问Shell
12 0
|
4天前
|
存储 安全 Linux
Linux:进程地址空间
Linux:进程地址空间
25 10
|
4天前
|
Shell Linux
【Linux】进程实践项目(更新中) — 自主shell编写
前几篇文章,我们学习进程的相关知识:进程概念,进程替换,进程控制。熟悉了进程到底是个什么事情,接下来我们来做一个实践,来运用我们所学的相关知识。这个项目就是手搓一个shell模块,模拟实现Xshell中的命令行输入。
14 1
|
4天前
|
Shell Linux 信息无障碍
5 个有用的 Linux Shell 转义序列
5 个有用的 Linux Shell 转义序列
|
4天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
30 5
|
4天前
|
Linux Shell 程序员
【Linux】权限(shell运行原理、概念,Linux权限)
【Linux】权限(shell运行原理、概念,Linux权限)
15 2
|
4天前
|
Ubuntu Linux Shell
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
75 0
|
4天前
|
Shell Linux C++
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
47 0
|
4天前
|
运维 Shell Linux
Linux 之大数据定制篇-Shell 编程
Linux 之大数据定制篇-Shell 编程
125 0