外观
MySQL Nagios 监控
Nagios 核心组件
1. Nagios Core
核心功能:
- 监控主机和服务的状态
- 处理告警通知
- 生成监控报告
- 提供 Web 界面
主要组件:
- Nagios Daemon:核心监控进程
- Nagios Web Interface:Web 管理界面
- Nagios Plugins:监控插件
- NRPE (Nagios Remote Plugin Executor):远程插件执行器
2. 监控插件
内置插件:
check_ping:检查网络连通性check_http:检查 HTTP 服务check_ssh:检查 SSH 服务
MySQL 专用插件:
check_mysql:检查 MySQL 连接check_mysql_health:检查 MySQL 健康状态check_mysql_query:执行自定义查询并检查结果
3. 配置文件
主要配置文件:
nagios.cfg:主配置文件objects/commands.cfg:命令定义objects/contacts.cfg:联系人定义objects/timeperiods.cfg:时间周期定义objects/templates.cfg:模板定义objects/localhost.cfg:本地主机配置
Nagios 安装与配置
1. 安装 Nagios Core
在 CentOS/RHEL 上安装:
bash
# 安装依赖
yum install -y gcc glibc glibc-common make gettext automake autoconf wget openssl-devel net-snmp net-snmp-utils epel-release
# 安装 NRPE
yum install -y nrpe nagios-plugins-all
# 安装 Nagios Core
tar -xzf nagios-4.4.6.tar.gz
cd nagios-4.4.6
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-commandmode
make install-config
make install-webconf
# 创建 nagios 用户和组
useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd apache
# 启动 Nagios
systemctl start nagios
systemctl enable nagios
systemctl start httpd
systemctl enable httpd在 Ubuntu/Debian 上安装:
bash
# 安装依赖
apt update
apt install -y build-essential libssl-dev libgd-dev libsnmp-dev libnet-snmp-perl
# 安装 Nagios Core
apt install -y nagios3 nagios-plugins-contrib
# 安装 NRPE
apt install -y nagios-nrpe-server2. 安装 MySQL 监控插件
安装 check_mysql 插件:
bash
# 该插件通常包含在 nagios-plugins-all 包中
# 验证插件是否存在
ls -la /usr/lib64/nagios/plugins/check_mysql安装 check_mysql_health 插件:
bash
# 下载插件
tar -xzf check_mysql_health-3.2.1.tar.gz
cd check_mysql_health-3.2.1
./configure
make
make install
# 验证插件是否安装成功
/usr/local/nagios/libexec/check_mysql_health --help3. 配置 MySQL 监控用户
创建监控用户:
sql
CREATE USER 'nagios'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, PROCESS, SUPER, REPLICATION CLIENT ON *.* TO 'nagios'@'localhost';
FLUSH PRIVILEGES;创建监控用户(远程监控):
sql
CREATE USER 'nagios'@'nagios-server-ip' IDENTIFIED BY 'password';
GRANT SELECT, PROCESS, SUPER, REPLICATION CLIENT ON *.* TO 'nagios'@'nagios-server-ip';
FLUSH PRIVILEGES;MySQL 监控配置
1. 配置命令
编辑 commands.cfg 文件:
txt
# 添加 MySQL 连接检查命令
define command {
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$ -d $ARG3$
}
# 添加 MySQL 健康检查命令
define command {
command_name check_mysql_health
command_line $USER1$/check_mysql_health --hostname=$HOSTADDRESS$ --username=$ARG1$ --password=$ARG2$ --mode=$ARG3$
}
# 添加 MySQL 查询检查命令
define command {
command_name check_mysql_query
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$ -d $ARG3$ -q "$ARG4$"
}2. 配置主机和服务
编辑 mysql-server.cfg 文件:
txt
# 定义 MySQL 服务器主机
define host {
use generic-host
host_name mysql-server
alias MySQL Database Server
address 192.168.1.100
check_command check-host-alive
max_check_attempts 3
check_interval 5
retry_interval 1
check_period 24x7
notification_interval 60
notification_period 24x7
contacts nagiosadmin
}
# 定义 MySQL 连接检查服务
define service {
use generic-service
host_name mysql-server
service_description MySQL Connection
check_command check_mysql!nagios!password!mysql
max_check_attempts 3
check_interval 5
retry_interval 1
check_period 24x7
notification_interval 60
notification_period 24x7
contacts nagiosadmin
}
# 定义 MySQL 健康状态检查服务
define service {
use generic-service
host_name mysql-server
service_description MySQL Uptime
check_command check_mysql_health!nagios!password!uptime
max_check_attempts 3
check_interval 5
retry_interval 1
check_period 24x7
notification_interval 60
notification_period 24x7
contacts nagiosadmin
}
# 定义 MySQL 连接数检查服务
define service {
use generic-service
host_name mysql-server
service_description MySQL Connections
check_command check_mysql_health!nagios!password!connections
max_check_attempts 3
check_interval 5
retry_interval 1
check_period 24x7
notification_interval 60
notification_period 24x7
contacts nagiosadmin
}
# 定义 MySQL 缓存使用率检查服务
define service {
use generic-service
host_name mysql-server
service_description MySQL Buffer Pool Usage
check_command check_mysql_health!nagios!password!buffer-pool-usage
max_check_attempts 3
check_interval 5
retry_interval 1
check_period 24x7
notification_interval 60
notification_period 24x7
contacts nagiosadmin
}
# 定义 MySQL 慢查询检查服务
define service {
use generic-service
host_name mysql-server
service_description MySQL Slow Queries
check_command check_mysql_health!nagios!password!slow-queries
max_check_attempts 3
check_interval 5
retry_interval 1
check_period 24x7
notification_interval 60
notification_period 24x7
contacts nagiosadmin
}
# 定义 MySQL 复制状态检查服务
define service {
use generic-service
host_name mysql-server
service_description MySQL Replication Status
check_command check_mysql_health!nagios!password!replication-delay
max_check_attempts 3
check_interval 5
retry_interval 1
check_period 24x7
notification_interval 60
notification_period 24x7
contacts nagiosadmin
}3. 配置远程监控
在 MySQL 服务器上安装 NRPE:
bash
# 在 CentOS/RHEL 上安装
yum install -y nrpe nagios-plugins-all
# 在 Ubuntu/Debian 上安装
apt install -y nagios-nrpe-server配置 NRPE:
bash
# 编辑 nrpe.cfg 文件
vim /etc/nagios/nrpe.cfg
# 添加 MySQL 监控命令
command[check_mysql]=/usr/lib64/nagios/plugins/check_mysql -H localhost -u nagios -p password -d mysql
command[check_mysql_health]=/usr/local/nagios/libexec/check_mysql_health --hostname=localhost --username=nagios --password=password --mode=$ARG1$
# 允许 Nagios 服务器访问
allowed_hosts=127.0.0.1,nagios-server-ip
# 重启 NRPE 服务
systemctl restart nrpe
systemctl enable nrpe在 Nagios 服务器上配置远程检查:
txt
# 编辑 commands.cfg 文件,添加 NRPE 检查命令
define command {
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -a $ARG2$
}
# 编辑 mysql-server.cfg 文件,添加远程服务定义
define service {
use generic-service
host_name mysql-server
service_description MySQL Connection
check_command check_nrpe!check_mysql
max_check_attempts 3
check_interval 5
retry_interval 1
check_period 24x7
notification_interval 60
notification_period 24x7
contacts nagiosadmin
}
define service {
use generic-service
host_name mysql-server
service_description MySQL Uptime
check_command check_nrpe!check_mysql_health!uptime
max_check_attempts 3
check_interval 5
retry_interval 1
check_period 24x7
notification_interval 60
notification_period 24x7
contacts nagiosadmin
}MySQL 监控指标
1. 连接状态
监控指标:
- 活动连接数
- 最大连接数
- 连接使用率
- 连接错误率
检查命令:
bash
# 检查连接数
check_mysql_health --hostname=localhost --username=nagios --password=password --mode=connections
# 检查连接使用率
check_mysql_health --hostname=localhost --username=nagios --password=password --mode=connection-usage2. 性能指标
监控指标:
- 查询执行速度
- 慢查询数量
- 缓存命中率
- 索引使用率
检查命令:
bash
# 检查慢查询
check_mysql_health --hostname=localhost --username=nagios --password=password --mode=slow-queries
# 检查查询缓存使用率
check_mysql_health --hostname=localhost --username=nagios --password=password --mode=query-cache-usage
# 检查索引使用率
check_mysql_health --hostname=localhost --username=nagios --password=password --mode=index-usage3. 存储指标
监控指标:
- 缓冲池使用率
- 表空间使用情况
- 临时表使用情况
- 日志空间使用情况
检查命令:
bash
# 检查缓冲池使用率
check_mysql_health --hostname=localhost --username=nagios --password=password --mode=buffer-pool-usage
# 检查表空间使用情况
check_mysql_health --hostname=localhost --username=nagios --password=password --mode=table-cache-usage4. 复制指标
监控指标:
- 复制延迟
- 复制状态
- 复制错误
- 复制积压
检查命令:
bash
# 检查复制延迟
check_mysql_health --hostname=localhost --username=nagios --password=password --mode=replication-delay
# 检查复制状态
check_mysql_health --hostname=localhost --username=nagios --password=password --mode=replication-running5. 系统指标
监控指标:
- CPU 使用率
- 内存使用率
- 磁盘使用率
- 网络流量
检查命令:
bash
# 检查 CPU 使用率
check_load -w 15,10,5 -c 30,25,20
# 检查磁盘使用率
check_disk -w 20% -c 10% -p /
# 检查内存使用率
check_mem.pl -w 80 -c 90告警配置
1. 联系人配置
编辑 contacts.cfg 文件:
txt
define contact {
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
email admin@example.com
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
}
# 定义联系组
define contactgroup {
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}2. 告警通知方法
配置邮件通知:
txt
# 编辑 commands.cfg 文件,添加邮件通知命令
define command {
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****>\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}
define command {
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****>\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}配置 SMS 通知:
txt
# 编辑 commands.cfg 文件,添加 SMS 通知命令
define command {
command_name notify-service-by-sms
command_line /usr/local/bin/send_sms.sh $CONTACTPAGER$ "$NOTIFICATIONTYPE$ Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"
}
# 编辑 contacts.cfg 文件,添加 SMS 通知选项
define contact {
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
email admin@example.com
pager 1234567890
service_notification_commands notify-service-by-email,notify-service-by-sms
host_notification_commands notify-host-by-email
}3. 告警阈值设置
设置合理的告警阈值:
| 指标 | 警告阈值 | 严重阈值 | 说明 |
|---|---|---|---|
| 连接数 | 80% | 90% | 基于 max_connections 设置 |
| 慢查询 | 10/分钟 | 30/分钟 | 根据应用需求调整 |
| 复制延迟 | 30秒 | 60秒 | 根据业务对数据一致性的要求 |
| 缓冲池使用率 | 80% | 90% | 基于 innodb_buffer_pool_size |
| CPU 使用率 | 70% | 90% | 基于服务器 CPU 核心数 |
| 磁盘使用率 | 80% | 90% | 基于磁盘总容量 |
配置告警阈值:
txt
# 编辑 mysql-server.cfg 文件,设置带阈值的服务定义
define service {
use generic-service
host_name mysql-server
service_description MySQL Connections
check_command check_mysql_health!nagios!password!connections --warning 80 --critical 90
max_check_attempts 3
check_interval 5
retry_interval 1
check_period 24x7
notification_interval 60
notification_period 24x7
contacts nagiosadmin
}监控面板与报告
1. Web 界面
访问 Nagios Web 界面:
- 地址:
http://nagios-server-ip/nagios - 用户名:
nagiosadmin - 密码:安装时设置的密码
主要功能:
- Dashboard:监控概览
- Hosts:主机状态
- Services:服务状态
- Maps:网络拓扑图
- Reports:监控报告
- Trends:趋势分析
- Configuration:配置管理
2. 监控报告
可用报告:
- Availability Report:可用性报告
- Trends Report:趋势报告
- Alert History:告警历史
- State History:状态历史
- Host Group Summary:主机组摘要
- Service Group Summary:服务组摘要
自定义报告:
- 使用 Nagios API 获取数据
- 集成第三方工具生成报告
- 如 NagiosGrapher、PNP4Nagios 等
3. 性能图表
集成 PNP4Nagios:
- 安装 PNP4Nagios
- 配置 Nagios 集成
- 查看性能图表
集成 Graphite:
- 安装 Graphite 和 Carbon
- 配置 Nagios 发送数据到 Graphite
- 使用 Grafana 查看图表
最佳实践
1. 监控策略
分层监控:
- 系统层:CPU、内存、磁盘、网络
- 数据库层:连接、查询、缓存、复制
- 应用层:响应时间、错误率、业务指标
监控频率:
- 关键指标:1-5 分钟
- 一般指标:5-15 分钟
- 次要指标:15-30 分钟
告警级别:
- Warning:需要关注,但不紧急
- Critical:需要立即处理
- Unknown:检查失败,需要排查
2. 配置管理
配置版本控制:
- 使用 Git 管理配置文件
- 记录配置变更
- 便于回滚错误配置
配置模板:
- 创建主机和服务模板
- 减少重复配置
- 提高配置一致性
配置验证:
- 使用
nagios -v /etc/nagios/nagios.cfg验证配置 - 避免配置错误导致 Nagios 无法启动
3. 告警管理
告警抑制:
- 配置合理的告警间隔
- 避免告警风暴
- 使用告警聚合
告警升级:
- 配置告警升级规则
- 确保告警得到及时处理
- 避免告警被忽略
告警响应:
- 建立告警响应流程
- 明确各角色的职责
- 记录告警处理过程
4. 维护与优化
定期维护:
- 清理历史数据
- 优化数据库性能
- 更新监控插件
监控优化:
- 移除不必要的监控
- 调整监控频率
- 优化检查命令
故障演练:
- 定期执行故障演练
- 测试监控系统的有效性
- 验证告警机制
常见问题与解决方案
1. 监控插件执行失败
问题:check_mysql 插件执行失败,返回错误信息
解决方案:
- 检查 MySQL 服务是否运行
- 验证监控用户的权限
- 检查网络连接是否正常
- 测试插件命令行执行
2. 告警风暴
问题:系统出现大量告警,导致告警风暴
解决方案:
- 配置合理的告警间隔
- 使用告警聚合
- 调整告警阈值
- 优化监控频率
3. 监控数据不准确
问题:监控数据与实际情况不符
解决方案:
- 验证插件的检查逻辑
- 检查监控用户的权限
- 确保插件版本兼容
- 校准监控阈值
4. Nagios 性能问题
问题:Nagios 服务器性能下降
解决方案:
- 增加 Nagios 服务器资源
- 优化监控频率
- 减少监控项数量
- 使用分布式监控
5. 告警通知失败
问题:告警通知无法发送
解决方案:
- 检查邮件服务器配置
- 验证联系人邮箱地址
- 测试通知命令执行
- 检查防火墙设置
集成第三方工具
1. 集成 PNP4Nagios
安装 PNP4Nagios:
bash
# 下载并安装 PNP4Nagios
tar -xzf pnp4nagios-0.6.26.tar.gz
cd pnp4nagios-0.6.26
./configure
make all
make fullinstall
# 配置 Nagios 集成
vim /etc/nagios/nagios.cfg
# 添加 PNP4Nagios 配置
process_performance_data=1
host_perfdata_command=process-host-perfdata
service_perfdata_command=process-service-perfdata
# 重启 Nagios 服务
systemctl restart nagios2. 集成 Grafana 和 Graphite
安装 Graphite 和 Carbon:
bash
# 安装依赖
pip install carbon graphite-web django-tagging
# 配置 Carbon
cp /opt/graphite/conf/carbon.conf.example /opt/graphite/conf/carbon.conf
cp /opt/graphite/conf/storage-schemas.conf.example /opt/graphite/conf/storage-schemas.conf
# 启动 Carbon
sudo -u nagios /opt/graphite/bin/carbon-cache.py start
# 安装 Grafana
yum install -y grafana
systemctl start grafana-server
systemctl enable grafana-server配置 Nagios 发送数据到 Graphite:
bash
# 安装 nagios-graphite 插件
git clone https://github.com/obfuscurity/nagios-graphite.git
cd nagios-graphite
cp nagiosgraphite.py /usr/local/bin/
# 配置 Nagios 命令
define command {
command_name process-service-perfdata
command_line /usr/local/bin/nagiosgraphite.py --carbon-host=localhost --carbon-port=2003 --nagios-service
}
define command {
command_name process-host-perfdata
command_line /usr/local/bin/nagiosgraphite.py --carbon-host=localhost --carbon-port=2003 --nagios-host
}3. 集成 InfluxDB 和 Grafana
安装 InfluxDB:
bash
# 安装 InfluxDB
yum install -y influxdb
systemctl start influxdb
systemctl enable influxdb
# 创建数据库
influx -execute "CREATE DATABASE nagios"
influx -execute "CREATE USER nagios WITH PASSWORD 'password' WITH ALL PRIVILEGES"配置 Nagios 发送数据到 InfluxDB:
bash
# 安装 nagios-influxdb-plugin
git clone https://github.com/luisdavim/nagios-influxdb-plugin.git
cd nagios-influxdb-plugin
cp nagios_influxdb.py /usr/local/bin/
# 配置 Nagios 命令
define command {
command_name process-service-perfdata
command_line /usr/local/bin/nagios_influxdb.py -H localhost -P 8086 -u nagios -p password -d nagios -m service
}
define command {
command_name process-host-perfdata
command_line /usr/local/bin/nagios_influxdb.py -H localhost -P 8086 -u nagios -p password -d nagios -m host
}配置 Grafana 数据源:
- 登录 Grafana Web 界面
- 添加 InfluxDB 数据源
- 配置数据库连接
- 创建监控仪表盘
常见问题(FAQ)
Q1: 如何监控多个 MySQL 实例?
A1: 监控多个 MySQL 实例的方法:
- 在 Nagios 中为每个实例创建单独的主机或服务定义
- 使用不同的监控用户和密码
- 配置不同的告警阈值
- 可以使用主机组和服务组进行管理
Q2: 如何监控 MySQL 复制拓扑?
A2: 监控 MySQL 复制拓扑的方法:
- 为每个复制节点创建监控服务
- 监控复制状态和延迟
- 使用
check_mysql_health插件的replication-delay和replication-running模式 - 配置复制告警阈值
- 可以使用 Orchestrator 可视化复制拓扑
Q3: 如何减少 Nagios 对 MySQL 的影响?
A3: 减少 Nagios 对 MySQL 影响的方法:
- 使用只读监控用户
- 限制监控频率,避免过于频繁的检查
- 优化监控查询,避免复杂查询
- 使用缓存插件,如 check_mysql_health 的缓存功能
- 考虑在从库上执行监控检查
Q4: 如何实现 Nagios 的高可用性?
A4: 实现 Nagios 高可用性的方法:
- 使用 Nagios 双机热备
- 配置共享存储保存配置和状态
- 使用虚拟 IP 实现故障转移
- 如使用 Pacemaker + Corosync 实现高可用
- 或使用分布式监控架构,如 Nagios XI 企业版
Q5: 如何扩展 Nagios 监控能力?
A5: 扩展 Nagios 监控能力的方法:
- 使用自定义插件扩展监控功能
- 集成第三方工具,如 PNP4Nagios、Grafana 等
- 使用 Nagios API 开发自定义应用
- 配置分布式监控,监控大规模环境
- 利用商业扩展,如 Nagios XI 提供更多功能
