到此在cacti中添加npc插件来调用nagios
[root@localhost plugins]# cd /usr/local/apache/htdocs/cacti/plugins
[root@localhost plugins]# tar -zxvf /install/npc-2.0.4.tar.gz
[root@localhost plugins]# vim ../include/config.php
$plugins[] = "npc";

测试:
http://192.168.1.120/cacti

(15)PNP
[root@localhost ~]# tar -zxvf pnp-0.4.13.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/pnp-0.4.13/
[root@localhost pnp-0.4.13]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-rrdtool --with-perfdata-dir=/usr/local/nagios/share/perfdata
[root@localhost pnp-0.4.13]# make all
[root@localhost pnp-0.4.13]# make install
[root@localhost pnp-0.4.13]# make install-config
[root@localhost pnp-0.4.13]# make install-init
配置pnp
[root@localhost pnp-0.4.13]# cd /usr/local/nagios/etc/pnp/
[root@localhost pnp]# cp process_perfdata.cfg-sample process_perfdata.cfg
[root@localhost pnp]# cp npcd.cfg-sample npcd.cfg
[root@localhost pnp]# cp rra.cfg-sample rra.cfg
[root@localhost pnp]# chown -R nagios:nagios /usr/local/nagios/etc/pnp/
[root@localhost pnp]# vim /usr/local/nagios/etc/pnp/process_perfdata.cfg
LOG_FILE = /usr/local/nagios/var/perfdata.log
# Loglevel 0=silent 1=normal 2=debug
LOG_LEVEL = 2
修改nagios配置文件
1、
[root@localhost pnp]# vim /usr/local/nagios/etc/objects/templates.cfg
define host{
        name            hosts-pnp
        register        0
        action_url      /nagios/pnp/index.php?host=$HOSTNAME$
        process_perf_data
}

##############################################################
define service {
        name            services-pnp
        register        0
        action_url      /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
        process_perf_data
}

2、
[root@localhost pnp]# vim /usr/local/nagios/etc/nagios.cfg
833 process_performance_data=1
845 host_perfdata_command=process-host-perfdata
846 service_perfdata_command=process-service-perfdata

3、
[root@localhost pnp]# vim /usr/local/nagios/etc/objects/commands.cfg
# 'process-host-perfdata' command definition
define command{
        command_name    process-host-perfdata
#       command_line    /usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTI
ONTIME$\t$HOSTOUTPUT$\t$HOSTPERFDATA$\n" >> /usr/local/nagios/var/host-perfdata.out
        command_line    /usr/local/nagios/libexec/process_perfdata.pl
        }


# 'process-service-perfdata' command definition
define command{
        command_name    process-service-perfdata
#       command_line    /usr/bin/printf "%b" "$LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATE$\t$SERVICEATTEMPT$\t$SERV
ICESTATETYPE$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$\n" >> /usr/local/nagios/var/service-perf
data.out
        command_line    /usr/local/nagios/libexec/process_perfdata.pl
        }

4、
[root@localhost pnp]# vim /usr/local/nagios/etc/objects/hosts.cfg
define host{
    use             linux-server,hosts-pnp
    host_name       115-server
    alias           115
    address         192.168.1.115
}

define  host{
    use             linux-server,hosts-pnp
    host_name       120-server
    alias           120
    address         192.168.1.120
}



5、
[root@localhost pnp]# vim /usr/local/nagios/etc/objects/services.cfg
define service{
    use         local-service,services-pnp
    host_name   115-server
    service_description     PING
    check_command       check_ping!100.0,20%!500.0,60%
}
###################################################################
define service{
    use         local-service,services-pnp
    host_name   120-server
    service_description     PING
    check_command       check_ping!100.0,20%!500.0,60%
}

[root@localhost objects]# /etc/init.d/nagios stop
[root@localhost objects]# /etc/init.d/nagios start

httpd://192.168.1.120/nagios




