nagios服务端默认没有check_nrpe插件,需要编译安装nrpe。


nrpe提供check_nrp 以及 nrpe二进制程序和启动进程所需的配置文件。


对于nagios服务端来说,其只需要check_nrpe插件来发送监控指令,而被监控端则需要通过nrpe进程来监控服务端发送过来的指令(默认监听5666),并执行本地插件获取信息,最后回传给监控端。由此可见,被监控端需要安装nagios-plugins来获取插件,但如果被监控端不使用由plugins组件提供的插件,当然也就不需要安装了。


nrpe的编译安装总结:

默认使用用户nagios和组nagios,最好提前创建

[root@localhostnrpe-2.15]#./configure--prefix=/mnt#测试目录[root@localhostnrpe-2.15]#[root@localhostnrpe-2.15]#makeall[root@localhostnrpe-2.15]#makeinstall[root@localhostnrpe-2.15]#

[root@localhostnrpe-2.15]#tree/mnt/mnt├──bin│└──nrpe└──libexec└──check_nrpe

已经生成了二进制文件和插件,对于nagios服务端来说,此步之后,直接拷贝check_nrpe到nagios插件存放目录即可(注意可执行权限)。


而对于被监控端来说,check_nrpe插件是没有用的,而是需要nrpe来提供监听服务,但此时安装目录并没有配置文件:

[root@localhostnrpe-2.15]#makeinstall-daemon-config/usr/bin/install-c-m775-onagios-gnagios-d/mnt/etc/usr/bin/install-c-m644-onagios-gnagiossample-config/nrpe.cfg/mnt/etc

可以手动将源码目录下的配置文件 sample-config/nrpe.cfg 拷贝到安装目录,并修改下属主和属组即可。


简单看下配置文件,需要修改allowed_hosts,将nagios服务端ip添加到后边,逗号隔开即可:

server_port=5666#server_address=127.0.0.1nrpe_user=nagiosnrpe_group=nagiosallowed_hosts=127.0.0.1,$NagiosIP

有了配置文件就可以启动nrpe进程了,有两种方式,一种是独立守护进程,一种超级守护进程的方式。

独立守护进程:

[root@localhostnrpe-2.15]#[root@localhostnrpe-2.15]#netstat-antp|grep5666[root@localhostnrpe-2.15]#/mnt/bin/nrpe-c/mnt/etc/nrpe.cfg-n-d[root@localhostnrpe-2.15]#netstat-antp|grep5666tcp000.0.0.0:56660.0.0.0:*LISTEN13643/nrpetcp00:::5666:::*LISTEN13643/nrpe[root@localhostnrpe-2.15]#

-c 指定配置文件

-n 不使用ssl,默认是使用的,编译时有参数--enable-ssl可以加上,应该是默认选项

如果使用了 -n, nagios监控端在使用check_nrpe时也需要指定此参数。

-d 已独立守护进程方式启动


超级守护进程,需要inetd or xinetd,在编辑相关配置文件时,指定“-i”参数即可。


为了启动方便,将服务添加到chkconfig

参考脚本:

#!/bin/bash#chkconfig:357822LOCK_FILE='/var/lock/subsys/nrped'NRPE_COMMAND='/usr/local/nrpe/bin/nrpe'NRPE_CONFIGURE_FILE='/usr/local/nrpe/etc/nrpe.cfg'start(){echo-n"startingnrped..."$NRPE_COMMAND-c$NRPE_CONFIGURE_FILE-d&>/dev/null&&touch$LOCK_FILE&>/dev/null[[$?-eq0]]&&echo-e"\033[1;32mOK\033[0m"||echo-e"\033[1;31mFAIL\033[0m"}stop(){echo-n"stoppingnrped..."killallnrpe&>/dev/null&&rm-f$LOCK_FILE&>/dev/ull[[$?-eq0]]&&echo-e"\033[1;32mOK\033[0m"||echo-e"\033[1;31mFAIL\033[0m"}status(){[[-f$LOCK_FILE]]&&echo"nrpeisrunning..."||echo"nrpeisn'trunning..."}case$1instart)start;;stop)stop;;restart)stop;start;;status)status;;esac


后期处理和测试:

[root@localhostnrpe-2.15]#chmod+x/etc/init.d/nrped[root@localhostnrpe-2.15]#chkconfig--addnrped[root@localhostnrpe-2.15]#chkconfig--list|grepnrpednrped0:off1:off2:off3:on4:off5:on6:off[root@localhostnrpe-2.15]#servicenrpedstartstartingnrped...OK[root@localhostnrpe-2.15]#servicenrpedstopstoppingnrped...OK[root@localhostnrpe-2.15]#servicenrpedrestartstoppingnrped...FAILstartingnrped...OK[root@localhostnrpe-2.15]#servicenrpedrestartstoppingnrped...OKstartingnrped...OK[root@localhostnrpe-2.15]#servicenrpedstatusnrpeisrunning...[root@localhostnrpe-2.15]#netstat-antp|grepnrpetcp000.0.0.0:56660.0.0.0:*LISTEN13940/nrpetcp00:::5666:::*LISTEN13940/nrpe[root@localhostnrpe-2.15]#


关于被监控端插件:

nagios监控端可以“远程”调用的指令需要在被监控端nrpe配置文件中提前定义,如下:

