CentOS/RHEL上的Nagios配置
RedHat系统中Nagios的配置文件地址如下所示。
/etc/nagios/objects 添加主机、服务,定义监测和定时器的配置文件 /usr/lib/nagios/plugins 实现监测的可执行文件
添加主机模板
为特定类型的主机创建所需的模板,相应修改安装所需的文件。
[root@mrtg objects]# cd /etc/nagios/objects/ [root@mrtg objects]# vim templates.cfg
define host{
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
}
define host{ name cisco-router
use generic-host
checkperiod 24x7
checkinterval 3 retryinterval 1 maxcheckattempts 3 checkcommand example-host-check notificationperiod 24x7 notificationinterval 0 notificationoptions d,u,r contactgroups admins register 0 }
添加主机和主机组
这里以默认的配置文件为例,主机和主机组添加至配置文件中。
[root@mrtg objects]# cp localhost.cfg example.cfg [root@mrtg objects]# vim example.cfg
#Adding Linux server
define host{
use linux-server
host_name our-server
alias our-server
address 172.17.1.23
}
#Adding Cisco Router
define host{
use cisco-router
host_name our-router
alias our-router
address 172.17.1.1
}
# HOST GROUP DEFINITION
define hostgroup{
hostgroup_name linux-servers
alias Linux Servers
members our-server
}
define hostgroup{
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
define command{
command_name example-host-check
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
define contact{ contact_name nagiosadmin
use generic-contact alias Nagios Admin email nagios@localhost, sentinel@example.tst }
最后,准备启动Nagios服务,可先试运行检测配置是否出错。
[root@mrtg ~]# nagios –v /etc/nagios/nagios.cfg [root@mrtg ~]# service nagios restart [root@mrtg ~]# chkconfig nagios on
配置后访问Nagios
现在一切就绪,可以开始Nagios之旅了。Ubuntu/Debian用户可以通过打开http://IP地址/nagios3网页访问Nagios,CentOS/RHEL用户可以打开http://IP地址/nagios,如http://172.17.1.23/nagios3来访问Nagios。“nagiosadmin”用户则需要认证来访问页面。
若Nagios没有依原设定运行,首先要做的是建立一个测试运行(dry run)。
Debian或Ubuntu系统:
# nagios3 -v /etc/nagios3/nagios.cfg
CentOS或RHEL系统:
# nagios -v /etc/nagios/nagios.cfg
日志文件也会提供重要线索,若需查看可以转至路径/var/log/nagios/nagios.log。
希望本文有所帮助。

