透过 Nagios 持续监控网络及服务器(中)
我们 上一篇文章 安装了 Nagios Core 并提升了管理界面的安全性,这次让我们监控 Linux 节点及常见 Web 服务吧。
实验环境概括
我们将重用上一篇文章的实验环境,并追加一个 HTTP 服务器及一个 DNS 服务器:
- 虚拟化:阿里云 ECS
- 内网网段:
192.168.1.0/24
角色 | 内网 IP 地址 | 公网 IP 地址 | 操作系统 |
---|---|---|---|
Nagios 服务器 | 192.168.1.207 |
47.242.247.225 |
Alibaba Cloud Linux 3.2104 LTS |
OpenVPN 服务器 | 192.168.1.208 |
47.242.158.8 |
Alibaba Cloud Linux 3.2104 LTS |
HTTP 服务器 | 192.168.1.209 |
47.242.149.199 |
Alibaba Cloud Linux 3.2104 LTS |
DNS 服务器 | 192.168.1.210 |
8.210.111.115 |
Alibaba Cloud Linux 3.2104 LTS |
客户端(笔记本电脑) | - | - | deepin 20.8 Community |
配置实验环境
首先启动 Nagios、OpenVPN 服务器,并把客户端连上 VPN,造访 http://192.168.1.207/nagios 并输入管理员名称及密码,若成功应该能如常造访管理界面:
监控 OpenVPN 节点
当前,我们的 Nagios 服务器只监控自己 localhost
的各项指标,但我们当然希望它也能监控我们网络中其他的节点与服务。其中,我们提升 Nagios 管理界面的安全性后搭建了个 OpenVPN 服务器并透过连上该服务器进入内网造访安全加固后的管理界面,所以我们下一个要监控的目标理所当然的正是这台 OpenVPN 服务器。
要增加监控对象就要修改配置文档:
/etc/nagios/nagios.cfg
是 Nagios 主要的配置文档/etc/nagios/objects/templates.cfg
包含常见 host 及服务定义,例如linux-server
、windows-server
等等/etc/nagios/objects/localhost.cfg
定义了localhost
服务器及监控该服务器的各项指标
有兴趣的小伙伴们可自行研究 /etc/nagios/objects/templates.cfg
及 /etc/nagios/
目录下其他的配置文档;时间关系,我们只看主要配置文档及 localhost
配置文档。
首先看看 localhost
配置文档。
define host {
use linux-server ; Name of host template to use
; This host definition will inherit all variables that are defined
; in (or inherited by) the linux-server host template definition.
host_name localhost
alias localhost
address 127.0.0.1
}
define host
定义一个节点的监控对象, use linux-server
使用 templates.cfg
下常见的 linux-server
定义,然后 host_name
、alias
定义该节点的名称,address
是该节点的 IP 地址。
define hostgroup {
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members localhost ; Comma separated list of hosts that belong to this group
}
hostgroup
是对节点的分类,把相关的节点放到一个群组,名称为 linux-servers
,Web 端显示为 Linux Servers
较为易读,该群组现阶段只包含 localhost
。
然后就是各种各样的服务、指标,皆称为 service
,例如 PING 或进程总数:
...
define service {
use local-service ; Name of service template to use
host_name localhost
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
...
define service {
use local-service ; Name of service template to use
host_name localhost
service_description Total Processes
check_command check_local_procs!250!400!RSZDT
}
...
然后再看看 /etc/nagios/nagios.cfg
主要配置文档,我们暂时只关注这行:
# Definitions for monitoring the local (Linux) host
cfg_file=/etc/nagios/objects/localhost.cfg
这行使主配置文档读入我们 localhost
的配置文档,不然 Nagios 不会自己去读 /etc/nagios/objects/localhost.cfg
。
了解这两个配置文档后,把 localhost
文档拷到 /etc/nagios/objects/openvpn-server.cfg
,我们将透过新的配置文档定义如何监控我们的 OpenVPN 服务器:
sudo cp /etc/nagios/objects/localhost.cfg /etc/nagios/objects/openvpn-server.cfg
打开 /etc/nagios/objects/openvpn-server.cfg
编辑,把这个 host 从 localhost
改为 openvpn-server
,并修改对应的 IP 地址:
# 旧
define host {
use linux-server ; Name of host template to use
; This host definition will inherit all variables that are defined
; in (or inherited by) the linux-server host template definition.
host_name localhost
alias localhost
address 127.0.0.1
}
# 新
def