wangl 发表于 2015-11-23 07:26:09

nagios 监控内存-增加新插件方法



被监控端

1 在nagios-plugins-nrpe安装目录(如/usr/lib64/nagios/plugins/)添加插件check_mem.sh





#!/bin/bash
USAGE=&quot;`basename $0` [-w|--warning]<percent free> [-c|--critical]<percent free>&quot;
THRESHOLD_USAGE=&quot;WARNING threshold must be greater than CRITICAL: `basename $0` $*&quot;
calc=/tmp/memcalc
percent_free=/tmp/mempercent
critical=&quot;&quot;
warning=&quot;&quot;
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
# print usage
if [[ $# -lt 4 ]]
then
echo &quot;&quot;
echo &quot;Wrong Syntax: `basename $0` $*&quot;
echo &quot;&quot;
echo &quot;Usage: $USAGE&quot;
echo &quot;&quot;
exit 0
fi
# read input
while [[ $# -gt 0 ]]
do
case &quot;$1&quot; in
-w|--warning)
shift
warning=$1
;;
-c|--critical)
shift
critical=$1
;;
esac
shift
done
# verify input
if [[ $warning -eq $critical || $warning -lt $critical ]]
then
echo &quot;&quot;
echo &quot;$THRESHOLD_USAGE&quot;
echo &quot;&quot;
echo &quot;Usage: $USAGE&quot;
echo &quot;&quot;
exit 0
fi
# Total memory available
total=`free -m | head -2 |tail -1 |gawk '{print $2}'`
# Total memory used
used=`free -m | head -2 |tail -1 |gawk '{print $3}'`
# Calc total minus used
free=`free -m | head -2 |tail -1 |gawk '{print $2-$3}'`
# normal values
#echo &quot;$total&quot;MB total
#echo &quot;$used&quot;MB used
#echo &quot;$free&quot;MB free
# make it into % percent free = ((free mem / total mem) * 100)
echo &quot;5&quot; > $calc # decimal accuracy
echo &quot;k&quot; >> $calc # commit
echo &quot;100&quot; >> $calc # multiply
echo &quot;$free&quot; >> $calc # division integer
echo &quot;$total&quot; >> $calc # division integer
echo &quot;/&quot; >> $calc # division sign
echo &quot;*&quot; >> $calc # multiplication sign
echo &quot;p&quot; >> $calc # print
percent=`/usr/bin/dc $calc|/bin/sed 's/^\./0./'|/usr/bin/tr &quot;.&quot; &quot; &quot;|/usr/bin/gawk {'print $1'}`
#percent1=`/usr/bin/dc $calc`
#echo &quot;$percent1&quot;
if [[ &quot;$percent&quot; -le$critical ]]
then
echo &quot;CRITICAL - $free MB ($percent%) Free Memory&quot;
exit 2
fi
if [[ &quot;$percent&quot; -le$warning ]]
then
echo &quot;WARNING - $free MB ($percent%) Free Memory&quot;
exit 1
fi
if [[ &quot;$percent&quot; -gt$warning ]]
then
echo &quot;OK - $free MB ($percent%) Free Memory&quot;
exit 0
fi

2.chmod a&#43;xcheck_mem.sh





3. vi /etc/nagios/nrpe.cfg增加command

command=/usr/lib64/nagios/plugins/check_mem.sh
-w 10 -c 5







4. 在nagios服务器端/etc/nagios/objects/XXX.cfg添加服务

define service{
use generic-service
host_name 8.25.218.26
service_description check_mem
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_command check_nrpe!check_mem
}

5. 验证配置: nagios-v /etc/nagios/nagios.cfg





6. 重启nagios:
/etc/rc.d/init.d/nagios restart
页: [1]
查看完整版本: nagios 监控内存-增加新插件方法