blackbox_exporter
2025年12月9日大约 2 分钟
The blackbox exporter allows blackbox probing of endpoints over HTTP, HTTPS, DNS, TCP, ICMP and gRPC.
blackbox-exporter的配置文件使用默认的即可(/usr/local/blackbox_exporter/blackbox.yml),文件里定义了进行目标检测时要使用的模块和模块参数。至于要检测哪些目标是定义在Prometheus 的Job配置中。
# Blackbox Exporter modules 配置文件
# 保存为 blackbox.yml,重启 blackbox_exporter 后生效
modules:
# TCP 连接探测,适合检测任意 TCP 服务端口是否可达(无 HTTP 握手)
tcp_connect_8000:
prober: tcp
timeout: 5s
# tcp 探测可以配置发送/期望回复,但对常规端口检测通常不需要
tcp:
# 可选:如果服务会在连接后立即返回特定数据,可以配置 query/response
# query: ""
# response: ""
# HTTP 探测(检查 2xx 响应 / 基本 HTTP 可用性),用于 web 服务的详细检查
http_8000:
prober: http
timeout: 5s
http:
# 请求方法
method: GET
# 期望的有效状态码范围,若服务返回 200 则认为成功
valid_status_codes: [200]
# 如果希望接受 2xx/3xx/4xx 可根据需要调整,例如 [200,301]
# valid_status_codes: [200,301]
# 不跟随重定向(根据需要可改为 true)
no_follow_redirects: false
# 优先使用 IPv4(内部网络通常使用 IPv4)
preferred_ip_protocol: "ip4"
# 若服务使用 TLS 且内部证书不受信任,可以内网使用跳过校验
tls_config:
insecure_skip_verify: true
# 可选自定义请求头
headers:
# Example:
# Host: example.local
# User-Agent: blackbox-probe
scrape_configs:
- job_name: 'blackbox-192.168.3.51-8000-tcp'
metrics_path: /probe
params:
module: [tcp_connect_8000] # 使用 blackbox.yml 中的模块名
static_configs:
- targets: ['192.168.3.51:8000'] # 被探测目标 (target)
relabel_configs:
# 将 __address__(目标)传给 /probe 的参数 target
- source_labels: [__address__]
target_label: __param_target
# 使用 target 值作为 instance 标签(可在 grafana/告警中显示)
- source_labels: [__param_target]
target_label: instance
# 把实际的 scrape 地址改为 blackbox exporter 的地址
- target_label: __address__
replacement: 127.0.0.1:9115 # <-- 把这改为 blackbox_exporter 的地址:端口role
groups:
- name: blackbox_service_alerts
interval: 30s
rules:
- alert: ServiceDown
expr: probe_success{instance="192.168.3.51:8000"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "服务 192.168.3.51:8000 离线 服务监控"
description: "服务心跳连续失败超过 1 分钟"