command[check_users]=/mnt/libexec/check_users-w5-c10command[check_load]=/mnt/libexec/check_load-w15,10,5-c30,25,20command[check_hda1]=/mnt/libexec/check_disk-w20%-c10%-p/dev/hda1command[check_zombie_procs]=/mnt/libexec/check_procs-w5-c10-sZcommand[check_total_procs]=/mnt/libexec/check_procs-w150-c200#command[check_users]=/mnt/libexec/check_users-w$ARG1$-c$ARG2$#command[check_load]=/mnt/libexec/check_load-w$ARG1$-c$ARG2$#command[check_disk]=/mnt/libexec/check_disk-w$ARG1$-c$ARG2$-p$ARG3$#command[check_procs]=/mnt/libexec/check_procs-w$ARG1$-c$ARG2$-s$ARG3$

可以定义阈值明确的指令也可以留有“变量”供监控端指定。

以上指令都是安装nagios-plugins后生成的指令,现在用shell手动编写一个简单的监控插件。

开发插件需要符合以下规则:

插件需要一个返回值和一段输出,而且nagios只接受第一段输出。

返回值和nagios五种状态的对应关系如下:

0: OK

1: WARNNING

2: CRITICAL

3: UNKNOWN

4: PENDING


脚本功能是简单的监控本地内存, 代码如下:

#!/bin/bash#Author:LinigYi#"Memorycheck"OK=0WARNNING=1CRITICAL=2UNKNOWN=3PENDING=4mem_string=$(free-m|grepMem)totle=$(echo$mem_string|awk'{print$2}')used=$(echo$mem_string|awk'{print$3}')free=$(echo$mem_string|awk'{print$4}')used_percent_string=$(echo"scale=3;${used}/${totle}"|bc)used_percent_aa=${used_percent_string:1:2}used_percent_bb=${used_percent_string:3:1}if[[$used_percent_bb-ge5]];thenused_percent=$((used_percent_aa+=1))elseused_percent=${used_percent_aa}fi[[$1=='-w']]&&shift&&warnning=$1&&shift[[$1=='-c']]&&shift&&critical=$1[[$warnning-gt$critical]]&&{echovaluewrong!!exit$PENDING}if[[$used_percent-ge$critical]];thenechoCRITICAL-Totle:$totleUsed:$usedFree:$free,usedpercent:${used_percent}%exit$CRITICALelif[[$used_percent-ge$warnning]];thenechoWARNNING-Totle:$totleUsed:$usedFree:$free,usedpercent:${used_percent}%exit$WARNNINGelseechoOK-Totle:$totleUsed:$usedFree:$free,usedpercent:${used_percent}%exit$OKfi


基本模拟plugins的插件输出格式

测试插件:

首先修改配置文件,将nagios服务端ip添加到允许列表:

[root@localhostmnt]#cat/mnt/etc/nrpe.cfg|grepallowed_hostsallowed_hosts=127.0.0.1,172.19.2.250[root@localhostmnt]#

[root@localhostmnt]#ls-l/mnt/libexec/total240-rwxr-xr-x.1rootroot160049Apr2218:10check_load-rwxr-xr-x.1rootroot1098Apr2218:14check_mem-rwxrwxr-x.1nagiosnagios76825Apr2216:54check_nrpe[root@localhostmnt]#

插件一定要有可执行权限! 拷贝一个对比的插件check_load

[root@localhostmnt]#cat/mnt/etc/nrpe.cfg|grep'^command'command_timeout=60command[check_users]=/mnt/libexec/check_users-w5-c10command[check_load]=/mnt/libexec/check_load-w15,10,5-c30,25,20command[check_hda1]=/mnt/libexec/check_disk-w20%-c10%-p/dev/hda1command[check_zombie_procs]=/mnt/libexec/check_procs-w5-c10-sZcommand[check_total_procs]=/mnt/libexec/check_procs-w150-c200command[check_mem]=/mnt/libexec/check_mem-w20-c40[root@localhostmnt]#

最后一个 check_mem 指令为自己定义。

启动nrpe

[root@localhostmnt]#[root@localhostmnt]#killallnrpe[root@localhostmnt]#netstat-antp|grepnrpe[root@localhostmnt]#/mnt/bin/nrpe-c/mnt/etc/nrpe.cfg-d[root@localhostmnt]#netstat-antp|grepnrpetcp000.0.0.0:56660.0.0.0:*LISTEN14831/nrpetcp00:::5666:::*LISTEN14831/nrpe[root@localhostmnt]#


在nagios服务端做测试:

[root@localhostlibexec]#ipaddshow1:lo:<LOOPBACK,UP,LOWER_UP>mtu16436qdiscnoqueuestateUNKNOWNlink/loopback00:00:00:00:00:00brd00:00:00:00:00:00inet127.0.0.1/8scopehostloinet6::1/128scopehostvalid_lftforeverpreferred_lftforever2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000link/ether00:1a:4a:94:7d:2ebrdff:ff:ff:ff:ff:ffinet172.19.2.250/24brd172.19.2.255scopeglobaleth0inet6fe80::21a:4aff:fe94:7d2e/64scopelinkvalid_lftforeverpreferred_lftforever[root@localhostlibexec]#[root@localhostlibexec]#[root@localhostlibexec]#pwd/usr/local/nagios/libexec[root@localhostlibexec]#./check_nrpe-H192.168.2.162-ccheck_loadOK-loadaverage:0.00,0.00,0.00|load1=0.000;15.000;30.000;0;load5=0.000;10.000;25.000;0;load15=0.000;5.000;20.000;0;[root@localhostlibexec]#[root@localhostlibexec]#echo$?0[root@localhostlibexec]#[root@localhostlibexec]#./check_nrpe-H192.168.2.162-ccheck_memWARNNING-Totle:3828Used:871Free:2956,usedpercent:23%[root@localhostlibexec]#echo$?1[root@localhostlibexec]#

检测成功 !


现在可以在nagios监控端添加监控服务了 !

附件:http://down.51cto.com/data/2367632