Ubuntu下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
define host{
name cisco-device ;名称,需修改
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 ; 邮件将要发至的组,需修改
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
define host{
use linux-server ; 使用的主机模板名
host_name our-server ; nagios使用的主机名
alias our-server
address 172.17.1.23 ; 主机的IP地址
}
# Host 2
define host{
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 ; 组员列表
}
define hostgroup {
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
}
define service {
hostgroup_name cisco-device
service_description Cisco Devices
check_command example-host-check
use generic-service
notification_interval 0 ; 初始化设置为0
}
联系人定义
进行如下定义将发送邮件需要的地址添加至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, sentinel@example.tst
}
最后,试运行初始化检测是否有配置错误。如果没有错误,Nagios开始安全运行。
root@mrtg:~#nagios –v /etc/nagios3/nagios.cfg root@mrtg:~# service nagios3 restart
