提交工单

用Blackbox Exporter做外部可用性监控:HTTP、TCP、ICMP与证书到期探测

Node Exporter 告诉你服务器的 CPU、内存、磁盘是否正常,但它回答不了一个更重要的问题:用户现在能不能打开你的网站? Nginx 配置错误、证书过期、防火墙误封、DNS 解析错误,这些情况下服务器内部的指标可能一切正常。

Blackbox Exporter 是 Prometheus 生态中的「外部探测」组件:它像一个用户一样,从外部访问你的服务,并把结果变成指标。监控体系的整体思路见 白盒监控与黑盒监控。

它能探测什么

探测方式 检查内容
HTTP / HTTPS 状态码、响应时间、页面是否包含关键字、证书到期时间
TCP 端口能否连通,可选检查握手内容(如 SSH 横幅)
ICMP ping 延迟和是否可达
DNS 域名能否解析、解析结果是否符合预期

工作方式

Blackbox Exporter 本身不保存目标列表。Prometheus 在抓取时把「要探测的目标」作为参数传给它:

Prometheus → http://blackbox:9115/probe?module=http_2xx&target=https://www.example.com
           ← probe_success 1, probe_duration_seconds 0.23, ...

所以目标列表写在 Prometheus 的配置里,Blackbox 只定义「怎么探测」(模块)。

第一步:安装

使用 Docker:

mkdir -p /opt/blackbox && cd /opt/blackbox

docker-compose.yml:

services:
  blackbox:
    image: prom/blackbox-exporter:latest
    restart: unless-stopped
    ports:
      - "127.0.0.1:9115:9115"
    volumes:
      - ./blackbox.yml:/etc/blackbox_exporter/config.yml:ro
    cap_add:
      - NET_RAW          # ICMP 探测需要

生产环境建议把 latest 换成固定的版本号。端口只绑定在本机或内网,不要把探测接口暴露在公网,否则任何人都能利用它去探测任意地址。Docker 端口与防火墙的关系见 Docker 端口绕过 UFW。

第二步:定义探测模块

blackbox.yml:

modules:
  http_2xx:
    prober: http
    timeout: 10s
    http:
      preferred_ip_protocol: ip4
      valid_status_codes: []          # 空表示 2xx
      follow_redirects: true

  http_keyword:
    prober: http
    timeout: 10s
    http:
      preferred_ip_protocol: ip4
      fail_if_body_not_matches_regexp:
        - "</html>"

  tcp_connect:
    prober: tcp
    timeout: 5s

  ssh_banner:
    prober: tcp
    timeout: 5s
    tcp:
      query_response:
        - expect: "^SSH-2.0-"

  icmp:
    prober: icmp
    timeout: 5s
    icmp:
      preferred_ip_protocol: ip4
  • http_keyword:状态码是 200 但页面内容是错误信息的情况很常见(例如 PHP 报错被吞掉、返回空白页)。检查页面是否包含某个固定的字符串,比单看状态码可靠。可以换成你首页上一定会出现的文字
  • ssh_banner:不仅检查端口开放,还检查确实是 SSH 服务在响应
docker compose up -d
curl "http://127.0.0.1:9115/probe?module=http_2xx&target=https://www.example.com"

输出中 probe_success 1 表示成功。

第三步:Prometheus 抓取配置

scrape_configs:
  - job_name: blackbox_http
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets:
          - https://www.example.com/
          - https://api.example.com/health
    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

  - job_name: blackbox_tcp
    metrics_path: /probe
    params:
      module: [tcp_connect]
    static_configs:
      - targets:
          - 203.0.113.21:3306
          - 203.0.113.11:22
    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

这三条 relabel 规则是固定写法,作用是:

  1. 把 targets 中写的地址,作为 target 参数传给 Blackbox
  2. 把这个地址作为 instance 标签,方便在图表中区分
  3. 把实际抓取的地址改为 Blackbox Exporter 本身

Prometheus 的安装和基本配置见 Prometheus + Grafana 监控。

第四步:关键指标

指标 含义
probe_success 1 成功,0 失败
probe_duration_seconds 整个探测耗时
probe_http_status_code HTTP 状态码
probe_http_duration_seconds{phase=...} 分阶段耗时:resolve、connect、tls、processing、transfer
probe_ssl_earliest_cert_expiry 证书链中最早到期的证书的到期时间(Unix 时间戳)
probe_icmp_duration_seconds ICMP 往返时间

分阶段耗时非常有用:网站变慢时,能区分是 DNS 解析慢、TCP 连接慢、TLS 握手慢,还是服务器处理慢。这与 curl 调试 HTTP 中的 -w 计时思路一致。

第五步:告警规则

groups:
  - name: blackbox
    rules:
      - alert: 站点不可用
        expr: probe_success == 0
        for: 3m
        labels: { severity: critical }
        annotations:
          summary: "{{ $labels.instance }} 探测失败"

      - alert: 站点响应慢
        expr: avg_over_time(probe_duration_seconds[10m]) > 3
        for: 10m
        labels: { severity: warning }

      - alert: 证书即将到期
        expr: (probe_ssl_earliest_cert_expiry - time()) / 86400 < 14
        for: 1h
        labels: { severity: warning }
        annotations:
          summary: "{{ $labels.instance }} 证书将在 {{ $value | humanize }} 天后到期"

告警的发送见 Alertmanager 告警配置。

探测点放在哪里

这是外部监控最容易被忽视的问题:探测点和被监控的服务在同一台服务器上,几乎没有意义——服务器断网时,探测程序也无法发出告警。

建议:

  • 探测点放在另一个机房、另一个服务商的机器上
  • 用户主要在中国大陆、网站在海外时,从海外探测正常不代表大陆用户能访问。可以在不同地区各放一个探测点,比较结果
  • 探测点本身也要被监控(例如用另一个探测点互相探测),否则探测点宕机时你会以为一切正常

常见问题排查

probe_success 为 0,但浏览器能打开

在探测 URL 后加 &debug=true,查看详细的探测过程:

curl "http://127.0.0.1:9115/probe?module=http_2xx&target=https://www.example.com&debug=true"

常见原因:

  • 探测点优先使用 IPv6 而网站的 IPv6 不通,设置 preferred_ip_protocol: ip4
  • 网站有防护规则,拦截了 Blackbox 的 User-Agent 或数据中心 IP,在防护规则中放行探测点
  • 超时时间设置太短

ICMP 探测全部失败

容器缺少 NET_RAW 权限,或者目标服务器禁止了 ping。被监控的服务器禁 ping 时,改用 TCP 探测。

小结

  • Blackbox Exporter 从外部探测 HTTP、TCP、ICMP、DNS,回答「用户能不能访问」
  • 模块定义怎么探测,目标列表写在 Prometheus 配置中,配合固定的三条 relabel 规则
  • 检查页面关键字比只看状态码可靠;分阶段耗时用于定位慢在哪里
  • 告警加 for 持续时间,证书提前 14 天告警
  • 探测点必须与被监控的服务分开,最好在不同机房、不同地区

常见问题

和 Uptime Kuma 之类的工具比如何选择?

Uptime Kuma 有图形界面,配置简单,适合只需要可用性监控的场景,见 Uptime Kuma 监控网站。已经使用 Prometheus 时,Blackbox Exporter 能把探测结果和其他指标放在一起分析和告警。

探测频率设置多少合适?

一般 30 秒到 1 分钟一次。过于频繁会在被探测的网站上产生大量日志,对网站本身也是负担。

能监控内网服务吗?

可以,只要探测点能访问到内网地址。内网服务和公网服务最好分开不同的 job,便于区分告警。