blackbox_exporter: prometheus 黑盒探测程序

发布时间: 更新时间: 总字数:980 阅读时间:2m 作者: IP上海 分享 网址

blackbox_exporter 是一种 prometheus 实现的黑盒探测程序,允许通过 HTTP、HTTPS、DNS、TCP、ICMP 和 gRPC 对端点进行黑盒探测。

介绍

Blackbox Exporter 是 Prometheus 生态中的一个重要工具,主要用于 黑盒监控(Blackbox Monitoring),即从外部视角探测目标服务的可用性和性能。以下是其核心作用和应用场景的详细介绍:

核心功能

  • 主动探测监控:通过主动向目标发送请求(如 HTTP、TCP、ICMP 等),模拟用户或客户端的访问行为,检测服务是否正常响应
  • 多协议支持
    • HTTP/HTTPS:检查 Web 服务的状态码、响应时间、SSL 证书过期时间等
    • TCP:验证端口是否开放、服务是否可连接(如数据库、Redis)
    • ICMP(Ping):检测主机网络连通性
    • DNS:查询 DNS 解析是否正常
    • gRPC:监控 gRPC 服务的健康状态
  • 灵活的指标导出:将探测结果(如响应时间、成功/失败状态)转换为 Prometheus 可抓取的 metrics

典型应用场景

  • 网站/API 可用性监控
    • 定时检测关键接口是否返回 200 OK
    • 验证登录、支付等核心业务流程是否正常
  • 网络质量监控
    • 通过 ICMP 检测跨机房、跨地域的网络延迟和丢包率
  • 证书过期监控
    • 提前预警 HTTPS 证书过期时间(通过 probe_ssl_earliest_cert_expiry 指标)
  • 多节点探测
    • 从不同地理位置(如云厂商的多个区域)探测服务,模拟真实用户访问体验

优势特点

  • 外部视角:不同于白盒监控(如 Node Exporter),它从用户角度发现问题(如 DNS 污染、CDN 故障)
  • 配置简单:通过 YAML 文件定义探测模块(如超时时间、校验规则)
  • 与 Prometheus 无缝集成:直接作为 Prometheus 的抓取目标,配合 Alertmanager 实现告警

部署

  • 配置 config/blackbox.yml参考
blackbox.yml ...
modules:
  http_2xx:
    prober: http
    http:
      preferred_ip_protocol: "ip4"
  http_post_2xx:
    prober: http
    http:
      method: POST
  tcp_connect:
    prober: tcp
  pop3s_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: "^+OK"
      tls: true
      tls_config:
        insecure_skip_verify: false
  grpc:
    prober: grpc
    grpc:
      tls: true
      preferred_ip_protocol: "ip4"
  grpc_plain:
    prober: grpc
    grpc:
      tls: false
      service: "service1"
  ssh_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: "^SSH-2.0-"
      - send: "SSH-2.0-blackbox-ssh-check"
  ssh_banner_extract:
    prober: tcp
    timeout: 5s
    tcp:
      query_response:
      - expect: "^SSH-2.0-([^ -]+)(?: (.*))?$"
        labels:
        - name: ssh_version
          value: "${1}"
        - name: ssh_comments
          value: "${2}"
  irc_banner:
    prober: tcp
    tcp:
      query_response:
      - send: "NICK prober"
      - send: "USER prober prober prober :prober"
      - expect: "PING :([^ ]+)"
        send: "PONG ${1}"
      - expect: "^:[^ ]+ 001"
  icmp:
    prober: icmp
  icmp_ttl5:
    prober: icmp
    timeout: 5s
    icmp:
      ttl: 5

该配置文件格式:

# 探针类型:http、 tcp、 dns、 icmp.
prober: <prober_string>

# 超时时间
[ timeout: <duration> ]

# 探针的详细配置,最多只能配置其中的一个
[ http: <http_probe> ]
[ tcp: <tcp_probe> ]
[ dns: <dns_probe> ]
[ icmp: <icmp_probe> ]
  • 启动服务
docker run --rm \
  -p 9115/tcp \
  --name blackbox_exporter \
  -v ./config:/config \
  quay.io/prometheus/blackbox-exporter:v0.26.0 --config.file=/config/blackbox.yml

prometheus 配置

与 Prometheus 集成,实现对不同 Target 的黑盒探测

scrape_configs:
  - job_name: 'blackbox'
    metrics_path: /probe
    params:
      module: [http_2xx]  # Look for a HTTP 200 response.
    static_configs:
      - targets:
        - http://prometheus.io    # Target to probe with http.
        - https://prometheus.io   # Target to probe with https.
        - http://example.com:8080 # Target to probe with http on port 8080.
    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  # The blackbox exporter's real hostname:port.
  - job_name: 'blackbox_exporter'  # collect blackbox exporter's operational metrics.
    static_configs:
      - targets: ['127.0.0.1:9115']

示例

http_post_2xx:
    prober: http
    timeout: 5s
    http:
      method: POST
      headers:
        Content-Type: application/json
      body: '{}'

http_basic_auth_example:
    prober: http
    timeout: 5s
    http:
      method: POST
      headers:
        Host: "login.example.com"
      basic_auth:
        username: "username"
        password: "mysecret"

参考

  1. https://github.com/prometheus/blackbox_exporter
  2. https://github.com/prometheus/blackbox_exporter/blob/master/CONFIGURATION.md
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数