前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux/Unix shell 脚本监控磁盘可用空间

Linux/Unix shell 脚本监控磁盘可用空间

作者头像
Leshami
发布2018-08-13 15:49:15
8880
发布2018-08-13 15:49:15
举报
文章被收录于专栏:乐沙弥的世界乐沙弥的世界

    Linux下监控磁盘的空闲空间的shell脚本,对于系统管理员或DBA来说,必不可少。下面是给出的一个监控磁盘空间空间shell脚本的样本,供大家参考。

1、监控磁盘的空闲空间shell脚本

代码语言:javascript
复制
robin@SZDB:~/dba_scripts/custom/bin> more ck_fs_space.sh 
#!/bin/bash
# ------------------------------------------------------------------------------+
#                  CHECK FILE SYSYTEM SPACE BY THRESHOLD                        |
#   Filename: ck_fs_space.sh                                                    |
#   Desc:                                                                       |
#       The script use to check file system space by threshold                  |
#       Once usage of the disk beyond the threshold, a mail alert will be sent. |   
#       Deploy it by crontab. e.g. per 15 min                                   |  
#   Usage:                                                                      |
#       ./ck_fs_space.sh <percent> </filesystem [/filesystem2]>                 |  
#                                                                               |
#   Author : Robinson                                                           | 
#   Blog   : http://blog.csdn.net/robinson_0612                                 |
# ------------------------------------------------------------------------------+
#
# -------------------------------
#  Set environment here 
# ------------------------------

if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile
fi

export host=`hostname`
export mail_dir=/users/robin/dba_scripts/sendEmail-v1.56
export mail_list='Robinson.cheng@12306.com'
export mail_fm='oracle@szdb.com'
tmpfile=/tmp/ck_fs_space.txt
alert=n

# --------------------------------
#  Check the parameter
# --------------------------------

max=$1

if [ ! ${2} ]; then
    echo "No filesystems specified."
    echo "Usage: ck_fs_space.sh 90 / /u01"
    exit 1
fi

# --------------------------------
#  Start to check the disk space
# --------------------------------

while [ "${2}" ]
    do
        percent=`df -P ${2} | tail -1 | awk '{print $5 }' | cut -d'%' -f1`
        if [ "${percent}" -ge "${max}" ]; then
            alert=y
            break
        fi;
        shift
    done;

# ------------------------------------------------------------------------
#  When a partition was above the threshold then send mail with df output
# ------------------------------------------------------------------------

if [ ! "${alert}" = 'n' ];then
    df -h >$tmpfile
    mail_sub="Disk usage beyond the threshold ${max} on ${host}."
    $mail_dir/sendEmail -u ${mail_sub} -f $mail_fm -t $mail_list -o message-file=${tmpfile}
fi;

exit;

2、脚本说明    a、该脚本使用了 sendEmail 工具来发送邮件。    b、使用方式为"Usage: ck_fs_space.sh 90 / /u01" 。    c、脚本中使用了一个while循环来逐个判断所有的指定分区的空闲空间是否超出阙值。    d、对于超出阙值的情形发送邮件并且附上当前服务器上磁盘空间的使用情况。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013年05月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档