nagios的另一强大功能是它能同时监测主机和服务。例如,它可以同时监测到ip地址和tcp/udp端口号。为进一步阐述此功能,我们假定有台需要监测的web服务器,nagios可运用在服务器上基于ip/服务器名运行ping命令的方法检测服务器是否在线,同时当服务器的rtt(往返时延)增加时,nagios会随时告警。另外,nagios还能检测tcp的80端口(web服务器)是否可达,比如可能出现服务器在线但apache/iis没有响应的情况。
本段教程将展示linux下nagios的安装与配置.
<a target="_blank"></a>
debian系统用户使用apt-get命令就可安装nagios:
root@mrtg:~# apt-get install nagios3
nagios安装过程中可以设置邮件服务器,安装后也可以进行自定义设置。
提示:为使nagios可正常发送邮件通知,需正确配置smtp。

可以看到nagios支持多种邮件传送方式。最常见的莫过于服务器(internet site)直接向接收者发送邮件,另一种常见的形式是用智能主机或中继服务器,将邮件传送至中继邮件服务器,然后它负责将邮件传送给接收者。
进行下一步操作时服务器的域名需要包含进去。
最后,设置一下nagios管理员‘nagiosadmin’的密码,用户可以之后自行修改密码。
[root@mrtg ~]# yum install nagios nagios-plugins
本段教程中,我们希望完成以下监测内容:
每三分钟监测一次所有linux服务器。
每三分钟监测一次所有思科路由器。
发出告警前nagios都将进行3次验证以确保问题确有发生。
当设备的往返时延超过100ms并且/或包丢失量超过20%将发出邮件告警。
余下的教程将指导您如何在linux系统中配置nagios。
明确nagios配置文件的地址至关重要,以下路径指明了debian系统下nagios的配置文件地址。
/etc/nagios-plugins 实现监测功能的专有脚本存放地址
/etc/nagios3 添加主机、服务,定义检测和定时器的配置文件
/usr/lib/nagios/plugins 用于监测的可执行文件
接下来的步骤相互关联,由此开始定义主机,主机组及向主机组添加服务操作。
我们对同一类型的主机定义了对应的模板,这里使用安装文件作举例说明。
首先,为linux设备定义主机模板。
root@mrtg:~# cd /etc/nagios3/conf.d
root@mrtg:/etc/nagios3/conf.d/# cp generic-host_nagios2.cfg linux-server.cfg
如下编辑linux-server.cfg。需要修改部分已经标出。
root@mrtg:/etc/nagios3/conf.d/# vim linux-server.cfg
-
define host{
name linux-server ; 名称,需修改
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
check_command example-host-check ; 检查所用脚本,需修改
check_interval 3 ; 连续检查的间隔,需修改
max_check_attempts 3 ; 产生邮件告警前的自检次数,需修改
notification_interval 0
notification_period 24x7
notification_options d,u,r
contact_groups admins ; 邮件将要发送至的组,需修改
register0
}
接下来,为cisco设备定义主机模板。
root@mrtg:/etc/nagios3/conf.d/# cp linux-server.cfg cisco-device.cfg
如下修改cisco-device.cfg。需要修改部分已经标出。
root@mrtg:/etc/nagios3/conf.d/# vim cisco-device.cfg
name cisco-device ;名称,需修改
check_command example-host-check ; 检查时使用的脚本,需修改
check_interval 3 ; 连续检查间隔,需修改
max_check_attempts 3 ; 产生邮件告警前的自检次数,需修改
notification_period 24x7
contact_groups admins ; 邮件将要发至的组,需修改
register 0
现在已定义主机模板,就可以添加需要监测的主机。以默认的文件作例子展示如下内容。
root@mrtg:/etc/nagios3/conf.d/# cp localhost_nagios2.cfg example.cfg
root@mrtg:/etc/nagios3/conf.d/# vim example.cfg
# host 1
use linux-server ; 使用的主机模板名
host_name our-server ; nagios使用的主机名
alias our-server
address 172.17.1.23 ; 主机的ip地址
# host 2
use cisco-device ; 使用的主机模板名
host_name our-router ; nagios使用的主机名
alias our-router
address 172.17.1.1 ; 主机的ip地址
当有多个主机时,为方便管理,建议将相似的主机组成一组。
root@mrtg:/etc/nagios3/conf.d/# vim hostgroups_nagios2.cfg
define hostgroup {
hostgroup_name linux-server ; 主机组名
alias linux servers
members our-server ; 组员列表
hostgroup_name cisco-device ; 主机组名
alias cisco devices
members our-server ; comma separated list of members
首先,定义一个服务example-host-check:当往返时延达到100ms预警值并且有20%包丢失时发出警告,而紧急告警设置为5000ms且包丢失比率为100%,只执行一个ipv4的ping请求检测。
root@mrtg:~# vim /etc/nagios-plugins/config/ping.cfg
define command{
command_name example-host-check
command_line /usr/lib/nagios/plugins/check_ping -h '$hostaddress$' -w 100,20% -c 5000,100% -p 1 -4
然后,将命令关联至主机组。
root@mrtg:/etc/nagios3/conf.d/# vim services_nagios2.cfg
define service {
hostgroup_name linux-server
service_description linux servers
check_command example-host-check
use generic-service
notification_interval 0 ; 初始化设置为0
hostgroup_name cisco-device
service_description cisco devices
进行如下定义将发送邮件需要的地址添加至nagios。
root@mrtg:/etc/nagios3/conf.d/# vim contacts.cfg
define contact{
contact_name root
alias root
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
email root@localhost, [email protected]
最后,试运行初始化检测是否有配置错误。如果没有错误,nagios开始安全运行。
root@mrtg:~# nagios -v /etc/nagios3/nagios.cfg
root@mrtg:~# service nagios3 restart
redhat系统中nagios的配置文件地址如下所示。
/etc/nagios/objects 添加主机、服务,定义监测和定时器的配置文件
/usr/lib/nagios/plugins 实现监测的可执行文件
为特定类型的主机创建所需的模板,相应修改安装所需的文件。
[root@mrtg objects]# cd /etc/nagios/objects/
[root@mrtg objects]# vim templates.cfg
name linux-server
use generic-host
check_period 24x7
check_interval 3
retry_interval 1
max_check_attempts 3
check_command example-host-check
notification_period 24x7
notification_interval 0
notification_options d,u,r
contact_groups admins
register 0
name cisco-router
这里以默认的配置文件为例,主机和主机组添加至配置文件中。
[root@mrtg objects]# cp localhost.cfg example.cfg
[root@mrtg objects]# vim example.cfg
#adding linux server
use linux-server
host_name our-server
alias our-server
address 172.17.1.23
#adding cisco router
use cisco-router
host_name our-router
alias our-router
address 172.17.1.1
# host group definition
define hostgroup{
hostgroup_name linux-servers
members our-server
hostgroup_name cisco-router
alias cisco-router
members our-router
定义一个服务example-host-check:当往返时延达到100ms预警值并且有20%包丢失时发出警告,而紧急告警设置为5000ms且包丢失比率为100%,只执行一个ipv4的ping请求检测。
[root@mrtg objects]# vim commands.cfg
command_line $user1$/check_ping -h $hostaddress$ -w 100.0,20% -c 5000.0,100% -p 1 -4
告警要发送的邮件地址添加至nagios中。
[root@objects objects]# vim contacts.cfg
contact_name nagiosadmin
use generic-contact
alias nagios admin
email nagios@localhost, [email protected]
最后,准备启动nagios服务,可先试运行检测配置是否出错。
[root@mrtg ~]# nagios –v /etc/nagios/nagios.cfg
[root@mrtg ~]# service nagios restart
[root@mrtg ~]# chkconfig nagios on
若nagios没有依原设定运行,首先要做的是建立一个测试运行(dry run)。
debian或ubuntu系统:
# nagios3 -v /etc/nagios3/nagios.cfg
centos或rhel系统:
# nagios -v /etc/nagios/nagios.cfg
原文发布时间为:2014-01-03
本文来自云栖社区合作伙伴“linux中国”