(16)配置Nagios客户端(远程主机)
[root@localhost ~]# yum -y install *gcc*
[root@localhost install]# useradd -s /sbin/nologin nagios
[root@localhost install]# tar -zxvf nagios-plugins-1.4.14.tar.gz -C /usr/src/
[root@localhost install]# cd /usr/src/nagios-plugins-1.4.14/
[root@localhost nagios-plugins-1.4.14]# ./configure
[root@localhost nagios-plugins-1.4.14]# make
[root@localhost nagios-plugins-1.4.14]# make install
[root@localhost nagios-plugins-1.4.14]# chown nagios:nagios /usr/local/nagios/
[root@localhost nagios-plugins-1.4.14]# chown -R nagios:nagios /usr/local/nagios/libexec/

安装NRPE
[root@localhost install]# tar -zxvf nrpe-2.12.tar.gz -C /usr/src/
[root@localhost install]# cd /usr/src/nrpe-2.12/
[root@localhost nrpe-2.12]# ./configure
[root@localhost nrpe-2.12]# make all
[root@localhost nrpe-2.12]# make install-plugin
[root@localhost nrpe-2.12]# make install-daemon
[root@localhost nrpe-2.12]# make install-daemon-config
root@localhost ~]# cd /usr/local/nagios/libexec/
[root@localhost libexec]# ls
check_apt      check_disk_smb  check_ide_smart     check_load      check_nrpe      check_ping     check_spop   check_users
check_breeze   check_dns       check_ifoperstatus  check_log       check_nt        check_pop      check_ssh    check_wave
check_by_ssh   check_dummy     check_ifstatus      check_mailq     check_ntp       check_procs    check_ssmtp  negate
check_clamd    check_file_age  check_imap          check_mrtg      check_ntp_peer  check_real     check_swap   urlize
check_cluster  check_flexlm    check_ircd          check_mrtgtraf  check_ntp_time  check_rpc      check_tcp    utils.pm
check_dhcp     check_ftp       check_jabber        check_nagios    check_nwstat    check_sensors  check_time   utils.sh
check_dig      check_http      check_ldap          check_nntp      check_oracle    check_simap    check_udp
check_disk     check_icmp      check_ldaps         check_nntps     check_overcr    check_smtp     check_ups

[root@localhost libexec]# vim /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,192.168.1.120   填写监控主机IP或域名

[root@localhost libexec]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@localhost libexec]# echo $?
0

[root@localhost libexec]#  netstat -ant | grep 5666
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN

[root@localhost libexec]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
NRPE v2.12

设置开机自动启动服务
[root@localhost nrpe-2.12]# echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >>/etc/rc.local 


[root@localhost libexec]# vim /usr/local/nagios/etc/nrpe.cfg
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 500 -c 550
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%

