前面已经做好了nagios的核心配置,接着来配置下pnp4nagios,使nagios收集的监控数据可以以图标的形式显示。

    pnp4nagios是一款强大的工具,它可以通过process_perfdata.pl脚本调用rrdtool对nagios的性能数据进行分析并绘制成相应的性能图。

    pnp4nagios有5种工作模式,它们分别是Synchronous Mode、Bulk Mode、Bulk Mode with NPCD、Bulk Mode with NPCD and npcdmod和Gearman Mode,这里只对用的模式做配置。详细说明可参考http://www.kar-chan.com/?p=2267

    1.创建默认配置文件

cd /usr/local/pnp4nagios/etc
cp misccommands.cfg-sample misccommands.cfg
cp nagios.cfg-sample nagios.cfg
cp rra.cfg-sample rra.cfg
cd pages
cp web_traffic.cfg-sample web_traffic.cfg
cd ../check_commands/
cp check_all_local_disks.cfg-samplecheck_all_local_disks.cfg
cp check_nrpe.cfg-sample check_nrpe.cfg
cp check_nwstat.cfg-sample check_nwstat.cfg
cp /usr/local/pnp4nagios/libexec/* /usr/local/nagios/libexec/

    2.1nagios4.x以前版本

将nagios主配置文件对应内容为以下参数

vim /usr/local/nagios/etc/nagios.cfg
enable_environment_macros=1
process_performance_data=1          
host_perfdata_command=process-host-perfdata
service_perfdata_command=process-service-perfdata

 2.2 nagios4.x以后版本需要采用Bulk Mode模式

否则会报如下错误

wKioL1YWRFzw2Bs4AAF5az_f5vU246.jpg

将nagios主配置文件对应内容为以下参数

wKiom1YWRXLynrJAAAF6rXqRylI418.jpg

wKioL1YWRYmRzVrNAAGn6jxc_2E931.jpg

wKiom1YWRXLTFb62AARxNnXNBts653.jpg

enable_environment_macros=1
process_performance_data=1 

host_perfdata_file=/usr/local/pnp4nagios/var/host-perfdata
service_perfdata_file=/usr/local/pnp4nagios/var/service-perfdata

host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$
service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::$SERVICEDESC$\tSERVICEPERFDATA::$SERVICEPERFDATA$\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$\tSERVICEOUTPUT::$SERVICEOUTPUT$

host_perfdata_file_mode=a
service_perfdata_file_mode=a

host_perfdata_file_processing_interval=15
service_perfdata_file_processing_interval=15

host_perfdata_file_processing_command=process-host-perfdata-file
service_perfdata_file_processing_command=process-service-perfdata-file

    

    2.3 修改commands.cfg文件

vim  /usr/local/nagios/etc/objects/commands.cfg

define command{
       command_name    check_nrpe
       command_line    $USER1$/check_nrpe-H $HOSTADDRESS$ -c $ARG1$
}

define command{
       command_name    process-service-perfdata-file
       command_line    /usr/local/pnp4nagios/libexec/process_perfdata.pl --bulk=/usr/local/pnp4nagios/var/service-perfdata
}

define command{
       command_name    process-host-perfdata-file
       command_line    /usr/local/pnp4nagios/libexec/process_perfdata.pl --bulk=/usr/local/pnp4nagios/var/host-perfdata
}

wKioL1YWRs2i0IhmAAHwbW-7_rA907.jpg

wKiom1YWRrbSzN86AAHIUG2uv8E762.jpg


    3.1创建监控主机模板和服务模板

vim /usr/local/nagios/etc/objects/templates.cfg
define host {
  name       host-pnp
  action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
  register  0
}
define service {
  name       service-pnp
  action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
  register  0
}

也可以添加在,其他参数下面省略了,下面这个方法可以减少很多配置主机启用pnp时的时间
vim /usr/local/nagios/etc/objects/templates.cfg
define host {

        action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
}
define service {

       action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$  
}

    3.2添加主机配置 方法一

默认nagios/etc/objects/ 下面没有 service.cfg host.cfg hostgroup.cfg 这几个配置文件,需要手动添加

vim hosts.cfg
define host{
         use                      linux-server,host-pnp #这个是根据templates.cfg信息定义,如果上面定义的模板host-pnp添加在define host和define sevice里面,这儿host-pnp可以不用加,因为linux-server已经包含了
         host_name                cacti                 #必须是 被监控的主机名
         alias                    cacti-web             #别名随便定义
         address                  10.30.20.10           #主机ip地址
         contact_groups           admins                #邮件组,下面会演示
}
define host{
         use                      linux-server,host-pnp 
         host_name                nginx                 
         alias                    nginx-web             
         address                  10.30.20.11           
         contact_groups           admins                
}

有多少机器就这样添加多少台

vim hostgroup.cfg
define hostgroup{
         hostgroup_name      servers                   #组名
         alias               servers_group             #别名
         members             cacti,nginx               #主机名 多个 逗号 隔开
}
vim service.cfg          #所有主机在同一配置文件,很乱
#### set cacti host  
define service{
         use                  local-service,services-pnp
         host_name            cacti
         service_description  http
         check_command        check_http
         contact_groups       admins
         flap_detection_enabled          0
}

define service{
         use                   local-service,services-pnp
         host_name             cacti
         service_description   SSH_port
         check_command         check_tcp!22 
         contact_groups        admins
         flap_detection_enabled          0
}
define service{
       use                      local-service,services-pnp
       host_name                cacti
       service_description      check_/
       check_command            check_nrpe!check_/  #使用nrpe检测,客户端需要定义
       contact_groups           admins
       flap_detection_enabled   0
}

#### set nginx host

define service{
        use                      local-service,service-pnp
        host_name                nginx
        service_description      Check_free_mem
        check_command            check_nrpe!check_free_mem
        contact_groups           admins
        flap_detection_enabled   0
}
define service{
       use                      local-service,services-pnp
       host_name                nginx
       service_description      check_/
       check_command            check_nrpe!check_/  #使用nrpe检测,客户端需要定义
       contact_groups           admins
       flap_detection_enabled   0            
}

有多少就需要添加多少,第一种方法 end


    3.3添加主机配置,方法二

    按主机组或业务划分,创建对应文件夹的方式,如将web业务统一放到web文件夹下

cd nagios/etc/objects/
mkdir web
cd web
vim web1.cfg
###定义host

define host{
         use                      linux-server,host-pnp #这个是根据templates.cfg信息定义,如果上面定义的模板host-pnp添加在define host和define sevice里面,这儿host-pnp可以不用加,因为linux-server已经包含了
         host_name                nginx                 #必须是 被监控的主机名
         alias                    nginx-web             #别名随便定义
         address                  10.30.20.11           #主机ip地址
         contact_groups           admins                #邮件组,下面会演示
}

###定义service

define service{
        use                      local-service,service-pnp
        host_name                nginx
        service_description      Check_free_mem
        check_command            check_nrpe!check_free_mem
        contact_groups           admins
        flap_detection_enabled   0
}

define service{
       use                      local-service,services-pnp
       host_name                nginx
       service_description      check_/
       check_command            check_nrpe!check_/  #使用nrpe检测,客户端需要定义
       contact_groups           admins
       flap_detection_enabled   0            
}
vim web2.cfg
###定义host

define host{
         use                      linux-server,host-pnp #这个是根据templates.cfg信息定义,如果上面定义的模板host-pnp添加在define host和define sevice里面,这儿host-pnp可以不用加,因为linux-server已经包含了
         host_name                cacti                 #必须是 被监控的主机名
         alias                    cacti-web             #别名随便定义
         address                  10.30.20.10           #主机ip地址
         contact_groups           admins                #邮件组,下面会演示
}

###定义service

define service{
        use                      local-service,service-pnp
        host_name                cacti
        service_description      Check_free_mem
        check_command            check_nrpe!check_free_mem
        contact_groups           admins
        flap_detection_enabled   0
}

define service{
        use                      local-service,service-pnp
        host_name                cacti
        service_description      Check_free_mem
        check_command            check_nrpe!check_free_mem
        contact_groups           admins
        flap_detection_enabled   0
}

最后将监控文件添加到主配置文件后 重启nagios服务即生效,添加方式如下

vim /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg

cfg_file=/usr/local/nagios/etc/objects/web/*.cfg
#cfg_dir=/usr/local/nagios/etc/objects/web    与上面*.cfg的配置方式等同