启用mod_status需确认模块加载、配置安全访问控制(如require ip)、开启extendedstatus以获取worker状态等关键指标,并通过check_apachestatus.pl等插件集成nagios实现带阈值的健康监控。

启用 Apache 的 status 模块
这是 Nagios 获取 Apache 内部运行指标的基础。需确保 mod_status 已加载,并配置可访问的状态接口:
- 检查是否启用:
httpd -M | grep status(或a2enmod status在 Debian/Ubuntu) - 在 Apache 配置中(如
httpd.conf或apache2.conf)添加:<location> SetHandler server-status Require local Require ip 192.168.10.0/24 # 替换为 Nagios 服务器 IP 段 </location> - 务必开启
ExtendedStatus On,否则无法获取 worker 状态、请求数等关键指标 - 重启 Apache:
systemctl restart apache2或apachectl restart - 手动验证:
curl http://localhost/server-status?auto应返回键值对格式数据(如BusyWorkers: 2)
部署并测试 Apache 监控插件
Nagios 不自带原生 Apache 健康检查脚本,需选用成熟插件,推荐 check_apachestatus.pl 或 Python 版 check_apache2.py:
Apache 2.4.62 官方 tar.gz 源码包是 Linux 及类 Unix 系统构建 Web 服务器的核心基础。通过源码编译安装,开发者能够灵活定制模块、优化性能并精准控制安装路径,满足多样化的业务需求。
- 下载脚本(例如 Perl 版):
wget http://www.ealook.com/download/check_apachestatus.pl - 赋予执行权限并归属 nagios 用户:
chmod 755 check_apachestatus.pl && chown nagios:nagios check_apachestatus.pl - 安装依赖(如 LWP):
cpan Bundle::LWP或用系统包管理器安装perl-libwww-perl - 命令行测试:
./check_apachestatus.pl -H 192.168.10.17 -u /server-status?auto,预期输出类似OK - Idle: 4, Busy: 1, Total Slots: 256
在 Nagios 中定义监控命令和服务
将插件接入 Nagios 配置体系,实现自动轮询与告警:
- 在
commands.cfg中添加命令定义:define command{ command_name check_apache_health command_line $USER1$/check_apachestatus.pl -H $HOSTADDRESS$ -u "/server-status?auto" } - 在主机对应的服务配置文件(如
linux/192.168.10.17.cfg)中定义服务:define service{ use generic-service host_name 192.168.10.17 service_description Apache Health Status check_command check_apache_health normal_check_interval 1 max_check_attempts 3 } - 验证配置语法:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg - 重载服务:
systemctl reload nagios或service nagios restart
关注关键健康指标与阈值设置
仅连通性检测(如 check_http)不够——它不能反映 Apache 实际负载能力。真正体现“健康状态”的是 mod_status 提供的运行时指标:
- BusyWorkers:过高(如 >80% 总线程数)说明请求积压,可能需调优 MPM 或扩容
- IdleWorkers:持续为 0 表明无空闲进程/线程,易触发超时
- CPULoad(若启用 ExtendedStatus):配合系统 CPU 监控交叉判断
- Total Accesses / Total kBytes:突增可能预示攻击或异常流量
- 进阶做法:修改插件参数支持警告/严重阈值,例如
-w "BusyWorkers>10" -c "BusyWorkers>20"









