# Nagios官网 http://www.nagios.org
nagios是php编写的,需要提供支持php的环境
支持插件
  安装nrpe是为了检测一些特殊的服务,比如负载
  iptables和selinux关闭
★Nagios安装
服务端(192.168.182.100)
编译安装很复杂,可以使用rpm包安装(适用于CentOS)
Centos6默认的yum源里没有nagios相关的rpm包,我们可以安装一个epel的扩展源
yum install -y httpd                <== 配置文件默认与httpd.conf对应,安装httpd是为了
                                        web访问,通过浏览器查看监控内容
nagios
nagios-plugins
nagios-plugins-all
nrpe
               nagios-plugins-nrpe
设置登录nagios后台的用户和密码:htpasswd -c /etc/nagios/passwd nagiosadmin
编辑配置文件 vim /etc/nagios/nagios.cfg
检测配置文件 nagios -v /etc/nagios/nagios.cfg 
启动服务:service httpd start; service nagios start
浏览器访问: http://192.168.182.100/nagios
客户端(192.168.182.105)
我们同样需要安装epel扩展源
在客户端机器上 rpm -ivh  http://www.aminglinux.com/bbs/da ... ease-6-7.noarch.rpm
yum install -y nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe 
编辑配置文件   vim /etc/nagios/nrpe.cfg  
allowed_hosts=127.0.0.1==>allowed_hosts=127.0.0.1,192.168.182.100 ip为服务端ip                                                <== 规定哪些主机可以使用nrpe服务
dont_blame_nrpe=0 ==> dont_blame_nrpe=1
启动客户端 /etc/init.d/nrpe start
★监控中心(192.168.182.100)添加被监控主机(192.168.182.105)
    cd /etc/nagios/conf.d/
    vim yue.cfg(182.105上)         //定义一个主机三个服务
define host{                                       <== 定义主机
        use                     linux-server       <== Name of host template to use
        host_name               yue                <== 被监控主机名
        alias                   hera
        address                 192.168.182.105    <== 被监控主机ip
        }
define service{
        use                     generic-service
        host_name               yue
        service_description     check_ping         <== 可以在web页面里看到
        check_command           check_ping!100.0,20%!200.0,50% <== 丢包20%警告,丢包50%
                                                                   heck_ping后面是要跟参数
                                                                   ,在写配置文件的时候参数 
要用!分隔开.
        max_check_attempts 5
        normal_check_interval 1
}
define service{
        use                     generic-service
        host_name               192.168.182.105
        service_description     check_ssh
        check_command           check_ssh
        max_check_attempts      5                   <== 当nagios检测到问题时,一共尝试检测5
次都有问题才会告警,如果该数值为1,
                                                        那么检测到问题立即告警       
        normal_check_interval 1                     <== 重新检测的时间间隔,单位是分钟,默
                                                        认是3分钟
        notification_interval           60 #在服务出现异常后,故障一直没有解决,nagios再次对使用者发出通知的时间。单位是分钟。如果你认为,所有的事件只需要一次通知就够了,可以把这里的选项设为0。 
}
define service{
        use                     generic-service
        host_name               192.168.0.12
        service_description     check_http
        check_command           check_http
        max_check_attempts      5
        normal_check_interval 1
}
  上面的服务不依赖于客户端nrpe服务,我们可以知道在自己电脑上可以使用ping或telnet探测远程任何一台机器是否存活以及是否开启某个端口或服务。 
  但是当我们想要检测客户端上的某个具体服务的情况时,就需要借助于nrpe了,比如想知道客户端机器的负载或磁盘使用情况。
