歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Linux/Unix shell 腳本監控磁盤可用空間

Linux/Unix shell 腳本監控磁盤可用空間

日期:2017/3/1 9:57:33   编辑:SHELL編程

Linux下監控磁盤的空閒空間的shell腳本,對於系統管理員或DBA來說,必不可少。下面是給出的一個監控磁盤空間空間shell腳本的樣本,供大家參考。

1、監控磁盤的空閒空間shell腳本

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]> |
# |
# ------------------------------------------------------------------------------+
#
# -------------------------------
# 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='[email protected]'
export mail_fm='[email protected]'
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 工具來發送郵件 http://www.linuxidc.com/Linux/2012-12/76495p2.htm。

b、使用方式為"Usage: ck_fs_space.sh 90 / /u01" 。

c、腳本中使用了一個while循環來逐個判斷所有的指定分區的空閒空間是否超出阙值。

d、對於超出阙值的情形發送郵件並且附上當前服務器上磁盤空間的使用情況。

Copyright © Linux教程網 All Rights Reserved