Skip to content

Instantly share code, notes, and snippets.

@greensea
Last active May 4, 2017 07:22
Show Gist options
  • Save greensea/a1a1a72d8a68789b8825735de9b413ac to your computer and use it in GitHub Desktop.
Save greensea/a1a1a72d8a68789b8825735de9b413ac to your computer and use it in GitHub Desktop.
检查服务器指定分区的剩余空间,如果低于阈值则发送邮件通知
#/bin/sh
if [ $# -lt 4 ]
then
echo "Usage: $0 <mount_point> <min_space_MB> <to_email> <from_email>"
exit 0
fi
MOUNT_POINT=$1
MIN_SPACE_MB=$2
TO_MAIL=$3
FROM_MAIL=$4
body=""
# 计算各种变量
df_line=`df -m "$MOUNT_POINT" | grep "$MOUNT_POINT"`
if [ -z "$df_line" ]
then
echo "挂载点 ${MOUNT_POINT} 不存在"
exit 1
fi
used=`echo $df_line | awk -F ' ' '{print $3}'`
left=`echo $df_line | awk -F ' ' '{print $4}'`
ratio=`echo $df_line | awk -F ' ' '{print $5}'`
if [ $left -gt $MIN_SPACE_MB ]
then
echo "剩余空间充足(${left} MB > ${MIN_SPACE_MB} MB),不会发送告警"
exit 0
fi
title="`hostname` 上的 ${MOUNT_POINT} 分区可用空间仅剩 ${left} MB"
body="`hostname` 服务器挂载点 ${MOUNT_POINT} 使用情况:\n"
body="${body}\n""已使用 $used MB"
body="${body}\n""使用率 $ratio"
body="${body}\n""剩余 $left MB"
body="${body}\n\n`hostname` 上的 ${MOUNT_POINT} 分区的剩余空间已不足提醒阈值 ${MIN_SPACE_MB} MB,请及时检查服务器磁盘空间状态"
body="${body}\n\n本次磁盘空间检查的时间为 `date`"
echo -e $title
echo -e "$body"
echo -e "$body" | mail -s "$title" $TO_MAIL -r $FROM_MAIL -v
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment