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    ,如需转载请自行联系原作者
相关文章
|
15天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
13天前
|
存储 Shell Linux
【攻防世界】unseping (反序列化与Linux bash shell)
【攻防世界】unseping (反序列化与Linux bash shell)
|
16天前
|
Shell Linux
【Linux】12. 模拟实现shell
【Linux】12. 模拟实现shell
27 2
|
21天前
|
Shell Linux
Linux的shell入门教程shell脚本入门教程
Linux的shell入门教程shell脚本入门教程
15 0
|
28天前
|
存储 算法 Shell
【Linux 环境变量相关】深入理解Linux下 CMake、Shell 与环境变量的交互(二)
【Linux 环境变量相关】深入理解Linux下 CMake、Shell 与环境变量的交互
48 0
|
29天前
|
Shell Linux 开发工具
shell的介绍以及Linux权限的讲解
shell的介绍以及Linux权限的讲解
31 2
|
1月前
|
网络协议 Shell Linux
【Shell 命令集合 系统管理 】Linux 查询域名的注册信息 whois命令 使用指南
【Shell 命令集合 系统管理 】Linux 查询域名的注册信息 whois命令 使用指南
48 1
|
SQL 分布式计算 大数据
电商项目之 DWD 用户启动日志表 Shell 脚本编写|学习笔记
快速学习电商项目之 DWD 用户启动日志表 Shell 脚本编写
92 0
电商项目之 DWD 用户启动日志表 Shell 脚本编写|学习笔记
|
关系型数据库 MySQL 大数据
电商项目之 Sqoop 的 Shell 脚本编写|学习笔记
快速学习电商项目之 Sqoop 的 Shell 脚本编写
205 0
电商项目之 Sqoop 的 Shell 脚本编写|学习笔记
|
运维 Java Shell
shell脚本回顾及find文件查找基本用法| 学习笔记
快速学习shell脚本回顾及find文件查找基本用法
291 0