用户使用 Prometheus 和 Blackbox Exporter 监控 SSL 证书过期时间,是运维中非常经典的“黑盒监控”场景。其核心原理是让 Prometheus 周期性请求 Blackbox Exporter 去访问你的 HTTPS 站点,Exporter 会从响应中提取证书的过期时间戳,再通过 Prometheus 的告警规则来判断剩余天数并发出通知。下面是一套从部署到告警的完整配置方案。
1. 工作原理
简单来说,Prometheus 会定期向 Blackbox Exporter 发送探测请求,并携带目标 URL。Exporter 执行探测后,会返回一个名为 `probe_ssl_earliest_cert_expiry` 的指标(即证书过期时间戳),Prometheus 抓取此指标并依据告警规则判断。
mermaid
flowchart LR
A[Prometheus Server] -->|1. scrape指标<br>/probe?target=example.com| B[Blackbox Exporter]
B -->|2. 发起HTTPS请求| C[目标网站 example.com]
C -->|3. 返回证书| B
B -->|4. 返回指标<br>probe_ssl_earliest_cert_expiry| A
A -->|5. 评估告警规则| D[Alertmanager]
D -->|6. 发送通知| E[邮件/钉钉/微信等]
2. 环境搭建与配置
假设你已经拥有 Prometheus 环境,以下是新增 SSL 证书监控的具体步骤。
2.1 安装与启动 Blackbox Exporter
你可以从 [GitHub Releases](https://github.com/prometheus/blackbox_exporter/releases) 下载最新版本。以下以 Linux 系统为例:
bash
下载并解压
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.25.0/blackbox_exporter-0.25.0.linux-amd64.tar.gz
tar xvfz blackbox_exporter-0.25.0.linux-amd64.tar.gz
cd blackbox_exporter-0.25.0.linux-amd64
直接启动(默认端口 9115)
./blackbox_exporter
你可以通过访问 `http://<exporter_ip>:9115/probe?module=http_2xx&target=https://www.baidu.com` 来测试是否返回了包含 `probe_ssl_earliest_cert_expiry` 的数据。
2.2 配置 Prometheus 采集任务
修改 Prometheus 配置文件(通常是 `prometheus.yml`),添加针对 Blackbox Exporter 的抓取任务。这里通过 `relabel_configs` 将目标 URL 传递给 Exporter,并将 `instance` 标签设置为该 URL,便于告警时识别。
yaml
scrape_configs:
- job_name: 'ssl-cert-monitor'
metrics_path: /probe
params:
module: [http_2xx] # 使用 HTTP/HTTPS 探测模块
static_configs:
- targets:
- https://example.com # 替换为你的域名
- https://api.example.com
- https://admin.example.com
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115 # 假设 Blackbox Exporter 在本机运行
2.3 添加告警规则
创建一个告警规则文件,例如 `ssl_rules.yml`,并在 Prometheus 主配置文件 `prometheus.yml` 中的 `rule_files` 下引用它。
yaml
groups:
- name: tls_cert_check
rules:
# 警告:证书将在 7 天后过期
- alert: SSLCertificateWillExpireSoon
expr: (probe_ssl_earliest_cert_expiry - time()) / 86400 < 7
for: 5m
labels:
severity: warning
annotations:
summary: "SSL 证书即将过期 (实例: {{ $labels.instance }})"
description: "证书将在 {{ $value | printf \"%.1f\" }} 天后过期,请尽快更新。"
# 紧急:证书已经过期
- alert: SSLCertificateExpired
expr: (probe_ssl_earliest_cert_expiry - time()) <= 0
for: 1m
labels:
severity: critical
annotations:
summary: "SSL 证书已过期 (实例: {{ $labels.instance }})"
description: "证书已于 {{ $labels.instance }} 过期,服务可能不可用。"
注:`(probe_ssl_earliest_cert_expiry - time()) / 86400` 计算的是证书剩余天数。
3. 高级注意事项
在配置监控时,有一个容易踩的坑:HTTP 重定向。
如果被监控的域名配置了 301/302 重定向(例如从 `old.com` 跳转到 `new.com`),Blackbox Exporter 默认会跟随重定向,导致最终监控的是 `new.com` 的证书。
如果你的需求是严格监控 `old.com` 本身的证书,需要修改 Blackbox Exporter 的配置文件(`blackbox.yml`),添加一个禁用重定向的模块:
yaml
modules:
http_2xx_no_redirect: # 自定义模块名
prober: http
timeout: 5s
http:
follow_redirects: false # 关键配置:禁用重定向
valid_http_versions: ["HTTP/1.1", "HTTP/2"]
valid_status_codes: [] # 2xx 默认是成功的
然后在 Prometheus 的配置中,将 `params` 下的 `module` 改为 `http_2xx_no_redirect`。
4. 可视化与通知
Grafana 可视化:推荐使用 Grafana 社区中的 Dashboard 模板 9965 或 13230,它们专为 Blackbox Exporter 设计,可以直观展示证书有效期、响应时间等数据。
配置 Alertmanager:将上述告警规则对接 Alertmanager,配置邮件、钉钉或企业微信等接收渠道,确保团队能第一时间收到通知。
通过这套方案,你可以在SSL证书过期前(例如提前 7 天或 30 天)收到告警,从容地完成证书更新,避免线上事故。