1、工作原理:
相比与主动模式中服务器主动去被监控机上轮询获取监控数据的方式,被动模式则是在被监控机上面通过插件或脚本获取监控数据,然后将数据通过send_nsca发往监控机,最后监控机通过Nsca接收并解析数据,并传递给Nagios。这样做的一个很大的优势就是将除去处理数据的其他工作都放在了被监控机上面(包括了数据的传输),这样就避免了被监控机数量大时,一次轮询时间过长而导致监控反应延迟,这也是被动模式能承担更大监控量的关键。
在MonitorClient上面,使用nagios-plugins提供的插件,得出监控数据,然后将数据存为一个文件,利用输入重定向,通过send_nsca将数据发往MonitorServer。MonitorServer上面运行一个nsca的daemon(默认开启5667端口),用来接收这些数据,然后做一个简单的处理(会和nagios的service文件进行对应,将多余的监控数据排除),然后将数据进行格式的转换,发给nagios的“外部命令文件”(默认配置为“/usr/local/nagios/var/rw/nagios.cmd”在nagios.cfg中定义的)。该文件是一个管道文件,也是nagios主程序的一个接口(用来接收监控数据),使用cat查看该文件时候,会出来经nsca处理后的数据格式。然后nagios主程序对数据进行处理(前台展示,警报)。
2、组成:
Nsca (安装在MonitorServer上,用来接收并解析MonitorClient发来的监控数据,传递给nagios)
Send_nsca(安装在MonitorClient上,用来发送监控数据。)
3、实现过程:
1)、修改nagios配置文件
vim /usr/local/nagios/etc/nagios.cfg
check_external_commands = 1 (enable commands file)
command_check_interval = -1 (check the external command file as often as possible )
2)、添加模板
vim /usr/local/nagios/etc/objects/template.cfg
define service{
name passive_service
use generic-service
max_check_attempts 3
active_checks_enabled 0
passive_checks_enabled 1
normal_check_interval 5
retry_check_interval 1
notifications_enabled 1
notification_interval 5
notification_period 24x7
contact_groups admins
register 0
}
3)、定义一个command
vim /usr/local/nagios/etc/objects/command.cfg
define command{
command_name check_dummy
command_line /usr/local/nagios/libexec/check_dummy $ARG1$
}
4)、定义一台主机需要被监控的服务
vim /usr/local/nagios/etc/objects/XH_DemoTrade.cfg
define service{
use passive_service
host_name XH_DemoTrade
service_description load
check_command check_dummy
contact_groups admins
}
define service{
use passive_service
host_name XH_DemoTrade
service_description disk
check_command check_dummy
contact_groups admins
}
define service{
use passive_service
host_name XH_DemoTrade
service_description logonService_aufront_common
check_command check_dummy
contact_groups admins
}
define service{
use passive_service
host_name XH_DemoTrade
service_description http
check_command check_dummy
contact_groups admins
}
5)、下载安装nsca
wget http://nchc.dl.sourceforge.net/project/nagios/nsca-2.x/nsca-2.9.1/nsca-2.9.1.tar.gz
tar zxvf nsca-2.9.1.tar.gz
./configure
make all
cp ./src/nsca /usr/local/nagios/bin/
cp ./sample-config/nsca.cfg /usr/local/nagios/etc
chown nagios.nagios /usr/local/nagios/bin/nsca
chown nagios.nagios /usr/local/nagios/etc/nsca.cfg
cp init-script /etc/init.d/nsca
chmod a+x /etc/init.d/nsca
chkconfig --add nsca
6)、编辑nsca配置文件
vim /usr/local/nagios/etc/nsca.cfg
server_address=10.0.100.32 (这里务必使用对外表现的IP)
debug=1 (debug选项即log选项,写入message)
aggregate_writes=1 (能够支持更大的监控量,建议开启)
max_packet_age=60 (数据包过期时间,默认30s,但是考虑到网络因素建议设为60s)
password=xxxxxxx (密码,最基础的加密方式,也可以不设置)
service nagios start
service nsca start
7)、在nagios client端安装nsca服务(其实只需要send_nsca和send_nsca.cfg两个文件,所以可以从nagios server端直接分发到nagios client)
wget http://nchc.dl.sourceforge.net/project/nagios/nsca-2.x/nsca-2.9.1/nsca-2.9.1.tar.gz
tar zxvf nsca-2.9.1.tar.gz
./configure
make all
cp ./src/send_nsca /usr/local/nagios/bin/
cp ./sample-config/send_nsca.cfg /usr/local/nagios/etc
chown nagios.nagios /usr/local/nagios/bin/nsca
chown nagios.nagios /usr/local/nagios/etc/nsca.cfg
8)、在nagios client设置监控脚本,调用libexec中nagios-plugins提供的插件,内容如下,其中JYS_check_proce是自定义的服务
vim /usr/local/nagios/nsca.sh
#!/bin/sh
host_name=$1
svc_name=$2
load_ARG="-w 5 -c 10"
http_ARG="-H 172.18.11.30 -u http://172.18.11.30/index.html"
disk_ARG="-w 20% -c 10% -p /dev/sda"
#echo "$host_name\t$svc_name\n"
case "$2" in
check_load)
output=`/usr/local/nagios/libexec/$2 $load_ARG`
status=`echo $?`
echo "$host_name;load;$status;$output">/tmp/nsca_load.out
cat /tmp/nsca_load.out | /usr/local/nagios/bin/send_nsca -H 10.0.100.32 -d ";" -c /usr/local/nagios/etc/send_nsca.cfg
;;
check_http)
output=`/usr/local/nagios/libexec/$2 $http_ARG`
status=`echo $?`
echo $status
echo "$host_name;http;$status;$output">/tmp/nsca_http.out
cat /tmp/nsca_http.out | /usr/local/nagios/bin/send_nsca -H 10.0.100.32 -d ";" -c /usr/local/nagios/etc/send_nsca.cfg
;;
check_disk)
output=`/usr/local/nagios/libexec/$2 $disk_ARG`
status=`echo $?`
echo "$host_name;disk;$status;$output">/tmp/nsca_disk.out
cat /tmp/nsca_disk.out | /usr/local/nagios/bin/send_nsca -H 10.0.100.32 -d ";" -c /usr/local/nagios/etc/send_nsca.cfg
;;
JYS_check_proce)
output=`/usr/local/nagios/libexec/$2 $3`
status=`echo $?`
echo "$host_name;$3;$status;$output">/tmp/nsca_JYS.out
cat /tmp/nsca_JYS.out | /usr/local/nagios/bin/send_nsca -H 10.0.100.32 -d ";" -c /usr/local/nagios/etc/send_nsca.cfg
;;
esac
9)、在crontab中添加任务
crontab -e
*/5 * * * * /bin/sh /usr/local/nagios/nsca.sh XH_DemoTrade JYS_check_proce logonService_aufront_common
*/5 * * * * /bin/sh /usr/local/nagios/nsca.sh XH_DemoTrade check_http -H 127.0.0.1
*/5 * * * * /bin/sh /usr/local/nagios/nsca.sh XH_DemoTrade check_disk
*/5 * * * * /bin/sh /usr/local/nagios/nsca.sh XH_DemoTrade check_load
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。