一、监控本地(localhost)内存
1、上传监控脚本 check_mem 到/usr/local/nagios/libexec# chown -R nagios.nagios check_mem
# chmod +x check_mem
2、修改commands配置# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check_mem
command_line $USER1$/check_mem -w $ARG1$ -c $ARG2$
}
3、修改localhost.cfg# vim /usr/local/nagios/etc/objects/localhost.cfg
define service{
use local-service
host_name localhost
service_description check_mem
check_command check_mem!20!10
}
4、重启nagios服务# service nagios restart
5、check_mem 脚本#!/bin/bash
USAGE="`basename $0` [-w|--warning] [-c|--critical]"
THRESHOLD_USAGE="WARNING threshold must be greater than CRITICAL: `basename $0` $*"
calc=/tmp/memcalc
percent_free=/tmp/mempercent
critical=""
warning=""
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
# print usage
if [[ $# -lt 4 ]]
then
echo ""
echo "Wrong Syntax: `basename $0` $*"
echo ""
echo "Usage: $USAGE"
echo ""
exit 0
fi
# read input
while [[ $# -gt 0 ]]
do
case "$1" 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 ""
echo "$THRESHOLD_USAGE"
echo ""
echo "Usage: $USAGE"
echo ""
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 "$total"MB total
#echo "$used"MB used
#echo "$free"MB free
# make it into % percent free = ((free mem / total mem) * 100)
echo "5" > $calc # decimal accuracy
echo "k" >> $calc # commit
echo "100" >> $calc # multiply
echo "$free" >> $calc # division integer
echo "$total" >> $calc # division integer
echo "/" >> $calc # division sign
echo "*" >> $calc # multiplication sign
echo "p" >> $calc # print
percent=`/usr/bin/dc $calc|/bin/sed 's/^\./0./'|/usr/bin/tr "." " "|/usr/bin/gawk {'print $1'}`
#percent1=`/usr/bin/dc $calc`
#echo "$percent1"
if [[ "$percent" -le $critical ]]
then
echo "CRITICAL - $free MB ($percent%) Free Memory"
exit 2
fi
if [[ "$percent" -le $warning ]]
then
echo "WARNING - $free MB ($percent%) Free Memory"
exit 1
fi
if [[ "$percent" -gt $warning ]]
then
echo "OK - $free MB ($percent%) Free Memory"
exit 0
fi
二、监控客户端内存使用情况
1、上传监控脚本 check_mem 到/usr/local/nagios/libexec# chown -R nagios.nagios check_mem
# chmod +x check_mem
2、修改nrpe.cfg# vim /usr/local/nagios/etc/nrpe.cfg
command[check_mem]=/usr/local/nagios/libexec/check_mem -w $ARG1$ -c $ARG2$ #增加此行
3、在nagios服务器端增加监控服务# vim /usr/local/nagios/etc/servers/192.168.200.111.cfg
define service{
use generic-service
host_name 192.168.200.111
service_description check_mem
check_command check_nrpe_args!check_mem!20!10 //check_nrpe_args命令,是根据你设置的Nagios远程执行插件(NRPE)而来;
}
三、报错。
1、在使用Nagios监控时出现了小问题在进行系统的内存监控时,以前的监控脚本一直没有问题,但是新服务器上一直无法得到正常的监控数据,后来在Nagios的客户端直接执行相关命令时发现报以下错误:/usr/local/nagios/libexec/check_mem -w 2 -c 1
/usr/local/nagios/libexec/check_mem: line 78: /usr/bin/dc: 没有那个文件或目录
没有dc命令,这是什么情况。网上查找了一翻也未能解决问题,最后在一篇文章找到一些线索,并完解决!
原来dc 命令是系统自带的一个计算相关的命令,但是Centos6.3却没有默认安装,那怎么安装上去呢,使用yum install dc 报没有dc这个安装包
最后只能google一下,原来bc与dc是两个相进的命令,而且dc 命令包含在bc安装包里。解决办法:yum install bcdc -V
dc (GNU bc 1.06.95) 1.3.95
Copyright 1994, 1997, 1998, 2000, 2001, 2004, 2005, 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
问题解决!
本站部分资源收集于网络,纯个人收藏,无商业用途,如有侵权请及时告知!