如何在 Linux 上安装和配置 Nagios
Nagios是最强大的网络监控系统之一,在业界得到广泛应用。它可以主动监控任何网络,并在检测到任何问题时生成音频/电子邮件警告和警报。检查类型和警报计时器是完全可定制的。
Nagios 的另一个令人难以置信的功能是它可以监控主机和服务,例如监控 IP 地址和 TCP/UDP 端口号。为了解释一下,让我们假设有一个我们想要监控的 Web 服务器。 Nagios 可以通过在服务器的 IP/名称上运行 ping
来检查服务器是否在线,并且可以将其设置为在服务器的往返时间 (RTT) 增加时提供警告。此外,Nagios 还可以检查 TCP 端口 80(Web 服务器)是否可达,例如服务器在线但 Apache/IIS 没有响应。
Nagio 监控可以通过远程插件执行器进一步扩展。还有基于Nagios的第三方监控工具,例如Centreon、FAN、op5 Monitor,它们在界面、自动化和技术支持方面补充了独立的Nagios引擎。
本教程介绍如何在 Linux 上安装和配置 Nagios。
在 Ubuntu 或 Debian 上安装 Nagios
在基于 Debian 的系统上,由于 apt-get,安装本身是一个非常简单的过程。
[email :~# apt-get install nagios3
邮件服务器设置可以在Nagios安装期间完成。也可以在以后需要时进行配置。
注意:Nagios 需要有效的 SMTP 配置才能发送电子邮件通知。

可以看出,Nagios 支持多种电子邮件传送选项。最常见的选项是服务器直接向收件人发送电子邮件的互联网站点。另一种广泛使用的选项是使用智能主机或中继服务器,其中服务器将电子邮件发送到中间邮件服务器,而中间邮件服务器又负责将邮件传递给收件人。
接下来,下一步必须包含服务器的域名。

最后,设置 Nagios 管理员用户 nagiosadmin
的密码。该值也可以稍后更改。

在 CentOS 或 RHEL 上安装 Nagios
使用yum
进行安装。设置 repoforge 存储库后,按如下方式运行 yum。
[email :~# yum install nagios nagios-plugins
监控要求
在本教程中,我们假设我们有以下监控需求。
所有 Linux 服务器将每 3 分钟检查一次。
所有 Cisco 路由器将每 3 分钟检查一次。
所有电子邮件提醒均应发送至地址 [email 。
Nagios 在发送任何警报之前将验证 3 次,以确保问题确实存在。
如果任何设备的 RTT 超过 100 毫秒和/或数据包丢失超过 20%,将生成电子邮件警报。
本教程的其余部分将指导您在 Linux 上配置 Nagios。
Ubuntu 上的 Nagios 配置
了解 Nagios 配置文件的位置非常重要。下表显示了 Nagios 配置文件在基于 Debian 的系统上的位置。
/etc/nagios-plugins |
Customizable scripts used for monitoring |
/etc/nagios3 |
Configuration files to add hosts, services, define checks and timers |
/usr/lib/nagios/plugins |
Executable files used for monitoring |
以下步骤是相互关联的。定义主机、主机组以及向主机组添加服务。
添加主机模板
定义如何处理类型主机的模板已定义。我们使用安装时提供的文件作为示例。
首先,为 Linux 设备定义主机模板。
[email :~# cd /etc/nagios3/conf.d
[email :/etc/nagios3/conf.d/# cp generic-host_nagios2.cfg linux-server.cfg
按如下方式编辑linux-server.cfg
。加粗部分为修改内容。
[email :/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 ; the script to be used while checking
check_interval 3 ; the interval between consecutive checks
max_check_attempts 3 ; number of rechecking before generating email alerts
notification_interval 0
notification_period 24x7
notification_options d,u,r
contact_groups admins ; the group where emails will be sent
register 0
}
接下来,为 Cisco 设备定义主机模板。
[email :/etc/nagios3/conf.d/# cp linux-server.cfg cisco-device.cfg
按如下方式修改 cisco-device.cfg
。粗体部分已修改。
[email :/etc/nagios3/conf.d/# vim cisco-device.cfg
# The highlighted parts are modified
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 ; the script to be used while checking
check_interval 3 ; the interval between consecutive checks
max_check_attempts 3 ; number of rechecking before generating email alerts
notification_interval 0
notification_period 24x7
notification_options d,u,r
contact_groups admins ; the group where emails will be sent
register 0
}
添加主机
现在已经定义了主机模板,接下来添加要监视的实际主机。同样,默认提供的文件用作示例。
[email :/etc/nagios3/conf.d/# cp localhost_nagios2.cfg example.cfg
[email :/etc/nagios3/conf.d/# vim example.cfg
# Host 1
define host{
use linux-server ; Name of host template to use
host_name our-server ; The hostname to be used by nagios
alias our-server
address 172.17.1.23 ; The IP address of the host
}
# Host 2
define host{
use cisco-device ; Name of host template to use
host_name our-router ; The hostname to be used by nagios
alias our-router
address 172.17.1.1 ; The IP address of the host
}
主机组定义
当存在多台主机时,为了便于管理,建议将相似类型的主机分组在一起。
[email :/etc/nagios3/conf.d/# vim hostgroups_nagios2.cfg
definehostgroup {
hostgroup_name linux-server ; the name of the host group
alias Linux Servers
members our-server ; comma separated list of members
}
definehostgroup {
hostgroup_name cisco-device ; the name of the host group
alias Cisco Devices
members our-server ; comma separated list of members
}
服务定义
首先,定义命令 example-host-check
,延迟警告值为 100 ms
,丢包警告值为 20%
。延迟和100%
数据包丢失的关键值为5000 ms
。 一个
传输 IPv4 ping 请求。
[email :~# 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
}
然后,该命令与主机组关联。
[email :/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 ; set > 0 if you want to be renotified
}
define service {
hostgroup_name cisco-device
service_description Cisco Devices
check_command example-host-check
use generic-service
notification_interval 0 ; set > 0 if you want to be renotified
}
联系定义
发送电子邮件所需的地址已添加到 Nagios。
[email :/etc/nagios3/conf.d/# vim contacts_nagios2.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 [email , [email
}
最后启动空运行,检查是否存在配置错误。如果没有错误,Nagios 可以安全(重新)启动。
[email :~#nagios –v /etc/nagios3/nagios.cfg
[email :~# service nagios3 restart
CentOS/RHEL 上的 Nagios 配置
下面显示了 Nagios 配置文件在基于 Redhat 的系统上的位置。
/etc/nagios/objects |
Configuration files to add hosts, services, define checks and timers |
/usr/lib/nagios/plugins |
Executable files used for monitoring |
添加主机模板
创建模板是为了定义需要为特定类型的主机执行哪些操作。安装时提供的文件已被修改。
cd /etc/nagios/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
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
}
添加主机和主机组
以默认提供的配置文件作为示例。主机和主机组添加在同一文件中。
cp localhost.cfg example.cfg
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
的服务定义了延迟警告值为 100 ms
和丢包警告值为 20%
。延迟的关键值为 5000 ms
,丢包的关键值为 100%
。仅传输一个
IPv4 ping 请求。
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 中。
vim contacts.cfg
define contact{
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
email [email , [email
}
最后,我们准备启动Nagios服务。建议进行试运行以找出配置中的错误。
nagios –v /etc/nagios/nagios.cfg
service nagios restart
chkconfig nagios on
配置后访问Nagios
现在您已准备好使用 Nagios。 Nagios 可以通过打开 URL http://
(对于 Ubuntu/Debian)或 http://
(对于 CentOS/RHEL)来访问。用户 nagiosadmin
需要经过身份验证才能访问该页面。

Nagios 故障排除
当 Nagios 未按预期运行时,要做的第一件事就是启动试运行。
在 Debian 或 Ubuntu 上:
# nagios3 -v /etc/nagios3/nagios.cfg
在 CentOS 或 RHEL 上:
# nagios -v /etc/nagios/nagios.cfg
日志文件也可能揭示一些重要的线索。日志文件的路径位于 /var/log/nagios/nagios.log
。
希望这可以帮助。