使用shell脚本进行服务器系统监控——文件系统监控(2)

本文介绍了一个用于监控Unix/Linux系统中文件系统使用情况的Shell脚本。该脚本通过定制化的阈值设置来检查文件系统的利用率,并能针对特定挂载点设定例外规则。当文件系统使用率超过设定阈值时,脚本会记录详细信息。

#!/usr/bin/ksh


WORKFILE="/tmp/df.work" # Holds filesystem data
>$WORKFILE # Initialize to empty
OUTFILE="/tmp/df.outfile" # Output display file
>$OUTFILE# Initialize to empty
BINDIR="/usr/local/bin" # Local bin directory
THISHOST=`hostname`# Hostname of this machine
FSMAX="85" # Max. FS percentage value

EXCEPTIONS="${BINDIR}/exceptions" # Overrides $FSMAX
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o #, comments


function load_EXCEPTIONS_file
{
cat $EXCEPTIONS | grep -v "^#" | sed /^$/d > $DATA_EXCEPTIONS
}

function check_exceptions
{

# set -x # Uncomment to debug this function

while read FSNAME NEW_MAX # Feeding Ddata from Bottom of Loop!!!
do
if [[ $FSNAME = $FSMOUNT ]] # Correct /mount_point?
then # Get rid of the % sign, if it exists!
NEW_MAX=$(echo $NEW_MAX | sed s//%//g)

if [ $FSVALUE -gt $NEW_MAX ]
then # Over Limit...Return a "0", zero
return 0 # FOUND MAX OUT - Return 0
fi
fi

done < $DATA_EXCEPTIONS # Feed from the bottom of the loop!!

return 1 # Not found in File
}

######## START OF MAIN #############

[[ -s $EXCEPTIONS ]] && load_EXCEPTIONS_file

# Get the data of interest by stripping out the
# /cdrom row and keeping columns 1, 5 and 6

df -k | tail +2 | egrep -v '/cdrom' /
| awk '{print $1, $5, $6}' > $WORKFILE

# Loop through each line of the file and compare column 2

while read FSDEVICE FSVALUE FSMOUNT
do
# Strip out the % sign if it exists
FSVALUE=$(echo $FSVALUE | sed s//%//g) # Remove the % sign
if [[ -s $EXCEPTIONS ]] # Do we have a non-empty file?
then # Found it!

# Look for the current $FSMOUNT value in the file
# using the check_exceptions function defined above.

check_exceptions
RC=$?
if [ $RC -eq 0 ] # Found it Exceeded!!
then
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" /
>> $OUTFILE
elif [ $RC -eq 1 ] # Not founf in exceptions file, use defaults
then
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
then
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" /
>> $OUTFILE
fi
fi
else # No exceptions file use the script default
if [ $FSVALUE -gt $FSMAX ] # Use Script Default
then
echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" /
>> $OUTFILE
fi
fi
done < $WORKFILE # Feed the while loop from the bottom...

# Display output if anything is exceeded...

if [[ -s $OUTFILE ]]
then
echo "/nFull Filesystem(s) on $THISHOST/n"
cat $OUTFILE
print
fi

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值