7007 发表于 2019-1-16 12:50:28

Nagios搭建完整记录

首先要感谢田逸老师,他的《互联网运营智慧》果真是一本不可多得的好书。田逸老师的blog地址是:http://sery.blog.运维网.com/,真心感谢田逸老师给我们带来这本非常好的书。

      本文中,完全是田逸老师实验步骤来实践的。不过也有自己的一些小的变动。不过不大。版权应该完全归田逸老师所有,我只是整理罢了。只是在想可能还有一些像我一样的菜鸟在看田逸老师的书的时候感觉配置有点小费劲,呵呵,大家应该可以参考我的实验步骤来操作。应该可以顺利的搭建出来。

      一直以来希望总结一套完整的nagios安装配置文档,却总也没有动手,看了田逸老师的《互联网运营智慧》一书中关于nagios搭建的介绍,照着老师的配置,却总有一点点弄不好,搭建好了以后,nagios服务器总也监控不了远程客户端的nrpe,在服务端测试过去也是弄不过去,在文章最后在写走过的弯路,已经把产生这种问题的原因找到了,在下边的文档配置中将解决这个问题。这里先好好的把所有步骤总结下,这里也只是写了一台服务器的监控,并没有涉及到更多的服务器,在后续总结中会陆续添加上。



搭建顺利几要素:
1、一定要保证apache添加了php支持
2、一定要保证客户机上有相应的check_脚本
3、一定要保证nrpe.cfg中关于command的记录是正常的。
4、一定要保证主机usr/local/nagios/etc/objects/hosts.cfg和/etc/hosts里面有关于services.cfg中的host_name项的记录,否则check_nrpe绝对不通



nagios服务器需要的包:
httpd-2.2.15.tar.gz
php-5.2.17.tar.gz
nagios-3.2.3.tar.gz
nagios-plugins-1.4.15.tar.gz



被监听服务器需要的包
nrpe-2.12.tar.gz
nagios-plugins-1.4.15.tar.gz



nagios安装环境介绍:
源码安装了apache服务。路径:/usr/local/apache
正确添加了php支持。



1、添加nagios用户,可以不使其自动登录。
useradd nagios



2、改造apache服务主配置文件/usr/local/apache/conf/httpd.conf,为其添加nagios虚拟目录:




[*]ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
[*]
[*]      AuthType Basic
[*]      Options ExecCGI
[*]      AllowOverride None
[*]       Order allow,deny
[*]      Allow from all
[*]      AuthName "Nagios Access"
[*]      AuthUserFile /usr/local/nagios/etc/htpasswd
[*]      Require valid-user
[*]
[*]Alias /nagios /usr/local/nagios/share
[*]
[*]      AuthType Basic
[*]      Options None
[*]      AllowOverride None
[*]      Order allow,deny
[*]      Allow from all
[*]      AuthName "Nagios Access"
[*]      AuthUserFile /usr/local/nagios/etc/htpasswd
[*]      Require valid-user
[*]

3、创建密码文件/usr/local/nagios/etc/htpasswd
# /usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd nagios



4、安装nagios程序包,我这里使用的是到2011年3月9日最新版,nagios-3.2.3
tar zxvf nagios-3.2.3.tar.gz –C /usr/local/
cd /usr/local/nagios-3.2.3/
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make all
make install
make install-commandmode
make install-config



5、安装nagios-plugins插件,这里安装的是nagios-plugins-1.4.15
tar zxvf nagios-plugins-1.4.15.tar.gz -C /usr/local/
cd /usr/local/nagios-plugins-1.4.15/
./configure --prefix=/usr/local/nagios --enable-readhat-pthread-workaround
make
make install



6、nagios的配置


(1)、修改主配置文件/usr/local/nagios/etc/nagios.cfg




[*]#将localhost.cfg注释掉,而改用多文件配置,使得配置文件更加清晰明了。
[*]#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
[*]
[*]#主机配置文件路径
[*]cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
[*]
[*]#主机组配置文件路径
[*]cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg
[*]
[*]#联系人配置文件路径
[*]cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
[*]
[*]#联系组配置文件路径
[*]cfg_file=/usr/local/nagios/etc/objects/contactgroups.cfg
[*]
[*]#监控服务配置文件路径
[*]cfg_file=/usr/local/nagios/etc/objects/services.cfg
[*]
[*]#监视时段配置文件路径
[*]cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
[*]
[*]#在Web界面下重启Nagios、停止主机/服务检查等操作,默认值为0
[*]check_external_commands=1
[*]
[*]#根据自己的情况定义这个命令检查时间间隔,默认值为1秒
[*]command-check_interval=10s



(2)、修改/usr/local/nagios/etc/cgi.cfg,这里写出被修改的部分




[*]authorized_for_system_information=nagios
[*]authorized_for_configuration_information=nagios
[*]authorized_for_system_commands=nagios
[*]authorized_for_all_services=nagios
[*]authorized_for_all_hosts=nagios
[*]authorized_for_all_service_commands=nagios
[*]authorized_for_all_host_commands=nagios

