【www.bbyears.com--ios】
说明
工作包括两部分监控端(一二三)和被监控端(四)
一、nrpe.cfg中添加脚本
nrpe.cfg中添加命令索引
command[check_used_mem]=/usr/local/nagios/libexec/check_used_mem.sh 80 90
说明:实际内存使用量超过80%警告;超过90%严重警告
二、添加具体脚本
参考脚本check_used_mem.sh
warn=$1
critical=$2
all=`free | sed -n '2p' | awk '{print $2}'`
used=`free | sed -n '3p' | awk '{print $3}'`
let "c=$used*100/$all"
if [[ $c -lt $warn ]]
then
echo "used mem/total < $warn% [used:$used, total:$total:$all]"
exit 0
elif [[ $c -lt $critical ]]
then
echo "used mem/total ≥$warn% [used:$used, total:$total:$all]"
exit 1
else
echo "used mem/total ≥$critical% [used:$used, total:$total:$all]"
exit 2
fi
解释:
1. 1、2分别指输入的第一、二个参数,比如:
del.sh脚本内容为
#/bin/bash
echo $1
sh del.sh a #第一个参数是a
chmod a+x ./del.sh a #第一个参数是a
2. free用来查看内存的使用情况
[root@xen_202_12 /]# free -m
total used free shared buffers cached
Mem: 3072 2459 612 0 207 1803
-/+ buffers/cache: 447 2624
Swap: 1913 0 1913
total 内存总数: 3072
used 已经使用的内存数: 2459
free 空闲的内存数: 612
shared 当前已经废弃不用,总是0
buffers: Buffer Cache内存数: 13220
cached: Page Cache内存数: 2720160
关系:total = used + free
第3行:
-/+ buffers/cache的意思:
-buffers/cache 的内存数: 447 (等于第1行的 used - buffers - cached)
+buffers/cache 的内存数: 2624 (等于第1行的 free + buffers + cached)
注:此处的内存数在用上面式子计算后,在大小上有一点点出入(还不知道是什么原因)。
可见-buffers/cache反映的是被程序实实在在吃掉的内存,而+buffers/cache反映的是可以挪用的内存总数。
3. sed -n '2p'指的是查找第二行
4. awk '{print $2} 指的是第二列,其中默认以空格分隔。可以利用-F指定分隔符
$echo 1b234b56b7 | awk -F 'b' '{print $2}'
234
5. 实用[[]],<用来比较字符串;-lt用来比较数字
三、重启nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
四、在监控端添加监控项
define service {
use generic-service
host_name 100.61.73.2,100,61,73.3
service_description 内存
check_command check_nrpe!check_used_mem
notifications_enabled 1
}
在nagios中使用python脚本监控linux主机
在被监控端192.168.5.110
1.先把getload.py放到/usr/local/nagios/libexec内
[root@nhserver1 ~]# vim /usr/local/nagios/libexec/getload.py
#! /usr/bin/env python
import os,sys
(d1,d2,d3) = os.getloadavg()
if d1 >= 5.0:
print "GETLOADAVG CRITICAL: Load average is %.2f" % (d1)
sys.exit(2)
elif d1 >= 2.0:
print "GETLOADAVG WARNING: Load average is %.2f" %(d1)
else:
print "GETLOADAVG OK: Load average is %.2f" %(d1)
[root@nhserver1 libexec]# chmod a+x getload.py
[root@nhserver1 libexec]# chown nagios:nagios getload.py
2. 在nrpe内加入自定义的命令
[root@nhserver1 libexec]# vim /usr/local/nagios/etc/nrpe.cfg
command[nh_check_getload]=/usr/local/nagios/libexec/getload.py
------------------------------------------------------------------------------------------
在nagios服务端测试192.168.5.10
[root@nhserver2 libexec]# /usr/local/nagios/libexec/check_nrpe -H 192.168.5.110 -c nh_check_getload
GETLOADAVG OK: Load average is 0.06
在服务端测试192.168.5.10的nagios中加入自定义脚本
[root@nhserver2 ~]# cd /usr/local/nagios/etc/objects
[root@nhserver2 objects]# vim hosts_192.168.5.110.cfg
define host{
use linux-server
host_name 192.168.5.110
alias 192.168.5.110
address 192.168.5.110
}
define hostgroup{
hostgroup_name nh_linuxs
alias nh_linuxs
members 192.168.5.110
}
define service{
use local-service
host_name 192.168.5.110
service_description check-host-alive
check_command check-host-alive
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
}
define service{
use local-service
host_name 192.168.5.110
service_description SSH
check_command check_ssh
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
}
define service{
use local-service
host_name 192.168.5.110
service_description check_nrpe_check_users
check_command check_nrpe!nh_check_users
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
}
define service{
use local-service
host_name 192.168.5.110
service_description check_nrpe_check_getload
check_command check_nrpe!nh_check_getload
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
}
[root@nhserver2 objects]# service nagios reload
在nagios的services中就可看到的状态了
192.168.5.110
check_nrpe_check_getload
OK 04-17-2014 16:21:53 0d 0h 4m 22s 1/5 GETLOADAVG OK: Load average is 0.00