Nagios中nrpe 无法读取check_memory插件的输出

在搭建nagios监控系统,使用check_memory插件时,报错:
NRPE : unable to read output

经过调试,发现是NRPE无法读取check_内存插件的输出。
需要将bash脚本中if语句的[[]]改成[]

插件使用了gawk和bc命令,在系统中安装了gawk和bc命令

apt-get install gawk bc

同时需要给插件赋予可执行权限。chmod 755 check_memory

vi /usr/lib/nagios/plugins/check_memory

# $Id: check_mem,v 1.3 2008/09/17 21:47:03 nagios Exp nagios $
#
#
# $Log: check_mem,v $
# Revision 1.3  2008/09/17 21:47:03  nagios
# dropped free in favor of using /proc/meminfo
#
# Revision 1.2  2008/09/17 21:00:24  nagios
# added usage statement and input validation from http://www.nagiosexchange.org/cgi-bin/page.cgi?g=2099.html;d=1
# respects to the author
#
# Revision 1.1  2008/09/17 20:57:38  nagios
# Initial revision
#
#
#!/bin/bash
USAGE="`basename ##代码##` [-w|--warning]<percent free> [-c|--critical]<percent free>"
THRESHOLD_USAGE="WARNING threshold must be greater than CRITICAL: `basename ##代码##` $*"

# print usage
if [ $# -lt 4 ]
then
	echo ""
	echo "Wrong Syntax: `basename ##代码##` $*"
	echo ""
	echo "Usage: $USAGE"
	echo ""
	exit 0
fi
# read input
while [ $# -gt 0 ]
  do
        case "" in
               -w|--warning)
               shift
               warning=
        ;;
               -c|--critical)
               shift
               critical=
        ;;
        esac
        shift
  done
# verify input
if [[ $warning -eq $critical || $warning -lt $critical ]]
then
	echo ""
	echo "$THRESHOLD_USAGE"
	echo ""
        echo "Usage: $USAGE"
	echo ""
        exit 0
fi

# Total physical memory
total=`cat /proc/meminfo |head -n 1 |tail -n 1| gawk '{print }'`

# Free physical memory
free=`cat /proc/meminfo |head -n 2 |tail -n 1| gawk '{print }'`

# Buffers
buffers=`cat /proc/meminfo |head -n 3 |tail -n 1| gawk '{print }'`

# Cached
cached=`cat /proc/meminfo |head -n 4 |tail -n 1| gawk '{print }'`

#Available physical memory
available=`echo "$free+$buffers+$cached" | bc`

# make it into % percent free = ((free mem / total mem) * 100)
percent=`echo "scale=2; $available/$total*100" | bc`

#echo $total
#echo $free
#echo $buffers
#echo $cached
#echo $available
#echo $percent

#echo $critical
#echo $warning

#echo `echo "$percent <=  $critical"|bc`
#echo `echo "$percent <=  $warning"|bc`
#echo `echo "$percent >  $warning"|bc`

if [ "`echo "$percent <=  $critical"|bc`" -eq 1 ]
	then
		echo "CRITICAL - $available KB ($percent%) Free Memory"
		exit 2
fi
if [ "`echo "$percent <=  $warning"|bc`" -eq 1 ]
        then
                echo "WARNING - $available KB ($percent%) Free Memory"
                exit 1
fi
if [ "`echo "$percent >  $warning"|bc`" -eq 1 ]
        then
                echo "OK - $availabe KB ($percent%) Free Memory"
                exit 0
fi
日期:2019-08-20 17:58:07 来源:oir作者:oir