★继续添加服务
  在服务端编辑配置文件  vim /etc/nagios/objects/commands.cfg
  增加:define command{
        command_name    check_nrpe   实现和对方nrpe服务的通信
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
  继续编辑配置文件      vim /etc/nagios/conf.d/yue.cfg 
  增加如下内容:
define service{
        use     generic-service
        host_name       yue
        service_description     check_load
        check_command           check_nrpe!check_load
        max_check_attempts 5
        normal_check_interval 1
}
define service{
        use     generic-service
        host_name       yue
        service_description     check_disk_hda1
        check_command           check_nrpe!check_hda1
        max_check_attempts 5
        normal_check_interval 1
}
define service{
        use     generic-service
        host_name       yue
        service_description     check_disk_hda2
        check_command           check_nrpe!check_hda2
        max_check_attempts 5
        normal_check_interval 1
}
说明:  check_nrpe!check_load :这里的check_nrpe就是在commands.cfg刚刚定义的,check_load是远程主机上的一个检测脚本
在远程主机上vim /etc/nagios/nrpe.cfg 搜索check_load,这行就是在服务端上要执行的脚本了,我们可以手动执行这个脚本
把check_hda1更改一下:/dev/hda1 改为 /dev/sda1
再加一行command[check_hda2]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda2
客户端上重启一下nrpe服务: service nrpe restart
服务端也重启一下nagios服务: service nagios restart
★配置告警
  vim /etc/nagios/objects/contacts.cfg //增加:
define contact{
        contact_name               123
        use                             generic-contact
        alias                           yue
        email              345394956@qq.com
        }
define contact{
        contact_name               456
        use                              generic-contact
        alias                            aaa
        email              345394956@qq.com
        }
define contactgroup{
        contactgroup_name                common
        alias                            common
        members                          123,456
        }
然后在要需要告警的服务里面加上contactgroup
define service{
        use                     generic-service
        host_name               192.168.182.105
        service_description     check_load
        check_command           check_nrpe!check_load
        max_check_attempts      5
        normal_check_interval   1
        contact_groups          common
        notifications_enabled   1           <==是否开启提醒功能(1开启,0禁用)一般,这个选项
会在主配置文件(nagios.cfg)中定义,效果相同。 service_notification_period 24*7 <==发送提醒的时间段。
非常重要的主机(服务)我定义为7×24
一般的主机(服务)就定义为上班时间
  如果不在定义的时间段内,无论出现任何问题都不会发送提醒。还有一个host_notification_period, 相同的设置方法。但需要加到host的定义配置文件里。
        service_notification_options:w,u,c,r <==上面的是host的状态,这个是service的状态。
                                                w(waning)   u(unknown) 
c(critical) r(recover)恢复了
host_notification_options:d,u,r <==发送提醒包括的情况
d(状态为DOWN) u(状态为UNREACHABLE)
r(状态恢复为OK)
                                                也需要加入到host的定义配置里
}
★配置图形显示 pnp4nagios (请自行演示,课上不演示)
安装
yum install pnp4nagios rrdtool
配置主配置文件
vim /etc/nagios/nagios.cfg  //修改如下配置
    process_performance_data=1    
    host_perfdata_command=process-host-perfdata
    service_perfdata_command=process-service-perfdata 
enable_environment_macros=1
修改commands.cfg
    vim /etc/nagios/objects/commands.cfg  
方便起见,我们可以注释掉原有对process-host-perfdata和process-service-perfdata,重新定义
加入以下内容
define command {
command_name process-service-perfdata
       command_line    /usr/bin/perl /usr/libexec/pnp4nagios/process_perfdata.pl  
 }  
define command {  
command_name process-host-perfdata
       command_line    /usr/bin/perl /usr/libexec/pnp4nagios/process_perfdata.pl -d HOSTPERFDATA  
}  
修改配置文件  templates.cfg
vim /etc/nagios/objects/templates.cfg 
define host {  
        name       hosts-pnp  
        register   0  
        action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_  
        process_perf_data               1  
}  
define service {  
        name       srv-pnp  
        register   0  
        action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$ 
        process_perf_data               1  
}  
修改host和service配置
vim /etc/nagios/conf.d/yue.cfg 
把  define host{
        use                     linux-server
改为:
define host{
        use                     linux-server,hosts-pnp
修改对应的service
将
define service{
        use     generic-service
        host_name       192.168.182.100
        service_description     check_disk_hda1
        check_command           check_nrpe!check_hda1
        max_check_attempts 5
        normal_check_interval 1
}
改为:
define service{
        use     generic-service,srv-pnp
        host_name       192.168.182.100
        service_description     check_disk_hda1
        check_command           check_nrpe!check_hda1
        max_check_attempts 5
        normal_check_interval 1
}
重启和启动服务:
service nagios restart
service httpd restart
service npcd start
★访问测试
   web下访问
192.169.182.100/nagios/
192.168.182.100/pnp4nagios/
原文地址:http://caoyue.blog.51cto.com/9876038/1616649