Nagios监控系统内存sh脚本

发布时间:2019-10-15编辑:脚本学堂
nagios监控系统内存的脚本代码。

nagios监控系统内存的脚本代码。

复制代码 代码如下:

#!/bin/sh
#====================================
# $Name:         sysmem_check.sh
# $Revision:     1.0
# $Function:     This Nagios plugin can be check linux system memory status
# $Author:       Shundong Zhao
# $organization: UnixHot
# $Create Date:  2010-06-10
#====================================

USAGE_Method="$(basename $0) [-w|--warning] <Free Percent> [-c|--critical] <Free Percent>"
USAGE_Value="WARNING value must be large than CRITICAL value: `basename $0` $*"
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3 
#设置Nagios要求的状态
if [ $# -lt 4 ];then  
        echo
        echo "Usage: $USAGE_Method"
        echo
        exit 0
fi

while [ $# -gt 0 ];do
        case "$1" in
               -w|--warning)
               shift
               WARNING=$1
        ;;
               -c|--critical)
               shift
               CRITICAL=$1
        ;;
        esac
        shift
done
if [[ $WARNING -eq $CRITICAL || $WARNING -lt $CRITICAL ]]
then
        echo
        echo "$USAGE_Value"
        echo
        echo "Usage: $USAGE_Method"
        echo
        exit 0
fi
FREE_MEM=$(free -m | grep - | awk -F ' ' '{print $4}')  #取当前未使用的内存,注意是未使用!
TOTAL_MEM=$(free -m | grep Mem | awk -F ' ' '{print $2}') #取当前系统总内存
PERCENT=$(bc <<< "scale=2;$FREE_MEM/$TOTAL_MEM" | tr '^.' ' ') #使用bc输出百分比
if [ "$PERCENT" -le "$CRITICAL" ] #如果监测百分比小于等于用户设置的严重错误值,返回2
        then
                echo "CRITICAL - $FREE_MEM MB ($PERCENT%) Free Memory"
                exit 2
fi
if [ "$PERCENT" -le  "$WARNING" ]  #如果百分比小于等于用户设置的警告值就警告并返回1
        then
                echo "WARNING - $FREE_MEM MB ($PERCENT%) Free Memory"
                exit 1
fi
if [ "$PERCENT" -gt "$WARNING" ] #如果百分比大于用户设置的警告值就输出OK返回0
        then
                echo "OK - $FREE_MEM MB ($PERCENT%) Free Memory"
                exit 0
fi