(3)、修改/usr/local/nagios/etc/objects/commands.cfg,添加对check_nrpe命令的定义,同时,在commands.cfg中还可以添加手机发送短信功能。因为这里并没有实现所以并没有写。具体实现请参照田逸老师的《互联网运营智慧》一书。




[*]# 'check-nrpe' command definition
[*]define command{
[*]      command_name    check_nrpe
[*]      command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
[*]      }

(4)、修改/usr/local/nagios/etc/objects/hosts.cfg主机配置文件

备注:注意host_name的指向,这里的server名称必须在/etc/hosts中定义,否则不会找到该客户机。




[*]define host {
[*]      host_name nagios-server
[*]      alias nagios server
[*]      address 192.168.2.4
[*]      contact_groups sagroup
[*]      check_command check-host-alive
[*]      max_check_attempts 5
[*]      notification_interval 10
[*]      notification_period 24x7
[*]      notification_options d,u,r
[*]      }

(5)、修改/usr/local/nagios/etc/objects/hostgroups.cfg主机组配置文件




[*]define hostgroup {
[*]      hostgroup_name sa-servers
[*]      alias sa servers
[*]      members nagios-server
[*]      }

(6)、修改/usr/local/nagios/etc/objects/contacts.cfg联系人配置文件




[*]define contact {
[*]      contact_name nagios
[*]      alias nagios administrator
[*]      service_notification_period 24x7
[*]      host_notification_period 24x7
[*]      service_notification_options w,u,c,r
[*]      host_notification_options d,u,r
[*]      service_notification_commands notify-service-by-email
[*]      host_notification_commands notify-host-by-email
[*]      email yikiboy@163.com
[*]      }

(7)、修改/usr/local/nagios/etc/objects/contactgroups.cfg联系人配置文件

备注:这里还可以添加更多的分组。用来建立不同级别的警报机制,比如主机不可达,主机宕机,主机恢复等各种级别的报警分别发给不同的用户。




[*]define contactgroup {
[*]      contactgroup_name sagroup
[*]      alias system administrator group
[*]      members nagios
[*]      }

(8)、修改/usr/local/nagios/etc/objects/services.cfg监控服务配置文件(重要)

备注:这里的hots_name必须为hosts.cfg中定义过的,否则check_nrpe将不能检测该服务。




[*]define service {
[*]      host_name nagios-server
[*]      service_description check-host-alive
[*]      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-host-alive
[*]      }
[*]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
[*]      }
[*]define service {
[*]      host_name nagios-server
[*]      service_description check-disk
[*]      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_nrpe!check_df
[*]      }
[*]define service {
[*]      host_name nagios-server
[*]      service_description 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
[*]      check_command check_nrpe!check_load
[*]      }
[*]define service {
[*]      host_name nagios-server
[*]      service_description 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
[*]      check_command check_nrpe!check_total_procs
[*]      }

7、在nagios监控服务器及客户端上部署nrpe,并在被监控客户端上部署nagios-plugings
tar zxvf nrpe-2.12.tar.gz -C /usr/local/
cd /usr/local/nrpe-2.12/
./configure --prefix=/usr/local/nrpe
make
make install



在nagios主机上:
cp /usr/local/nrpe/libexec/check_nrpe /usr/local/nagios/libexec/

在客户端上:
tar zxvf nagios-plugins-1.4.15.tar.gz -C /usr/local/
cd /usr/local/nagios-plugins-1.4.15/
./configure --prefix=/usr/local/nrpe --enable-readhat-pthread-workaround
make
make install



8、nrpe的配置



(1)、mkdir /usr/local/nrpe/etc


(2)、cp /usr/local/nrpe-2.12/sample-config/nrpe.cfg /usr/local/nrpe/etc


(3)、修改配置文件/usr/local/nrpe/etc/nrpe.cfg,这里列出我的配置文件:




[*]log_facility=daemon
[*]pid_file=/var/run/nrpe.pid
[*]server_address=192.168.2.4
[*]server_port=5666
[*]nrpe_user=nagios
[*]nrpe_group=nagios
[*]allowed_hosts=127.0.0.1,192.168.2.4
[*]dont_blame_nrpe=0
[*]debug=0
[*]command_timeout=60
[*]connection_timeout=300
[*]command=/usr/local/nrpe/libexec/check_load -w 15,10,5 -c 30,25,20
[*]command=/usr/local/nrpe/libexec/check_disk -w 20% -c 10%
[*]command=/usr/local/nrpe/libexec/check_procs -w 5 -c 10 -s Z
[*]command=/usr/local/nrpe/libexec/check_procs -w 150 -c 200
[*]command=/usr/local/nrpe/libexec/ip_conn.sh 8000 10000
[*]#allowed_hosts指允许监控你的nagios服务器地址。因为是客户机,这里必须要填上nagios监控主机的ip,也可以填上127.0.0.1,用逗号分隔。
[*]#这里command
[*]里定义的项目,就是在主机的check_nrpe中添加的定义字符串,请看services.cfg中的“check_nrpe!check_df”你就明白这些字符串是干什么用的了。也就可以自己手动添加一些自己要监控的程序进去了。
[*]#这里的check_ips中指定的ip_conn.sh脚本是要手动创建的(脚本摘自田逸老师的《互联网运营智慧》一书)。

