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    ,如需转载请自行联系原作者
相关文章
|
8天前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
153 10
|
8天前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
62 2
|
3月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
213 1
|
6月前
|
存储 监控 Linux
Linux: 检测磁盘坏块 你得会吧!
Linux: 检测磁盘坏块 你得会吧!
358 19
Linux: 检测磁盘坏块 你得会吧!
|
5月前
|
Linux
在线对Linux进行磁盘扩容的技术指南。
综上所述,Linux磁盘扩容的过程,重要的不仅是技术,更是对每一步骤的深刻理解和投入的爱心。只要手握正确的工具,我们不仅能满足"孩子"的成长需求,还能享受其中的乐趣和成就。
327 10
|
5月前
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
128 4
|
5月前
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
271 3
|
6月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
435 25
|
6月前
|
Linux Shell
shell_42:Linux参数移动
总的来说,参数移动是Linux shell脚本中的一个重要概念,掌握它可以帮助我们更好地处理和管理脚本中的参数。希望这个解释能帮助你理解和使用参数移动。
94 18
|
6月前
|
监控 固态存储 Linux
如何判断Linux磁盘是SSD还是HDD?
总的来说,判断磁盘是SSD还是HDD并不复杂,只需要使用正确的命令和方法,就可以轻松得到结果。希望这些信息对你有所帮助,如果你还有其他问题,欢迎随时提问。
518 15