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    ,如需转载请自行联系原作者
相关文章
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
835 3
Linux系统初始化脚本
|
8月前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
774 9
|
8月前
|
算法 Linux Shell
Linux实用技能:打包压缩、热键、Shell与权限管理
本文详解Linux打包压缩技巧、常用命令与原理,涵盖.zip与.tgz格式操作、跨系统传文件方法、Shell运行机制及权限管理,助你高效使用Linux系统。
Linux实用技能:打包压缩、热键、Shell与权限管理
|
8月前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
785 2
|
10月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
1182 1
|
12月前
|
Java Linux
自定义linux脚本用于快速jar包启动、停止、重启
自定义linux脚本用于快速jar包启动、停止、重启
465 29
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
631 3
|
Unix Shell Linux
在Unix/Linux操作系统中,Shell脚本广泛用于自动化任务
在Unix/Linux操作系统中,Shell脚本广泛用于自动化任务
300 2
|
Unix Shell Linux
在Linux和类Unix系统中,Shell提供了多种命令用于用户和权限管理
在Linux和类Unix系统中,Shell提供了多种命令用于用户和权限管理
253 4