Prometheus 如何配置监控 SSL 证书即将过期

要监控 SSL 证书过期,标准方案是 Prometheus + Blackbox Exporter:用 Blackbox 探测 HTTPS 站点、读取证书有效期,再用 Prometheus 规则做 "即将过期" 告警。

一、安装 Blackbox Exporter

bash 复制代码
# 下载(以最新版为例)
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.25.0/blackbox_exporter-0.25.0.linux-amd64.tar.gz
tar zxf blackbox_exporter-*.tar.gz
cd blackbox_exporter-*

# 启动(默认 9115 端口)
./blackbox_exporter --config.file=blackbox.yml

二、配置 Blackbox(blackbox.yml)

专门用来强制检查 SSL 证书:

复制代码
modules:
  http_ssl_cert:
    prober: http
    timeout: 5s
    http:
      valid_http_versions: ["HTTP/1.1", "HTTP/2"]
      method: GET
      # 允许自签/过期(只看证书有效期)
      fail_if_ssl: false
      fail_if_not_ssl: true
      tls_config:
        insecure_skip_verify: true

三、Prometheus 配置(prometheus.yml)

复制代码
scrape_configs:
  - job_name: "blackbox_ssl"
    metrics_path: /probe
    params:
      module: [http_ssl_cert]  # 对应上面的 module
    static_configs:
      - targets:
          - https://www.baidu.com
          - https://your-domain.com
          - https://api.yourapp.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 地址

四、关键指标

  • probe_ssl_earliest_cert_expiry:证书过期时间戳(Unix 秒)
  • probe_success:探测是否成功(1 = 成功)

五、告警规则(ssl_rules.yml)

复制代码
groups:
  - name: ssl_cert_alerts
    rules:
      # 30天内过期(警告)
      - alert: SSL_Cert_Expire_In_30_Days
        expr: (probe_ssl_earliest_cert_expiry - time()) / 86400 < 30
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "SSL证书即将过期(30天内)"
          description: "实例 {{ $labels.instance }} 证书还有 {{ $value | printf \"%.0f\" }} 天过期"

      # 7天内过期(严重)
      - alert: SSL_Cert_Expire_In_7_Days
        expr: (probe_ssl_earliest_cert_expiry - time()) / 86400 < 7
        for: 2m
        labels:
          severity: critical
        annotations:
          summary: "SSL证书即将过期(7天内)"
          description: "实例 {{ $labels.instance }} 证书还有 {{ $value | printf \"%.0f\" }} 天过期"

      # 已过期
      - alert: SSL_Cert_Expired
        expr: probe_ssl_earliest_cert_expiry - time() <= 0
        for: 1m
        labels:
          severity: disaster
        annotations:
          summary: "SSL证书已过期"
          description: "实例 {{ $labels.instance }} 证书已过期,请立即更换"

六、在 prometheus.yml 加载规则

复制代码
rule_files:
  - "ssl_rules.yml"

七、重载生效

复制代码
# 热重载(需 --web.enable-lifecycle)
curl -X POST http://localhost:9090/-/reload

# 或重启
systemctl restart prometheus

八、验证

  1. Prometheus → Status → Targets:看 blackbox_ssl 是否 UP

  2. Prometheus → Graph:执行 plaintext

    复制代码
    probe_ssl_earliest_cert_expiry

    能看到时间戳

  3. Prometheus → Alerts:看是否触发 SSL 相关告警

九、常用查询(剩余天数)

promql

复制代码
# 所有域名剩余天数
(probe_ssl_earliest_cert_expiry - time()) / 86400

# 30天内过期
(probe_ssl_earliest_cert_expiry - time()) / 86400 < 30

十、Grafana 面板(可选)

导入 Dashboard ID:13230(Blackbox Exporter HTTPS/SSL)


总结

  1. Blackbox Exporter 负责探测 HTTPS、读取证书有效期
  2. Prometheus 抓取指标 probe_ssl_earliest_cert_expiry
  3. 告警规则 判断剩余天数(30/7/0 天)
  4. Alertmanager 发邮件 / 企业微信 / 钉钉
相关推荐
kekekzt1 天前
TCP协议的粘包问题介绍,IP分片,MTU,MSS,滑动窗口的概念及之间的关系
网络·网络协议·tcp/ip
Learn-Share_HY1 天前
[IT Network]如何配置反向路徑過濾器(rp_filter),以解決路由不對稱問題?
linux·嵌入式硬件·物联网·网络协议·tcp/ip·http·iot
小镇敲码人1 天前
【深入浅出】之Qt 事件系统实战:鼠标、键盘、滚轮、窗口与定时器事件
数据库·qt·网络协议·mysql·http
K成长日志1 天前
BLE不可连接状态--广播态
物联网·网络协议·蓝牙·低功耗·iot·ble
DevOpenClub1 天前
全球区域与 IP 定位工作台案例方案
大数据·网络·网络协议·tcp/ip
wuyk5551 天前
从零吃透 Modbus 通信|第 6 章:线圈功能码 05/0F 实现 & Modbus‑TCP 基础入门
c语言·网络·网络协议·tcp/ip
Titan20241 天前
HTTPS基础知识梳理
服务器·网络·c++·网络协议·学习·http·https
新时代牛马1 天前
epoll 源码路径:从epoll_ctl 到ep_poll 的就绪唤醒
网络·数据库·网络协议
艾莉丝努力练剑1 天前
【AI大模型接入SDK】WebSocket & SSE 协议
网络·c++·websocket·网络协议·学习·面试
xieliyu.1 天前
计算机网络‑IP 协议解析:核心特性总结
网络·笔记·网络协议·学习·tcp/ip·计算机网络