news 2026/9/5 8:10:16

【Prometheus·Exporter 篇】Blackbox Exporter:黑盒监控与网络探测

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【Prometheus·Exporter 篇】Blackbox Exporter:黑盒监控与网络探测

前言

前面讲的 Exporter 都是"白盒"监控——需要在被监控方安装 Agent。但有些场景你无法在目标上安装任何东西:外部 API、第三方服务、网络设备。Blackbox Exporter 从外部探测目标,属于"黑盒"监控。


一、白盒 vs 黑盒监控

白盒监控: 目标内部 → 安装 Exporter → 暴露内部指标 → CPU、内存、QPS、错误率 → 回答"为什么出问题了" 黑盒监控: 外部 → 探测目标 → 验证可达性 → HTTP 状态码、响应时间、证书有效期 → 回答"是否出问题了"
维度白盒黑盒
部署位置目标内部外部
可见性内部细节外部行为
告警含义即将出问题已经出问题
适用自己的服务外部/不可控目标

二、安装 Blackbox Exporter

二进制

wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.25.0/blackbox_exporter-0.25.0.linux-amd64.tar.gz tar xzf blackbox_exporter-0.25.0.linux-amd64.tar.gz cd blackbox_exporter-0.25.0.linux-amd64 sudo cp blackbox_exporter /usr/local/bin/

配置文件

# /etc/blackbox/blackbox.yml modules: # HTTP 探测 http_2xx: prober: http timeout: 5s http: preferred_ip_protocol: ip4 ip_protocol_fallback: false valid_http_versions: [HTTP/1.1, HTTP/2.0] valid_status_codes: [200, 301, 302] method: GET follow_redirects: true headers: User-Agent: "Prometheus-Blackbox-Exporter" # TLS 配置 tls_config: insecure_skip_verify: false # HTTPS 探测(关注证书) http_2xx_check_tls: prober: http timeout: 5s http: valid_status_codes: [200] method: GET tls_config: insecure_skip_verify: false # POST 请求探测 http_post_2xx: prober: http timeout: 5s http: method: POST headers: Content-Type: application/json body: '{"key":"value"}' # TCP 端口探测 tcp_connect: prober: tcp timeout: 5s # SSH 端口探测(banner 检查) ssh_banner: prober: tcp timeout: 5s tcp: query_response: - expect: "^SSH-2.0-" # ICMP ping icmp: prober: icmp timeout: 5s icmp: preferred_ip_protocol: ip4 # DNS 解析 dns_check: prober: dns timeout: 5s dns: query_name: "example.com" query_type: A valid_rcodes: [NOERROR]

Systemd 服务

# /etc/systemd/system/blackbox_exporter.service [Unit] Description=Blackbox Exporter After=network.target [Service] Type=simple ExecStart=/usr/local/bin/blackbox_exporter \ --config.file=/etc/blackbox/blackbox.yml \ --web.listen-address=0.0.0.0:9115 \ --log.level=info Restart=on-failure [Install] WantedBy=multi-user.target

Docker 部署

docker run -d \ --name blackbox-exporter \ -p 9115:9115 \ -v /etc/blackbox/blackbox.yml:/etc/blackbox/blackbox.yml \ prom/blackbox-exporter:v0.25.0 \ --config.file=/etc/blackbox/blackbox.yml

三、HTTP 探测

手动探测

# 探测一个 URL curl "http://localhost:9115/probe?target=https://example.com&module=http_2xx" # 查看探测结果 probe_success 1 probe_duration_seconds 0.234 probe_http_status_code 200 probe_ssl_earliest_cert_expiry 1.738688e+09

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 - http://internal-app:8080/health relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: blackbox-exporter:9115

关键指标

# 1. 探测成功(0=失败,1=成功) probe_success # 2. 响应时间 probe_duration_seconds # 3. HTTP 状态码 probe_http_status_code # 4. SSL 证书剩余天数 (probe_ssl_earliest_cert_expiry - time()) / 86400 # 5. DNS 解析时间 probe_dns_lookup_time_seconds # 6. TLS 握手时间 probe_tls_handshake_seconds # 7. HTTP 版本 probe_http_version

四、TCP 端口探测

scrape_configs: - job_name: 'blackbox-tcp' metrics_path: /probe params: module: [tcp_connect] static_configs: - targets: - mysql:3306 - redis:6379 - kafka:9092 - postgres:5432 labels: probe_type: tcp relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: blackbox-exporter:9115

五、ICMP Ping 探测

scrape_configs: - job_name: 'blackbox-icmp' metrics_path: /probe params: module: [icmp] static_configs: - targets: - 8.8.8.8 - 8.8.4.4 - 114.114.114.114 - 192.168.1.1 - 10.0.0.1 labels: probe_type: icmp relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: blackbox-exporter:9115

⚠️踩坑提示:ICMP 探测需要 root 权限或setcap cap_net_raw+ep /usr/local/bin/blackbox_exporter

# 设置 cap_net_raw 权限 sudo setcap cap_net_raw+ep /usr/local/bin/blackbox_exporter

六、DNS 解析探测

scrape_configs: - job_name: 'blackbox-dns' metrics_path: /probe params: module: [dns_check] static_configs: - targets: - 8.8.8.8 # DNS 服务器 labels: dns_query: 'example.com' relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: blackbox-exporter:9115

七、SSL 证书监控

