opijoiu 发表于 2016-1-25 09:13:49

nagios安装配置

Nagios官网 http://www.nagios.org

1. Nagios安装 - 服务端(192.168.101.235)
Centos6默认的yum源里没有nagios相关的rpm包,但是我们可以安装一个epel的扩展源:

#yum install -y epel-release

然后安装nagios相关的包

#yum install -y httpd nagios nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe
设置登录nagios后台的用户和密码:

# htpasswd -c /etc/nagios/passwd nagiosadmin
New password: daixuan
如果需要修改配置文件(这里不需要):vim /etc/nagios/nagios.cfg
检测配置文件,显示如下,则配置文件没有问题
# nagios -v /etc/nagios/nagios.cfg
Total Warnings: 0
Total Errors:   0
启动服务:
# service httpd start; service nagios start
浏览器访问: http://192.168.101.235/nagios



2. Nagios安装 - 客户端(192.168.101.236)
在客户端机器上

# yum install -y epel-release
# yum install -y nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe
修改配置文件
# vim /etc/nagios/nrpe.cfg
找到“allowed_hosts=127.0.0.1”,添加主机IP

1
allowed_hosts=127.0.0.1,192.168.101.235




找到” dont_blame_nrpe=0” 改为1,1表示允许

1
dont_blame_nrpe=1




启动客户端

# /etc/init.d/nrpe start

3. 监控中心(server:192.168.101.235)添加被监控主机(client:192.168.101.236)

# cd /etc/nagios/conf.d/
# vim 192.168.101.236.cfg //添加一下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
define host{
      use                     linux-server            
      host_name         192.168.101.236
      alias                     101.236
      address               192.168.101.236
      }
define service{
      use                     generic-service
      host_name               192.168.101.236
      service_description   check_ping
      check_command         check_ping!100.0,20%!200.0,50%
      max_check_attempts 5
      normal_check_interval 1
}
define service{
      use                     generic-service
      host_name               192.168.101.236
      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.101.236
      service_description   check_http
      check_command         check_http
      max_check_attempts      5
      normal_check_interval 1
}




以上服务不依赖于客户端nrpe服务,我们在自己电脑上可以使用ping或者telnet探测远程任何一台机器是否存活、是否开启某个端口或服务。 而当我们想要检测客户端上的某个具体服务的情况时,就需要借助于nrpe了,比如想知道客户端机器的负载或磁盘使用情况。
检查配置文件是否有错误:
# nagios -v /etc/nagios/nagios.cfg
Total Warnings: 0
Total Errors:   0
# service httpd restart; service nagios restart

发现http服务critical,可能是80端口没有启动,client上启动httpd服务即可。

4. 继续添加服务
在服务端添加nrpe命令
# vim /etc/nagios/objects/commands.cfg
增加:


1
2
3
4
define command{
      command_name    check_nrpe
      command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
      }





# vim /etc/nagios/conf.d/192.168.101.236.cfg
增加如下内容:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
define service{
      use   generic-service
      host_name       192.168.101.236
      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       192.168.101.236
      service_description   check_disk_sda1
      check_command       check_nrpe!check_sda1
      max_check_attempts 5
      normal_check_interval 1
}
define service{
      use   generic-service
      host_name       192.168.101.236
      service_description   check_disk_sda3
      check_command         check_nrpe!check_sda3
      max_check_attempts 5
      normal_check_interval 1
}





注:check_nrpe!check_load :这里的check_nrpe就是在commands.cfg刚刚定义的脚本命令,check_load是在远程主机上的定义的一个检测脚本,在远程主机client(192.168.101.236)上
# vim /etc/nagios/nrpe.cfg
搜索check_load,我们可以手动执行这个脚本,把命令check_hda1更改为check_sda1,/dev/hda1 改为 /dev/sda1再加一行check_sda3,结果如下:

1
2
3
command=/usr/lib/nagios/plugins/check_load -w 15,10,5 -c 30,25,20
command=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda1
command=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda3




客户端上重启一下nrpe服务: service nrpe restart
服务端也重启一下nagios服务: service nagios restart
刷新浏览器,在service中找到可以看到监控的情况,过几分钟监控状态全部自动变为ok


5. 配置告警

# vim /etc/nagios/objects/contacts.cfg //增加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
define contact{
      contact_name               123
      use                           generic-contact
      alias                           daixuan
      email            1350368559@qq.com
      }


define contact{
      contact_name               456
      use                           generic-contact
      alias                            aaa
      email            18917870837@189.com
      }


define contactgroup{
      contactgroup_name         common
      alias                                  common
      members                        123,456
      }





然后在要需要告警的服务里面加上contactgroup

# vim /etc/nagios/conf.d/192.168.101.236.cfg

1
2
3
4
5
6
7
8
9
10
11
12
define service{
      use   generic-service
      host_name       192.168.101.236
      service_description   check_load
      check_command         check_nrpe!check_load
      max_check_attempts 5
      normal_check_interval 1
      contact_groups      common
      notifications_enabled1   # ;是否开启提醒功能。1为开启,0为禁用,一般,这个选项会在主配置文件(nagios.cfg)中定义,效果相同。
       # notification_period   24x7   ;发送提醒的时间段。非常重要的主机(服务)我定义为7×24,一般的主机(服务)就定义为上班时间。如果不在定义的时间段内,无论什么问题发生,都不会发送提醒。
      notification_options w,u,c,r# ;这个是service的状态。w为waning, u为unknown, c为critical, r为recover(恢复了),类似的还有一个host对应的状态:d,u,r d = 状态为DOWN, u = 状态为UNREACHABLE , r = 状态恢复为OK,需要加入到host的定义>
配置里。




参考:
调用短信接口http://www.iyunv.com/thread-149047-1-1.html
整合微信http://www.iyunv.com/thread-149046-1-1.html


问题排错:
1、监控status information提示:return code 255 is out of bounds,原因:没有权限或者监控nrpe服务未启动,参考:https://support.nagios.com/forum/viewtopic.php?f=7&t=26974
解决方法,确认服务器有登陆权限,并且被监控客户端client的nrpe服务启动
# vim /etc/nagios/nrpe.cfg找到“allowed_hosts=127.0.0.1”,添加主机IP
1
allowed_hosts=127.0.0.1,192.168.101.235




找到” dont_blame_nrpe=0” 改为1,1表示允许
1
dont_blame_nrpe=1




启动客户端
# /etc/init.d/nrpe start

2、不能发邮件
经过测试,虚拟机不能直接发邮件,但是阿里云主机可以,原因是没有公网IP,如果有问题参考以下三个步骤:http://www.iyunv.com/thread-155674-1-1.html
(1)hosts里的配置
# cat /etc/hosts

[*]172.16.4.3nagios.comnagios ##一定要有本机的ip对应
(2)hostname与/etc/sysconfig/network里的hostname一致,并与hosts里的一致
(3)去nagios的web里,选择报警的服务(如果httpd),点击http旁边的这个摁钮,如果你看到notification状态为disabled,那么你可以选择右侧的enabled notification for this services,确认对话框出来后,查看日志tail /var/log/nagios/nagios.log
如果内容有service notification这样的话,就代表nagios发送了邮件报警

页: [1]
查看完整版本: nagios安装配置