在服务端安装NRPE和配置nagios
[root@localhost ~]# tar -zxvf nrpe-2.12.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nrpe-2.12/
[root@localhost nrpe-2.12]# ./configure
[root@localhost nrpe-2.12]# make all
[root@localhost nrpe-2.12]# make install-plugin
测试插件于客户端是否通信
[root@localhost nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.115
NRPE v2.12

定义一个check_nrpe命令
[root@localhost nrpe-2.12]# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }

添加远程主机监控  在这我们监控远程主机服务:用户数、CPU利用率
[root@localhost nrpe-2.12]# vim /usr/local/nagios/etc/objects/services.cfg
define service{
    use                         local-service,services-pnp
    host_name                   125-server
    service_description         users
    check_command               check_nrpe!check_users
}

define service{
    use                         local-service,services-pnp
    host_name                   125-server
    service_description         load
    check_command               check_nrpe!check_load
}
 
define service{
    use                         local-service,services-pnp
    host_name                   125-server
    service_description         swap
    check_command               check_nrpe!check_swap
}



define service{
    use                         local-service,services-pnp
    host_name                   125-server
    service_description         Total Proocesses
    check_command               check_nrpe!check_total_procs
}


define service{
    use                         local-service,services-pnp
    host_name                   125-server
    service_description         Zombie Proocesses
    check_command               check_nrpe!check_zombie_procs
}







[root@localhost nrpe-2.12]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
[root@localhost nrpe-2.12]# service nagios stop
[root@localhost nrpe-2.12]# service nagios start
Starting nagios:This account is currently not available.
 done.


http://192.168.1.120/nagios
-----------------------------------------------------------------------------------------------------------------------------------------------------------

(17)实现Nagios短信报警
下载linux版本飞信程序
[root@localhost ~]# tar -zxvf fetion20091117-linux.tar.gz
[root@localhost ~]# mkdir /usr/local/fetion
[root@localhost ~]# cp fx/* /usr/local/fetion
cp: omitting directory `fx/cache'
cp: omitting directory `fx/commands'
cp: omitting directory `fx/done'
cp: omitting directory `fx/logs'
cp: omitting directory `fx/plugins'
[root@localhost fetion]# echo "/usr/local/fetion" >> /etc/ld.so.conf

[root@localhost fetion]# ldd /usr/local/fetion/fetion
        linux-gate.so.1 =>  (0x00d51000)
        libACE-5.7.2.so => not found
        libACE_SSL-5.7.2.so => not found
        libssl.so.4 => not found
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x0748e000)
        libm.so.6 => /lib/libm.so.6 (0x00b97000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x009f0000)
        libc.so.6 => /lib/libc.so.6 (0x00a4f000)
        /lib/ld-linux.so.2 (0x00a31000)
        libcrypto.so.4 => not found
[root@localhost fetion]# /usr/local/fetion/fetion


Nagios配置
[root@localhost fetion]# vim /usr/local/nagios/etc/objects/templates.cfg
define contact{
        name                            generic-contact         ; The name of this contact template
        service_notification_period     24x7                    ; service notifications can be sent an
ytime
        host_notification_period        24x7                    ; host notifications can be sent anyti
me
        service_notification_options    w,u,c,r,f,s             ; send notifications for all service s
tates, flapping events, and scheduled downtime events
        host_notification_options       d,u,r,f,s               ; send notifications for all host stat
es, flapping events, and scheduled downtime events
        service_notification_commands   notify-service-by-email,notify-service-by-sms   ; send service
 notifications via email
        host_notification_commands      notify-host-by-email,notify-host-sms    ; send host notificati
ons via email
        register                        0                       ; DONT REGISTER THIS DEFINITION - ITS
NOT A REAL CONTACT, JUST A TEMPLATE!
        }


[root@localhost ~]# vim /usr/local/nagios/etc/objects/commands.cfg
#####################feixin kehuduan###############################################
define command{
        command_name    notify-service-by-sms  #定义一个服务发生故障时发生报警
                           #短信的指令
        command_line    /usr/local/fetion/fetion --mobile=18610405476 --pwd=18610405479 --to=$CONTACTP
AGER$ --msf-utf8=$HOSTADDRESS$ --msg-utf8="$HOSTADDRESS$ $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"0
}
####################################################################################################
define command{
        command_name    notify-host-by-sms     #定义一个主机发生故障时发生报警短信的指令
        command_line    /usr/local/fetion/fetion --mobile=18610405479 --pwd=18610405479 -to=$SONTACTPA
GER$ --msg-utf8="Host $HOSTSTATE$ alert for $HOSTNAME$! on '$DATETIME$'"
}


[root@localhost fetion]# vim /usr/local/nagios/etc/objects/contacts.cfg
define contact{
        contact_name                    shancg          ; Short name of user
        use                             generic-contact         ; Inherit default values from generic-
contact template (defined above)
        alias                           sa-system               ; Full name of user

        email                           shancg@corpmail.cn      ; <<***** CHANGE THIS TO YOUR EMAIL AD
DRESS ******
        pager                           18610405479              # 飞信的手机号
        }



测试文件配置是否正确
[root@localhost ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors:   0


从启动服务Nagios
[root@localhost ~]# service nagios restart