scrape_configs: - job_name: 'ssl-cert-expiry' metrics_path: /probe params: module: [http_2xx_check_tls] static_configs: - targets: - https://www.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: blackbox-exporter:9115
# 证书剩余天数 (probe_ssl_earliest_cert_expiry - time()) / 86400 # 证书过期告警(30 天) ((probe_ssl_earliest_cert_expiry - time()) / 86400) < 30

八、告警规则

groups: - name: blackbox-alerts rules: # 服务不可达 - alert: ServiceDown expr: probe_success == 0 for: 2m labels: severity: critical annotations: summary: "Service {{ $labels.instance }} is down" description: "Probe to {{ $labels.instance }} has been failing for 2 minutes" # 响应时间过长 - alert: SlowResponse expr: probe_duration_seconds > 5 for: 5m labels: severity: warning annotations: summary: "Slow response from {{ $labels.instance }}" description: "Response time is {{ $value }}s (threshold: 5s)" # SSL 证书即将过期 - alert: SSLCertExpiringSoon expr: (probe_ssl_earliest_cert_expiry - time()) / 86400 < 30 for: 1h labels: severity: warning annotations: summary: "SSL cert for {{ $labels.instance }} expires soon" description: "Certificate expires in {{ $value | printf "%.0f" }} days" # SSL 证书即将过期(紧急) - alert: SSLCertExpiringCritical expr: (probe_ssl_earliest_cert_expiry - time()) / 86400 < 7 for: 1h labels: severity: critical annotations: summary: "SSL cert for {{ $labels.instance }} expires in 7 days!" # HTTP 状态码异常 - alert: HTTPStatusError expr: probe_http_status_code >= 500 for: 2m labels: severity: critical annotations: summary: "HTTP {{ $labels.instance }} returning {{ $value }}" # DNS 解析失败 - alert: DNSResolutionFailed expr: probe_dns_rcode != 0 for: 5m labels: severity: warning annotations: summary: "DNS resolution failed for {{ $labels.instance }}"

九、动态目标配置

从 Consul 发现

scrape_configs: - job_name: 'blackbox-consul' metrics_path: /probe params: module: [http_2xx] consul_sd_configs: - server: 'consul:8500' services: ['web', 'api'] relabel_configs: - source_labels: [__address__] target_label: __param_target regex: '(.+):(?:\d+)' replacement: 'http://$1/' - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: blackbox-exporter:9115

从文件发现

scrape_configs: - job_name: 'blackbox-file' metrics_path: /probe params: module: [http_2xx] file_sd_configs: - files: ['/etc/prometheus/targets/blackbox/*.yml'] relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: blackbox-exporter:9115
# /etc/prometheus/targets/blackbox/services.yml - targets: - https://service1.example.com - https://service2.example.com labels: team: payments env: production - targets: - https://service3.example.com labels: team: orders env: production

要点回顾

探测类型module场景
HTTPhttp_2xxWeb 服务可用性
HTTPS+TLShttp_2xx_check_tls证书监控
TCPtcp_connect数据库/中间件端口
SSHssh_bannerSSH 服务检查
ICMPicmp主机存活检查
DNSdns_checkDNS 解析监控
  • Blackbox 是外部探测,无需在目标安装 Agent
  • 通过relabel_configs将 target 转为参数传给 Blackbox
  • HTTP 探测可监控状态码、响应时间、SSL 证书
  • ICMP 探测需要 cap_net_raw 权限
  • SSL 证书过期告警是 Blackbox 最常用场景

下一篇预告

现有 Exporter 覆盖了常见场景,但有时需要监控自定义业务指标。下一篇【Prometheus·Exporter 篇】自定义 Exporter 开发:Client Library 实战将手把手开发自己的 Exporter。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/5 8:09:22

用多智能体狼人杀环境评测大模型推理与记忆能力

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/5 8:07:37

Step Plan 动态规划工具:从智能排期到团队协作的全面评测

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/5 8:06:22

基于ISO13400 (DoIP) :风丘实现车辆高速刷写

近年来&#xff0c;在整车研发中基于以太网实现车辆高带宽通讯无疑是人们热议的话题。无论是车内基于车载以太网来减少线束成本&#xff0c;实现ADAS、信息娱乐系统等技术&#xff0c;还是基于新的电子电气架构以及远程诊断需求来实现以太网诊断&#xff08;DoIP&#xff09;&a…

作者头像 李华
网站建设 2026/9/5 8:02:24

美食餐饮评选,电子打分牌和现场大屏怎么用

美食节、厨艺比拼这类评选&#xff0c;评委通常在展台之间走动&#xff0c;边尝边打分。用纸质表要一手拿表一手拿笔&#xff0c;还得找地方垫着写&#xff1b;用手机打分则要低头操作&#xff0c;评委之间的分数还容易互相看到。现场观众的体验也重要。评到一半的时候&#xf…

作者头像 李华
网站建设 2026/9/5 7:58:45

零门槛动漫发型编辑器:免费免安装的创意设计工具

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/5 7:57:53

利红AI人工智能快报2026年9月4日

1、最新数据显示&#xff0c;我国AI核心产业规模已突破1.2万亿元&#xff0c;同比增速达40%&#xff0c;AI技术全面覆盖工业研发设计、生产制造、质检运维等核心环节。 2、今年新发布2026年本科专业目录&#xff0c;新增38种新专业并首设“交叉学科”门类。这些新专业大量呈现…

作者头像 李华