第5章:服务管理
第5章:服务管理
服务(Service)是 Nagios 监控的核心执行单元,代表主机上的具体检查项。本章详细讲解服务定义、检查命令、阈值设置、服务依赖、时间段配置和通知策略。
一、服务定义基础
1.1 服务对象属性
define service {
# 基本属性
host_name web-server-01 # 所属主机
service_description HTTP # 服务描述(唯一标识)
display_name Web Service # 显示名称
servicegroups web-services # 所属服务组
# 检查配置
check_command check_http!80!/ # 检查命令
max_check_attempts 3 # 最大检查尝试次数
check_interval 5 # 正常检查间隔(分钟)
retry_interval 1 # 重试间隔(分钟)
check_period 24x7 # 检查时间段
# 通知配置
notifications_enabled 1 # 启用通知
notification_interval 60 # 通知间隔(分钟)
notification_options w,u,c,r # 通知选项
notification_period 24x7 # 通知时间段
contact_groups admins # 联系人组
# 高级选项
is_volatile 0 # 是否为易失性服务
active_checks_enabled 1 # 启用主动检查
passive_checks_enabled 1 # 启用被动检查
parallelize_check 1 # 并行检查
obsess_over_service 1 # 监视服务
check_freshness 0 # 新鲜度检查
event_handler_enabled 1 # 事件处理器
flap_detection_enabled 1 # 抖动检测
process_perf_data 1 # 处理性能数据
retain_status_information 1 # 保持状态信息
retain_nonstatus_information 1 # 保持非状态信息
failure_prediction_enabled 1 # 故障预测
# 模板
use generic-service # 继承模板
}
1.2 最小化服务定义
# 模板(templates.cfg)
define service {
name generic-service
max_check_attempts 3
check_interval 5
retry_interval 1
check_period 24x7
notification_interval 60
notification_options w,u,c,r
notification_period 24x7
contact_groups admins
register 0
}
# 实际服务定义(只需定义差异部分)
define service {
use generic-service
host_name web-server-01
service_description HTTP
check_command check_http
}
二、服务检查命令
2.1 常用检查命令定义
# HTTP 检查
define command {
command_name check_http
command_line $USER1$/check_http -H $HOSTADDRESS$ -p $ARG1$ -u $ARG2$
}
# HTTPS 检查
define command {
command_name check_https
command_line $USER1$/check_http -H $HOSTADDRESS$ -S -p $ARG1$ -u $ARG2$ --sni
}
# SSH 检查
define command {
command_name check_ssh
command_line $USER1$/check_ssh -H $HOSTADDRESS$ -p 22
}
# DNS 检查
define command {
command_name check_dns
command_line $USER1$/check_dns -H $ARG1$ -s $HOSTADDRESS$
}
# 磁盘使用率检查
define command {
command_name check_disk
command_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
}
# CPU 负载检查
define command {
command_name check_load
command_line $USER1$/check_load -w $ARG1$ -c $ARG2$
}
# 内存使用率检查
define command {
command_name check_memory
command_line $USER1$/check_mem -w $ARG1$ -c $ARG2$ -f
}
# MySQL 检查
define command {
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u $USER5$ -p $USER6$ -d $ARG1$
}
# 进程数检查
define command {
command_name check_procs
command_line $USER1$/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$
}
# 日志文件检查
define command {
command_name check_log
command_line $USER1$/check_log -F $ARG1$ -O $ARG2$ -q $ARG3$
}
# TCP 端口检查
define command {
command_name check_tcp
command_line $USER1$/check_tcp -H $HOSTADDRESS$ -p $ARG1$ -t 10
}
# UDP 端口检查
define command {
command_name check_udp
command_line $USER1$/check_udp -H $HOSTADDRESS$ -p $ARG1$ -s "$ARG2$" -e "$ARG3$"
}
2.2 阈值参数格式
不同插件的阈值格式不同,常见格式:
| 插件类型 | 阈值格式 | 示例 |
|---|
| 数值范围 | @min:max | -w @1:10 -c @0:20 |
| 简单阈值 | value | -w 80 -c 95 |
| 百分比 | value% | -w 80% -c 95% |
| 时间范围 | value[unit] | -w 1s -c 5s |
| 复合阈值 | w1,w2 -c c1,c2 | -w 80,50% -c 95,80% |
2.3 命令参数传递
# 单参数
define service {
use generic-service
host_name web-server-01
service_description HTTP
check_command check_http!80
# ARG1 = 80
}
# 多参数
define service {
use generic-service
host_name web-server-01
service_description HTTP
check_command check_http!80!/login!200!Welcome
# ARG1 = 80, ARG2 = /login, ARG3 = 200, ARG4 = Welcome
}
# 磁盘检查示例
define service {
use generic-service
host_name db-server-01
service_description Root Partition
check_command check_disk!10%!5%!/
# 警告阈值 = 10%, 严重阈值 = 5%, 检查路径 = /
}
三、服务状态与阈值
3.1 服务状态定义
| 状态 | 含义 | 典型触发条件 |
|---|
| OK | 正常 | 所有指标在阈值内 |
| WARNING | 警告 | 指标接近阈值 |
| CRITICAL | 严重 | 指标超过严重阈值 |
| UNKNOWN | 未知 | 插件异常或无法判断 |
3.2 阈值设计模式
# 磁盘使用率阈值
check_command check_disk!10%!5%!/
# 含义: 使用率 > 90% 报 WARNING, > 95% 报 CRITICAL
# CPU 负载阈值
check_command check_load!5,4,3!10,8,6
# 含义: 1分钟负载 > 5 或 5分钟 > 4 或 15分钟 > 3 → WARNING
# 1分钟负载 > 10 或 5分钟 > 8 或 15分钟 > 6 → CRITICAL
# 内存使用率
check_command check_memory!80%!90%
# 含义: 使用率 > 80% → WARNING, > 90% → CRITICAL
# HTTP 响应时间
check_command check_http!5!10
# 含义: 响应时间 > 5s → WARNING, > 10s → CRITICAL
3.3 复合阈值示例
# check_ping 的复合阈值
check_command check_ping!100.0,20%!500.0,60%
# 含义: RTA > 100ms 或 丢包率 > 20% → WARNING
# RTA > 500ms 或 丢包率 > 60% → CRITICAL
# check_http 的完整示例
define command {
command_name check_http_full
command_line $USER1$/check_http -H $HOSTADDRESS$ -p $ARG1$ -u $ARG2$ \
-w $ARG3$ -c $ARG4$ -e 200 -s "$ARG5$"
}
define service {
use generic-service
host_name web-server-01
service_description HTTP Login Page
check_command check_http_full!443!/login!5!10!Welcome
}
四、服务依赖
4.1 依赖概念
服务依赖用于避免级联告警:
Web 服务 (HTTP)
│
└── 依赖于 → 数据库服务 (MySQL)
│
└── 依赖于 → 磁盘服务 (Root Disk)
4.2 依赖定义
# HTTP 依赖于 MySQL
define servicedependency {
host_name web-server-01
service_description HTTP
dependent_host_name web-server-01
dependent_service_description MySQL Connection
execution_failure_criteria c,u # 执行失败标准
notification_failure_criteria c,u # 通知失败标准
inherits_parent 1 # 继承父依赖
}
# MySQL 依赖于磁盘
define servicedependency {
host_name db-server-01
service_description MySQL
dependent_host_name db-server-01
dependent_service_description Root Disk
execution_failure_criteria c
notification_failure_criteria c
}
# 跨主机依赖
define servicedependency {
host_name db-server-01
service_description MySQL
dependent_host_name web-server-01
dependent_service_description HTTP
execution_failure_criteria w,u,c
notification_failure_criteria w,u,c
}
4.3 依赖失败标准
| 标志 | 含义 | 说明 |
|---|
o | OK | 依赖服务为 OK 时触发 |
w | WARNING | 依赖服务为 WARNING 时触发 |
u | UNKNOWN | 依赖服务为 UNKNOWN 时触发 |
c | CRITICAL | 依赖服务为 CRITICAL 时触发 |
n | None | 任何状态都不触发(无依赖) |
p | Pending | 依赖服务处于 Pending 状态时触发 |
4.4 主机依赖
define hostdependency {
host_name gateway
dependent_host_name web-server-01
execution_failure_criteria d,u
notification_failure_criteria d,u
inherits_parent 1
}
五、时间段配置
5.1 时间段定义
# 24x7 全天候
define timeperiod {
timeperiod_name 24x7
alias 24 Hours A Day, 7 Days A Week
sunday 00:00-24:00
monday 00:00-24:00
tuesday 00:00-24:00
wednesday 00:00-24:00
thursday 00:00-24:00
friday 00:00-24:00
saturday 00:00-24:00
}
# 工作时间(周一到周五 9:00-18:00)
define timeperiod {
timeperiod_name workhours
alias Normal Work Hours
monday 09:00-18:00
tuesday 09:00-18:00
wednesday 09:00-18:00
thursday 09:00-18:00
friday 09:00-18:00
}
# 非工作时间
define timeperiod {
timeperiod_name nonworkhours
alias Non-Work Hours
sunday 00:00-24:00
monday 00:00-09:00,18:00-24:00
tuesday 00:00-09:00,18:00-24:00
wednesday 00:00-09:00,18:00-24:00
thursday 00:00-09:00,18:00-24:00
friday 00:00-09:00,18:00-24:00
saturday 00:00-24:00
}
# 周末
define timeperiod {
timeperiod_name weekends
alias Weekends
sunday 00:00-24:00
saturday 00:00-24:00
}
# 排除节假日
define timeperiod {
timeperiod_name workhours-with-exceptions
alias Work Hours With Exceptions
use workhours
exclude holidays
}
define timeperiod {
timeperiod_name holidays
alias Holidays
january 1 00:00-24:00 # 元旦
july 4 00:00-24:00 # 国庆节
december 25 00:00-24:00 # 圣诞节
}
5.2 时间段应用
# 检查时间段:何时执行检查
define service {
...
check_period 24x7
}
# 通知时间段:何时发送通知
define service {
...
notification_period workhours
}
# 组合使用:全天检查,但只在工作时间通知
define service {
use generic-service
host_name dev-server-01
service_description Disk Space
check_command check_disk!20%!10%!/
check_period 24x7
notification_period workhours
}
六、服务通知
6.1 通知选项
notification_options w,u,c,r
# w = WARNING
# u = UNKNOWN
# c = CRITICAL
# r = RECOVERY(从异常恢复到正常)
# n = None(不发送通知)
6.2 通知场景配置
# 关键服务:所有状态变化都通知
define service {
use generic-service
host_name critical-server
service_description Core Application
check_command check_app
notification_options w,u,c,r
notification_interval 15 # 15分钟重复通知
contact_groups critical-ops
}
# 一般服务:只通知严重问题
define service {
use generic-service
host_name dev-server-01
service_description Log Rotation
check_command check_logrotate
notification_options c,r
notification_interval 1440 # 24小时重复通知
contact_groups ops-team
}
# 监控服务:不通知,仅记录
define service {
use generic-service
host_name any-host
service_description SNMP Community
check_command check_snmp_community
notifications_enabled 0
}
6.3 通知间隔配置
| 场景 | 通知间隔 | 说明 |
|---|
| 核心业务 | 5-15 分钟 | 快速响应 |
| 一般服务 | 30-60 分钟 | 正常响应 |
| 非关键服务 | 120-1440 分钟 | 低优先级 |
| 已知问题 | 0(仅首次) | 不重复通知 |
七、服务组
7.1 服务组定义
define servicegroup {
servicegroup_name web-services
alias Web Services
members web-server-01,HTTP,web-server-01,HTTPS,web-server-01,HTTP Login
notes All web-related services
notes_url /nagios/cgi-bin/extinfo.cgi?type=5&servicegroup=web-services
action_url /nagios/cgi-bin/cmd.cgi?cmd_typ=84&servicegroup=web-services
}
define servicegroup {
servicegroup_name db-services
alias Database Services
members db-server-01,MySQL,db-server-01,MySQL Slave,db-server-01,MySQL Disk
}
7.2 服务组的成员格式
# 格式: 主机名,服务描述,主机名,服务描述,...
members host1,service1,host2,service2
# 示例:
members web-01,HTTP,web-01,HTTPS,web-02,HTTP,web-02,HTTPS
八、易失性服务
8.1 概念
易失性服务(Volatile Service)每次检查结果都触发通知,即使状态未变化:
define service {
use generic-service
host_name security-server
service_description Intrusion Detection
check_command check_ids_alerts
is_volatile 1 # 启用易失性
notification_options w,u,c,r
max_check_attempts 1 # 不重试
}
8.2 适用场景
| 场景 | 说明 |
|---|
| 安全告警 | 每次告警都需要通知 |
| 日志监控 | 每次发现异常日志都需要通知 |
| 计数监控 | 每次计数变化都通知 |
九、业务场景示例
9.1 Web 服务器监控配置
# HTTP 检查
define service {
use generic-service
host_name web-server-01
service_description HTTP
check_command check_http!80!/
contact_groups web-ops
}
# HTTPS 检查
define service {
use generic-service
host_name web-server-01
service_description HTTPS
check_command check_https!443!/
contact_groups web-ops
}
# SSL 证书检查
define service {
use generic-service
host_name web-server-01
service_description SSL Certificate
check_command check_ssl_cert!30!7
check_interval 1440 # 每天检查一次
contact_groups web-ops,security-team
}
# 磁盘检查
define service {
use generic-service
host_name web-server-01
service_description Root Partition
check_command check_disk!10%!5%!/
}
# 磁盘检查 - /var
define service {
use generic-service
host_name web-server-01
service_description Var Partition
check_command check_disk!15%!10%!/var
}
# CPU 负载
define service {
use generic-service
host_name web-server-01
service_description CPU Load
check_command check_load!5,4,3!10,8,6
}
# 内存使用率
define service {
use generic-service
host_name web-server-01
service_description Memory Usage
check_command check_memory!80%!95%
}
# 进程数
define service {
use generic-service
host_name web-server-01
service_description Apache Processes
check_command check_procs!5!10!httpd
}
# SSH 服务
define service {
use generic-service
host_name web-server-01
service_description SSH
check_command check_ssh
}
9.2 数据库服务器监控配置
# MySQL 检查
define service {
use generic-service
host_name db-server-01
service_description MySQL
check_command check_mysql_health!connection-time!5!10
contact_groups dba-team
}
# MySQL 连接数
define service {
use generic-service
host_name db-server-01
service_description MySQL Connections
check_command check_mysql_health!threads-connected!150!200
contact_groups dba-team
}
# MySQL 慢查询
define service {
use generic-service
host_name db-server-01
service_description MySQL Slow Queries
check_command check_mysql_health!slow-queries!10!50
notification_options c,r
contact_groups dba-team
}
# MySQL 复制状态
define service {
use generic-service
host_name db-slave-01
service_description MySQL Replication
check_command check_mysql_health!slave-lag!60!300
contact_groups dba-team
}
9.3 网络设备监控配置
# SNMP 接口状态
define service {
use generic-service
host_name core-switch
service_description Interface Eth0
check_command check_snmp_interface!eth0!100M!90%
contact_groups network-ops
}
# SNMP CPU 使用率
define service {
use generic-service
host_name core-switch
service_description CPU Usage
check_command check_snmp_cpu!80%!95%
contact_groups network-ops
}
# SNMP 内存使用率
define service {
use generic-service
host_name core-switch
service_description Memory Usage
check_command check_snmp_mem!80%!95%
contact_groups network-ops
}
十、注意事项
| 注意事项 | 说明 |
|---|
| service_description 唯一 | 同一主机上的服务描述必须唯一 |
| 阈值合理性 | 阈值设置过严会产生告警风暴 |
| 检查间隔平衡 | 过于频繁影响性能,过于稀疏延迟发现问题 |
| 通知升级 | 重要服务应配置通知升级策略 |
| 依赖关系 | 合理设置依赖避免级联告警 |
| 被动检查 | 对于无法主动检查的服务使用被动检查 |
十一、本章小结
- 服务定义通过
check_command 关联检查逻辑 - 阈值设置需要根据业务特点合理调整
- 服务依赖避免级联告警
- 时间段配置控制检查和通知的执行时机
- 服务组便于批量管理相关服务
下一章:第6章:联系人与通知 - 学习如何配置联系人、通知命令和通知升级策略。