在上一篇中,我们成功安装了nagios监控端,使其能够正常运作,但是还没有配置,因此还发挥不出任何作用。

这篇我们就开始配置nagios了,让它发挥真正的效用


nagios配置文件在 ~/etc目录下,主要是以下几种。


文件名类型备注nagios.cfg主配置文件需要修改cgi.cfg脚本控制文件可添加用户commands.cfg命令定义文件命令配置文件,基本不需要改contacts.cfg用户和用户组定义文件
配置用户和组,需要改timeperiods.cfg时间定义文件定义时间,不需要修改templates.cfg主机定义模板文件定义监控主机的类型的模板文件,基本不需改

1.修改主配置文件

主要修改如下几项

check_external_commands=1 #允许web界面修改重启nagios服务

command_check_interval=10s #命令检查时间间隔

cfg_file=/usr/local/nagios/etc/objects/101.cfg #添加要监控主机的配置文件,一个主机对应一个文件。

cfg_file=/usr/local/nagios/etc/objects/235.cfg # 我习惯是已最后一个IP为名字命名

。。。。。。。。。

2.检查脚本控制文件cgi.cfg

use_authentication=1 #启用用户验证

authorized_for_system_information=nagiosadmin,testauthorized_for_configuration_information=nagiosadmin,testauthorized_for_system_commands=nagiosadmin,test authorized_for_all_services=nagiosadmin,testauthorized_for_all_hosts=nagiosadmin,testauthorized_for_all_service_commands=nagiosadmin,testauthorized_for_all_host_commands=nagiosadmin,test

如果要添加更多用户需要在上面的选项都添加,且用户都是由 /usr/local/apache/bin/htpasswd –c /usr/local/nagios/etc/htpasswd XXX 生成的(XXX为用户名)。


3.命令配置文件commands.cfg

定义命令的配置文件,如果没有需要自定义一个特殊的检查的话,可以跳过不修改。


4.contacts.cfg timeperiods.cfg templates.cfg 为三个模板文件,分别是时间,主机,和用户的模板。

网上很多大神都自己全部重写的配置模板,这些全部弃之不用。其实如果没有什么特殊需要的话,模板里面的情况足够应对大部分需求。在创建具体监控目标时,直接引用模板就可以了,省时省力。

三个模板的内容大致如下

contacts.cfg

define contact{

contact_name nagiosadmin ; 联系人名称

use generic-contact ; 所利用的模板 alias Nagios Admin ; 别名 email test@tssss.com ; 邮箱地址

}


define contactgroup{

contactgroup_name admins ; 组名

alias Nagios Administrators ; 组的别名

members nagiosadmin ; 组的成员

}



在timeperiods.cfg中

define timeperiod{

timeperiod_name 24x7

alias 24 Hours A Day, 7 Days A Week

sunday 00:00-24:00

monday 00:00-24:00

tuesday 00:00-24:00

wednesday 00:00-24:00 ;定义好的24*7直接引用就能用

thursday 00:00-24:00

friday 00:00-24:00

saturday 00:00-24:00

}


在templates.cfg中模板较多,这里就举几个常用的,其它的大多数选项的大同小异。

