Apache被Prometheus监控的核心前提是启用mod_status并配置/server-status?auto机器可读接口,再通过apache_exporter转换指标;需禁用Require local、合理设置ExtendedStatus、限制IP访问、验证接口输出,并在Prometheus中配置抓取job。
apache 要被 prometheus 监控,核心前提是它得“说人话”——也就是通过 mod_status 暴露机器可读的状态数据,再由 apache_exporter 翻译成 prometheus 能理解的指标格式。光启模块远远不够,配置细节直接决定监控是否可用、安全、稳定。
确保 mod_status 正确启用并可访问
Apache 默认不开启 mod_status,需手动加载并配置访问路径:
- 启用模块:执行
a2enmod status(Debian/Ubuntu)或确认LoadModule status_module modules/mod_status.so在httpd.conf中未被注释 - 添加位置配置:在主配置或虚拟主机中加入类似以下块(注意路径和权限):
<Location "/server-status">
SetHandler server-status
Require ip 192.168.10.0/24 # 替换为 exporter 所在机器的内网 IP 段
</Location> - 禁用
Require local:它在容器、IPv6 或反向代理下基本失效,容易导致 exporter 抓取失败 - 重启 Apache 生效:
systemctl restart apache2或apachectl graceful
按需控制 ExtendedStatus 开关
ExtendedStatus 决定是否返回每个请求的详细信息(如 URL、处理时长、CPU 占用),影响性能与安全性:
- 调试阶段可临时开启:
ExtendedStatus On,放在<IfModule mod_status.c>块内 - 生产环境建议关闭:
ExtendedStatus Off,避免额外开销和内存压力 - 该指令只能出现在主服务器上下文,不能写在
.htaccess或虚拟主机内部
验证 ?auto 接口是否就绪
/server-status?auto 是 exporter 唯一依赖的数据源,必须返回纯文本键值对:
Apache Superset 是一个广泛采用的开源 BI 平台,用于 SQL 探索、图表构建和仪表板交付。当代理需要查询仓库数据、组装仪表板或使用成熟的分析界面解释指标而不是临时笔记本代码时,此技能非常有用。
- 本地测试命令:
curl -s http://localhost/server-status?auto | head -5 - 预期输出类似:
Total Accesses: 12345
Total kBytes: 6789
BusyWorkers: 3
IdleWorkers: 12
CPULoad: 0.123 - 若返回 HTML 页面或 403/404,说明路径未匹配、权限不足或模块未生效
- 不建议将此接口直接暴露公网;如需跨网络访问,应走内网代理或加 Basic 认证
部署与配置 apache_exporter
Exporter 是无状态轻量工具,负责拉取 ?auto 并转换指标:
- 启动时明确指定目标地址:
./apache_exporter --scrape_uri="http://apache-host:80/server-status?auto" - 默认监听
:9117/metrics,Prometheus 抓取地址即为该地址 - Docker 启动示例:
docker run -d -p 9117:9117 -e HTTPD_URL=http://192.168.5.100/server-status?auto quay.io/ukhomeofficedigital/httpd-exporter - Prometheus 配置中添加 job:
- job_name: 'apache'
static_configs:
- targets: ['exporter-host:9117']
关键指标与初步告警方向
不用全量采集,盯住三类核心指标就能覆盖大部分问题场景:
-
请求速率:用
rate(apache_requests_total[5m])观察突增或归零,后者常是服务中断信号 -
连接状态:关注
apache_connections和apache_connections_idle,空闲连接长期为 0 表明请求排队 -
Worker 利用率:计算
apache_currently_busy_workers / apache_max_workers,持续 >0.8 可能需调优或查慢请求

