vim /usr/local/nrpe/libexec/ip_conn.sh



[*]#!/bin/sh
[*]#if [ $# -ne 2 ]
[*]#       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 -connect counts is $ip_conns"
[*]      exit 0
[*]fi
[*]if [ $ip_conns -gt $1 -a $ip_conns -lt $2 ]
[*]      then
[*]      echo "Warning -connect counts is $ip_conns"
[*]      exit 1
[*]fi
[*]if [ $ip_conns -gt $2 ]
[*]      then
[*]      echo "Critical -connect counts is $ip_conns"
[*]      exit 2
[*]fi
[*]#指定2个连接数,当连接数大于指定的第一个数的时候,给予warning警报,当连接数大于指定的第二个数的时候,给予Critical警报。

9、在各客户端测试nrpe服务中各个插件的功能

# /usr/local/nrpe/libexec/check_load -w 15,10,5 -c 30,25,20
OK - load average: 0.00, 0.00, 0.00|load1=0.000;15.000;30.000;0; load5=0.000;10.000;25.000;0; load15=0.000;5.000;20.000;0;


# /usr/local/nrpe/libexec/check_disk -w 20% -c 10%
DISK OK - free space: / 2706 MB (41% inode=81%); /boot 81 MB (87% inode=99%); /dev/shm 251 MB (100% inode=99%); /mnt 172666 MB (81% inode=-);| /=3889MB;5558;6253;0;6948 /boot=12MB;78;88;0;98 /dev/shm=0MB;200;225;0;251 /mnt=38022MB;168550;189619;0;210688


# /usr/local/nrpe/libexec/check_procs -w 5 -c 10 -s Z
PROCS OK: 0 processes with STATE = Z


# /usr/local/nrpe/libexec/check_procs -w 150 -c 200
PROCS OK: 66 processes


# /usr/local/nrpe/libexec/ip_conn.sh 8000 10000
/usr/local/nrpe/libexec/ip_conn.sh: line 6: nestat: command not found
OK -connect counts is 0



10、在服务端测试check_nrpe功能



# ./check_nrpe -H nagios-server -c check_df
DISK OK - free space: / 2706 MB (41% inode=81%); /boot 81 MB (87% inode=99%); /dev/shm 251 MB (100% inode=99%); /mnt 172666 MB (81% inode=-);| /=3889MB;5558;6253;0;6948 /boot=12MB;78;88;0;98 /dev/shm=0MB;200;225;0;251 /mnt=38022MB;168550;189619;0;210688




# ./check_nrpe -H nagios-server -c check_load
OK - load average: 0.00, 0.00, 0.00|load1=0.000;15.000;30.000;0; load5=0.000;10.000;25.000;0; load15=0.000;5.000;20.000;0;


# ./check_nrpe -H nagios-server -c check_zombie_procs
PROCS OK: 0 processes with STATE = Z


# ./check_nrpe -H nagios-server -c check_total_procs
PROCS OK: 69 processes


# ./check_nrpe -H nagios-server -c check_ips
OK -connect counts is 0



11、各机器以独立守护进程启动nrpe服务
/usr/local/nrpe/bin/nrpe -c /usr/local/nrpe/etc/nrpe.cfg -d



12、服务器端启动nagios监控服务
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg



13、启动apache服务器
/usr/local/apache/bin/apachectl -k start



14、测试nagios功能
elinks http://127.0.0.1/nagios输入账号密码看看是不是状态都正常。



15、测试通过以后,向/etc/rc.local中添加启动脚本

vim /etc/rc.local




[*]/usr/local/apache/bin/apachectl -k start
[*]/usr/local/nrpe/bin/nrpe -c /usr/local/nrpe/etc/nrpe.cfg -d
[*]/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

http://blog.运维网.com/attachment/201103/163945106.png
  配置全部完成。
  后记:在配置完成当初,完全是忘了配置php了,哎,一直提示没有权限登录。等后来编译了php以后,还是报错,我就很诧异。怎么回事呢,后来仔细检查httpd.conf,才发现原来是这一行搞的怪
  ScriptAlias /nagios /usr/local/nagios/share

  应该为Alias,改回来以后,登录nagios不报错了,但是疯狂的报Critical错误,经分析,全是check_nrpe连接外部主机的。经过反复测试,并且查看各个代码,发现原来是hostname那里连不过去。经过测试,添加到/etc/hosts中,问题解决。过了一会,全ok了。哎,一波三折啊。写的太不仔细了,太郁闷了。以后一定不会这样了。



页: [1]
查看完整版本: Nagios搭建完整记录