define contact{

name generic-contact ; 模板-联系人

service_notification_period 24x7 ; 服务出状况的通知时间段

host_notification_period 24x7 ; 主机出状况通知时间段

service_notification_options w,u,c,r ;

host_notification_options d,u,r ; 服务和主机在那些情况下会有所动作(w-警告,u-未知,c-危险,r-恢复,d-当机)

service_notification_commands notify-service-by-email ;

host_notification_commands notify-host-by-email ; 服务和主机采取的动作为发送邮件(具体定义在commands中)

register 0 因为这个为模板,所以为0



define host{

name generic-host ; 模板-全局主机

notifications_enabled 1 ;

event_handler_enabled 1 ;

flap_detection_enabled 1 ;基本就是开启各项功能

failure_prediction_enabled 1 ;

process_perf_data 1 ;

retain_status_information 1 ;

retain_nonstatus_information 1 ;

notification_period 24x7 ;

register 0 ;

}




define host{

name linux-server ; 模板-LINUX服务器

use generic-host ; 借用全局模板

check_period 24x7 ; 检查的时间段

check_interval 5 ; 检查时间间隔 (分钟)

retry_interval 1 ; 重试的检查间隔(分钟)

max_check_attempts 10 ; 最大检查次数

check_command check-host-alive ; 指定检查命令

notification_period 24x7 ; 故障时发送的时间范围

;

notification_interval 120 ; 故障没解决再次发出通知的间隔时间

notification_options d,u,r ; 指定状态下才发送

contact_groups admins ; 发送的组

register 0 ;

还有个windows-server和generic-service等,与上面linux-server大同小异,就不列了。




上面这些模板在引用是可以省很大力气的,基本可以应对大部分情况。



准备工作做了那么多,现在终于突入最后一个阶段。建立对应的服务器监视配置文件

例如现在有一台IP为192.168.1.121的linux服务器

在~/etc/object/目录下创建一个文件121.cfg

然后在其中填入如下内容



define host{

host_name 121_agent_master /主机名,起个认得的就行

use linux-server /引用的模板为linux-server

address 192.168.1.121 /主机的地址

}


define service{

host_name 121_agent_master /引用上面的资料

name 121-service / 服务名,用来引用

use generic-service / 引用服务模板


}


define service{

use 121-service / 引用上面的配置

service_description ping /显示在nagios上的服务名,最好通俗易懂

check_command check_ping!20000.0,80%!30000.0,100%!10!30

} / 第3排就是具体的命令了 ,具体写法可以参照commands.cfg中的说明,这条命令是测试ping的。


define service{

use 121-service

service_description http 80

check_command check_tcp!80!3!8!20

} 这组是测试80端口的存活



5.部署nrpe

在被监控机上要安装配置nrpe才能和监控端形成互动,这样监控才能真正的形成。


首先在监控机上。。安装NRPE组件

tar xzf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure --prefix=/usr/local/nagios
make all
make install-plugin


然后在配置文件command.cfg中添加如下字段

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


这样在添加监控时,就可以使用nrpe这个项目了



然后在被监控端上


1)添加用户

useradd nagios

2)安装nagios-plugins

tar zxvf nagios-plugins-1.4.15.tar.gz

cd nagios-plugins-1.4.15

./configure –prefix=/usr/local/nagios

make & make install

chown nagios.nagios /usr/local/nagios/
chown -R nagios.nagios /usr/local/nagios/libexec/



3) 安装nrpe

tar zxvf nrpe-2.8.1.tar.gz

cd nrpe-2.8.1

./configure –prefix=/usr/local/nagios

make all
make install-plugin
make install-daemon
make install-daemon-config



4)修改配置文件/usr/local/nagios/etc/nrpe.cfg

主要修改以下两项

server_address=192.168.1.121

allowed_hosts=127.0.0.1,192.168.1.100 (后面为监控端的IP)


6.启动nrpe服务并检验其配置

1)以独立守护进程启动nrpe服务

/usr/local/nagios/bin/nrpe –c /usr/local/nagios/etc/nrpe.cfg –d

2)成功的话可以看到5666端口的监听

netstat -an |grep "5666"



7.启动nagios来监控这台主机

1)首先检查配置是否有错误,基本上错误都能找的很准确


/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg


直到看到以下内容就没问题了

Total Warnings: 0Total Errors: 0Things look okay - No serious problems were detected during the pre-flight check


2) 后台启动nagios


/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg &


3) 打开浏览器 输入 http:192.168.1.100/nagios 就能访问了 用户名是nagiosadmin(具体是在上一篇设置过的)。进入后点击host 即可看到监控的主机 应该有2台 一台为121 一台为localhost


这样简单的nagios监控就完成了。当然nagios的能力不止这些,依靠插件能够监控更多的东西。

当然 ,是我有空下期再写了。