附件:
check_mem
#script to check real memory usage
# L.Gill 02/05/06 - V.1.0
# ------------------------------------------
# ######## Script Modifications ##########
# ------------------------------------------
# Who When What
# --- ---- ----
# LGill 17/05/06 "$percent" lt 1% fix - sed edits dc result beggining with "."
#
#
#!/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 $4+$7}'`
# 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. 在nagios客户端:
cd /usr/local/nagios/libexec
vim check_mem
把附件内容添加到文件中,这样监控内存的程序就做好了。
赋予权限:
把check_mem(见附件)放到/usr/local/nagios/libexec/下,并赋予权限
chmod +x /usr/local/nagios/libexec/check_mem
chown nagios.nagios /usr/local/nagios/libexec/check_mem
2. 编辑nrpe配置文件
[root@localhost libexec]# vim /usr/local/nagios/etc/nrpe.cfg
添加:
command[check_mem]=/usr/local/nagios/libexec/check_mem -w 200 -c 150
保存退出
3. 到 nagios 的server端做配置
配置commands.cfg
[root@localhost nrpe-2.12]# vim /usr/local/nagios/etc/objects/commands.cfg
在swap项目之后添加:
define command{
command_name check_mem
command_line $USER1$/check_mem -w $ARG1$ -c $ARG2$
}
保存退出
配置 services.cfg
[root@localhost nrpe-2.12]# vim /usr/local/nagios/etc/objects/services.cfg
在swap项目之后添加:
define service{
host_name nagios-server
service_description check-memory
check_command check_nrpe!check_mem
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
4. 重启nagios
/etc/init.d/nagios restart
阅读(5455) | 评论(0) | 转发(0) |