9、定义监控的项目,也叫服务,创建services.cfg<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

vi  /usr/local/nagios/etc/services.cfg
#监控主机是否存活
define service{
        host_name               nagios-server
        service_description     check-host-alive
        check_command           check-host-alive
        max_check_attempts      5
        normal_check_interval   5
        retry_check_interval    2
        check_period            24x7
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          sagroup
        }
#监控主机的web服务
define service{
        host_name               nagios-server
        service_description     check_tcp 80
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        check_command           check_tcp!80
        }
#监控主机的cpu负载情况
define service{
        host_name               nagios-server
        service_description     cpu load
        check_command           check_nrpe!check_load
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        }
#监控主机的进程数
define service{
        host_name               nagios-server
        service_description     total-procs
        check_command           check_nrpe!check_total_procs
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        }

 

说明:
host_name:必须是主机配置文件hosts.cfg中定义的主机。

check_command:在commands.cfg文件中定义或在nrpe.cfg里面定义的命令;

max_check_attempts: 最大重试次数,一般设置为4次左右;

normal_check_interval retry_check_interval检查间隔的单位是分钟。
notification_interval   通知间隔指探测到故障后,每隔多长时间发送一次报警信息,单位是分钟。

notification_options 通知选项跟联系人配置文件相同。

contact_groups:配置文件contactgroup.cfg定义的组名称。

注意:check_command选项后面跟的命令一定要在commands.cfg里有定义;
如果要监控其他的主机的信息,可以通过复制并修改想应的选项来进行添加;
通过复制添加下面这两台服务器的监控项目:
win2003linux

 

四、安装 nrpe

tar  xvf  nrpe-2.12.tar.gz
cd  nrpe-2.12.
./configure  --prefix=/usr/local/nrpe
make
make install
#复制文件,因为在nrpe安装目录/usr/local/nrpe/libexec里只有cneck_nrpe这一个文件,而在nagios/libexec里却没有,还有一个就是nrpe.cfg文件里面默认定义的那几个命令后面的路径是放在/usr/local/nrpe/libexec的目录里面,也要把那几个文件复制过来,如果不复制过来的话必须要修改nrpe.cfg里面定义的命令的路径,免得在services.cfg里面定义check_command时提示找不到命令;现在把下面的文件复制过来:

   cp /usr/local/nrpe/libexec/check_nrpe  /usr/local/nagios/libexec

   cp /usr/local/nagios/libexec/check_disk  /usr/local/nrpe/libexec

   cp /usr/local/nagios/libexec/check_load  /usr/local/nrpe/libexec

   cp /usr/local/nagios/libexec/check_ping  /usr/local/nrpe/libexec

   cp /usr/local/nagios/libexec/check_procs  /usr/local/nrpe/libexec

cp /usr/local/nagios/libexec/check_users  /usr/local/nrpe/libexec

 

#修改nrpe配置文件,只把改过的地方写出来
vi  /usr/local/nrpe/etc/nrpe.cfg
server_address=192.168.0.10      //以单独的守护进程运行
allowed_hosts=127.0.0.1,192.168.0.10  //设置允许nagios监控服务器可以访问
command[check_users]=/usr/local/nrpe/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nrpe/libexec/check_load -w 15,10,5 -c 30,25,20
#command[check_hda1]=/usr/local/nrpe/libexec/check_disk -w 20 -c 10 -p /dev/hda1   //注释掉
command[check_df]=/usr/local/nrpe/libexec/check_disk -w 20 -c 10              //添加这一行,监控整个磁盘利用率
command[check_zombie_procs]=/usr/local/nrpe/libexec/check_procs -w 5 -c 10 -s z
command[check_total_procs]=/usr/local/nrpe/libexec/check_procs -w 150 -c 200
command[check_ips]=/usr/local/nrpe/libexec/ip_conn.sh 8000 10000     //监控ip连接数

 

说明:
●     command[check_users]=/usr/local/nrpe/libexec/check_users –w 5 –c 10在默认情况下check_users的插件是放在/usr/local/nrpe/libexec/目录里面,而目录里面在默认情况下是没有这一个文件的,所以说要从/usr/local/nagios/libexec/目录下拷贝一个过来;或者说的它后面的它改成:  command[check_users]=/usr/local/nagios/libexec/check_users –w 5 –c 10 这样的话就可以了,要不然在引用check_users的时候会提示没有那命令;
ps
:我这里为了方便,就是从/usr/local/nagios/libexec下把那几个文件拷贝过来;

●     在上面的nrpe.cfg配置文件里面,在中括号 “ [ ] “里面部分是命令名,也就是check_nrep –c 后面可以接的内容,等号=后面的就是实际执行的插件程序的路径;从上往下分别是检测登录用户数,cpu使用率,磁盘的容量,僵尸进程,总进程,连接数;

●     要是还要添加其它监控项目,不要忘记了在这里定义相应的命令;例:如果要监控主机的swap分区使用情况,当空闲空间小于20%时为警告状态,当空闲空间小于10%时为严重状态。需要在nrpe.cnf里面添加下面的命令:/usr/local/nagios/libexec/check_swap -w 20% -c 10% 如还有其它的,添加相就应的就可以了;关于命令用法可以能过/usr/local/nagios/libexec/check_swap –h这样的命令来查询;

●     command[check_ips]=/usr/local/nrpe/libexec/ip_conn.sh 8000 10000 ip连接数,ip_conn.sh脚本需要自己写,下面给出脚本的内容:

#!/bin/sh

#if [ $#-ne 2 ]

#then

# echo "usage:$0 -w num1 -c num2"

#exit 3

#fi

 

 

ip_conns=`netstat -an |grep tcp |grep est |wc -l`

 

        if [ $ip_conns -lt $1 ]

        then

        echo "ok -connectcounts is $ip_conns"

        exit 0

        fi

 

        if [ $ip_conns -gt $1 -a $ip_conns -lt $2 ]

        then

        echo "warning -connectcounts is $ip_conns"

        exit 1

        fi

 

        if [ $ip_conns -gt $2 ]

        then

        echo "critical -connectcounts is $ip_conns"

        exit 2

        fi

     我在nrpe配置文件nrpe.cfg把脚本所需的两个参数写上了,因此这个脚本就不需判断两个参数输入值的情况。只要当前ip连接数大于8000,系统就发warning报警,超过10000,则发“critical”报警信息。把这个脚本放在目录/usr/local/nrpe/libexec下,并给于执行权限;

注:脚本来自田逸的《开源监控利器nagios

 

修改/usr/local/nagios/etc/objects/commands.cfg,在最后添加以下内容:

########################################################################

# 'check_nrpe ' command definition

define command{

        command_name check_nrpe

        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

        }

添加check_nrpe的命令支持,要不是加的话,在”check_cmmmands   check_nrpe!check_nrpe”这样的情况下的时候,会提示没有check_nrpe这一个命令。