Nagios 监控运维完整教程 / 第4章:主机管理
第4章:主机管理
主机(Host)是 Nagios 监控体系的基本单位。本章详细讲解主机定义、主机检查、被动检查、父主机关系、主机分组及图标配置等核心内容。
一、主机定义基础
1.1 主机对象属性
define host {
host_name web-server-01 # 主机名称(唯一标识)
alias Production Web Server # 别名(描述性名称)
address 192.168.1.100 # IP 地址或域名
display_name Web Server 01 # 显示名称(可选)
# 检查配置
check_command check_ping # 检查命令
max_check_attempts 3 # 最大检查尝试次数
check_interval 5 # 检查间隔(分钟)
retry_interval 1 # 重试间隔(分钟)
check_period 24x7 # 检查时间段
# 通知配置
notifications_enabled 1 # 是否启用通知
notification_interval 60 # 通知间隔(分钟)
notification_options d,u,r # 通知选项
notification_period 24x7 # 通知时间段
contact_groups admins # 通知联系人组
# 事件处理
event_handler_enabled 1 # 是否启用事件处理器
event_handler restart-service # 事件处理器命令
# 状态保持
retain_status_information 1 # 保持状态信息
retain_nonstatus_information 1 # 保持非状态信息
# 高级选项
process_perf_data 1 # 是否处理性能数据
active_checks_enabled 1 # 是否启用主动检查
passive_checks_enabled 1 # 是否启用被动检查
obsess_over_host 1 # 是否监视主机
flap_detection_enabled 1 # 是否启用抖动检测
failure_prediction_enabled 1 # 是否启用故障预测
check_freshness 0 # 是否检查新鲜度
# 模板
use linux-server # 继承模板
# 图标和注释
icon_image linux40.gif # 图标图片
statusmap_image linux40.gd2 # 状态地图图标
vrml_image linux40.png # VRML 图标
2d_coords 100,200 # 2D 坐标
3d_coords 100.0,200.0,50.0 # 3D 坐标
}
1.2 最小化主机定义
实际使用中,通常通过模板继承简化定义:
# 模板定义
define host {
name linux-server
max_check_attempts 3
check_interval 5
retry_interval 1
check_period 24x7
notification_interval 120
notification_options d,u,r
notification_period workhours
contact_groups admins
check_command check_ping!100,20%!500,60%
register 0
}
# 实际主机定义(只需定义差异部分)
define host {
use linux-server
host_name web-server-01
alias Production Web Server
address 192.168.1.100
}
二、主机检查
2.1 主机检查流程
主机检查触发
│
▼
执行 check_command(通常为 check_ping)
│
├─ 返回 OK → 状态设为 UP
│
└─ 返回 CRITICAL → 是否达到 max_check_attempts?
├─ 否 → 状态设为 DOWN(软状态)
└─ 是 → 状态设为 DOWN(硬状态)→ 发送通知
2.2 常用主机检查命令
# check_ping - ICMP ping 检查
define command {
command_name check_ping
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5
}
# check_tcp - TCP 端口检查
define command {
command_name check_tcp_port
command_line $USER1$/check_tcp -H $HOSTADDRESS$ -p $ARG1$
}
# check_ssh - SSH 服务检查
define command {
command_name check_ssh_host
command_line $USER1$/check_ssh -H $HOSTADDRESS$ -p 22
}
# check_host_alive - 综合主机存活检查
define command {
command_name check_host_alive
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000,100% -c 5000,100% -p 1
}
2.3 主机检查配置详解
| 配置项 | 说明 | 默认值 | 建议值 |
|---|---|---|---|
max_check_attempts | 硬状态前的检查次数 | 3 | 关键主机 5, 一般 3 |
check_interval | 正常检查间隔(分钟) | 5 | 关键 1, 一般 5, 次要 15 |
retry_interval | 异常重试间隔(分钟) | 1 | 1-2 |
check_period | 允许检查的时间段 | 24x7 | 根据业务调整 |
check_command | 检查命令 | 无 | 必须定义 |
2.4 状态类型说明
| 状态类型 | 含义 | 触发条件 |
|---|---|---|
| HARD | 硬状态 | 达到 max_check_attempts |
| SOFT | 软状态 | 未达到 max_check_attempts |
# 示例:max_check_attempts = 3
# 第1次检查失败 → SOFT 1
# 第2次检查失败 → SOFT 2
# 第3次检查失败 → HARD 1 → 触发通知
三、被动检查
3.1 被动检查概念
被动检查(Passive Check)由外部系统提交结果,Nagios 不主动发起检查:
外部系统(脚本/监控代理)──→ 提交检查结果 ──→ Nagios Core
│
▼
处理被动检查结果
更新主机/服务状态
3.2 启用被动检查
# 主机定义中启用被动检查
define host {
use linux-server
host_name external-host
alias External Host
address 203.0.113.10
passive_checks_enabled 1
active_checks_enabled 0 # 可选:禁用主动检查
check_freshness 1 # 启用新鲜度检查
freshness_threshold 300 # 5 分钟未收到更新视为过期
}
3.3 提交被动检查结果
# 通过命令文件提交被动主机检查结果
# 语法: [时间戳] PROCESS_HOST_CHECK_RESULT;主机名;状态码;输出信息
# 状态码: 0=UP, 1=DOWN, 2=UNREACHABLE
# 提示: 使用当前时间戳
echo "[$(date +%s)] PROCESS_HOST_CHECK_RESULT;external-host;0;Host is UP" \
>> /var/log/nagios/rw/nagios.cmd
# 通过外部命令脚本提交
#!/bin/bash
CMD_FILE="/var/log/nagios/rw/nagios.cmd"
TIMESTAMP=$(date +%s)
HOST_NAME="external-host"
STATUS=0
OUTPUT="Host is alive - received heartbeat"
echo "[${TIMESTAMP}] PROCESS_HOST_CHECK_RESULT;${HOST_NAME};${STATUS};${OUTPUT}" \
>> ${CMD_FILE}
3.4 新鲜度检查
新鲜度检查确保被动检查的数据不会过期:
define host {
use linux-server
host_name snmp-device
alias SNMP Monitored Device
address 192.168.1.200
passive_checks_enabled 1
active_checks_enabled 0
check_freshness 1
freshness_threshold 600 # 10 分钟未收到更新触发主动检查
check_command check_snmp_alive # 过期时执行的检查命令
}
四、父主机关系
4.1 父主机概念
父主机(Parent Host)用于定义网络拓扑关系,帮助 Nagios 判断主机是否真正不可达:
Internet
│
▼
┌──────────────┐
│ Gateway │ (父主机)
│ 192.168.1.1 │
└──────────────┘
│ │
┌───────┘ └───────┐
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Web Server │ │ DB Server │
│ 192.168.1.10 │ │ 192.168.1.20 │
└──────────────┘ └──────────────┘
4.2 配置父主机
# 定义父主机(网关)
define host {
use generic-switch
host_name gateway
alias Network Gateway
address 192.168.1.1
parents internet-gateway # 网关的父主机
}
# 定义子主机
define host {
use linux-server
host_name web-server-01
alias Web Server 01
address 192.168.1.10
parents gateway # 指定父主机
}
define host {
use linux-server
host_name db-server-01
alias DB Server 01
address 192.168.1.20
parents gateway
}
4.3 多级父主机
# 多级网络拓扑
define host {
use generic-switch
host_name core-switch
alias Core Switch
address 10.0.0.1
parents internet-router
}
define host {
use generic-switch
host_name access-switch-1
alias Access Switch 1
address 10.0.1.1
parents core-switch
}
define host {
use linux-server
host_name server-01
alias Server 01
address 10.0.1.10
parents access-switch-1
}
4.4 父主机的作用
| 场景 | 无父主机 | 有父主机 |
|---|---|---|
| Gateway 宕机 | 所有主机报告 DOWN | 子主机报告 UNREACHABLE |
| 子主机宕机 | 子主机报告 DOWN | 子主机报告 DOWN |
| 通知效果 | 大量 DOWN 告警 | 只有 Gateway DOWN 告警 + UNREACHABLE |
状态区别:
DOWN:主机本身故障,需要处理UNREACHABLE:网络路径不通,可能是父节点故障
五、主机分组
5.1 主机组定义
define hostgroup {
hostgroup_name linux-servers
alias Linux Servers
members web-server-01,db-server-01,app-server-01
hostgroup_members production-servers,staging-servers # 嵌套主机组
notes All Linux servers in the data center
notes_url /nagios/cgi-bin/extinfo.cgi?type=3&group=linux-servers
action_url /nagios/cgi-bin/cmd.cgi?cmd_typ=55&hostgroup=linux-servers
}
define hostgroup {
hostgroup_name webservers
alias Web Servers
members web-server-01,web-server-02
}
define hostgroup {
hostgroup_name dbservers
alias Database Servers
members db-server-01,db-server-02
}
define hostgroup {
hostgroup_name network-devices
alias Network Devices
members core-switch,access-switch-1,access-switch-2
}
5.2 主机组属性
| 属性 | 说明 | 必需 |
|---|---|---|
hostgroup_name | 主机组名称(唯一) | 是 |
alias | 别名 | 是 |
members | 成员主机列表 | 否 |
hostgroup_members | 嵌套主机组 | 否 |
notes | 备注信息 | 否 |
notes_url | 备注链接 | 否 |
action_url | 操作链接 | 否 |
5.3 主机组在服务中的应用
# 为整个主机组定义服务
define service {
use generic-service
hostgroup_name linux-servers
service_description SSH
check_command check_ssh
}
define service {
use generic-service
hostgroup_name webservers
service_description HTTP
check_command check_http
}
六、图标和视觉配置
6.1 主机图标
define host {
use linux-server
host_name web-server-01
address 192.168.1.100
icon_image linux40.gif # 小图标
statusmap_image linux40.gd2 # 状态地图图标
vrml_image linux40.png # 3D 视图图标
}
6.2 常用图标
| 图标文件 | 适用对象 | 说明 |
|---|---|---|
linux40.gif | Linux 服务器 | Tux 企鹅图标 |
windows40.gif | Windows 服务器 | Windows 徽标 |
switch40.gif | 网络交换机 | 交换机图标 |
router40.gif | 路由器 | 路由器图标 |
server40.gif | 通用服务器 | 通用服务器图标 |
printer40.gif | 打印机 | 打印机图标 |
database40.gif | 数据库 | 数据库图标 |
disk40.gif | 存储设备 | 磁盘图标 |
6.3 状态地图坐标
# 2D 坐标(用于状态地图)
define host {
use linux-server
host_name web-server-01
address 192.168.1.100
2d_coords 100,200 # x,y 坐标
}
# 3D 坐标(用于 3D 地图)
define host {
use linux-server
host_name db-server-01
address 192.168.1.200
3d_coords 100.0,200.0,50.0 # x,y,z 坐标
}
七、主机扩展信息
7.1 定义扩展信息
define hostextinfo {
host_name web-server-01
icon_image linux40.gif
icon_image_alt Linux Server
vrml_image linux40.png
statusmap_image linux40.gd2
2d_coords 100,200
3d_coords 100.0,200.0,50.0
notes Production web server running Apache
notes_url /nagios/cgi-bin/extinfo.cgi?type=1&host=web-server-01
action_url /nagios/cgi-bin/cmd.cgi?cmd_typ=55&host=web-server-01
}
八、业务场景示例
8.1 Web 集群监控
# Web 服务器主机
define host {
use linux-server
host_name web-01
alias Web Server 01
address 10.0.1.10
parents lb-01
icon_image linux40.gif
}
define host {
use linux-server
host_name web-02
alias Web Server 02
address 10.0.1.11
parents lb-01
icon_image linux40.gif
}
# 负载均衡器
define host {
use linux-server
host_name lb-01
alias Load Balancer 01
address 10.0.1.1
parents core-switch
icon_image server40.gif
}
# 核心交换机
define host {
use generic-switch
host_name core-switch
alias Core Switch
address 10.0.0.1
icon_image switch40.gif
}
# 主机组
define hostgroup {
hostgroup_name web-cluster
alias Web Cluster
members web-01,web-02,lb-01
}
8.2 多数据中心监控
# 北京数据中心
define host {
use linux-server
host_name bj-gateway
alias Beijing Gateway
address 172.16.1.1
}
define host {
use linux-server
host_name bj-web-01
alias Beijing Web 01
address 172.16.1.10
parents bj-gateway
}
# 上海数据中心
define host {
use linux-server
host_name sh-gateway
alias Shanghai Gateway
address 172.16.2.1
}
define host {
use linux-server
host_name sh-web-01
alias Shanghai Web 01
address 172.16.2.10
parents sh-gateway
}
# 分组
define hostgroup {
hostgroup_name beijing-dc
alias Beijing Data Center
members bj-gateway,bj-web-01
}
define hostgroup {
hostgroup_name shanghai-dc
alias Shanghai Data Center
members sh-gateway,sh-web-01
}
九、注意事项
| 注意事项 | 说明 |
|---|---|
| host_name 唯一性 | 每个主机的 host_name 必须唯一 |
| 地址可解析 | address 可以是 IP 或可解析的域名 |
| 父主机拓扑 | 合理设置父主机避免误告警 |
| 检查间隔 | 根据主机重要性设置不同间隔 |
| 通知策略 | 重要主机缩短通知间隔 |
| 分组管理 | 使用 hostgroup 简化服务定义 |
十、本章小结
- 主机定义是监控的基础,需合理规划命名和地址
- 主动检查由 Nagios 发起,被动检查由外部提交
- 父主机关系帮助区分真正故障和网络不可达
- 主机组简化批量服务配置
- 图标配置提升 Web 界面可视化效果
下一章:第5章:服务管理 - 学习如何定义和管理主机上的监控服务。