发布时间:2026/9/5 8:10:21
【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] DescriptionBlackbox Exporter Afternetwork.target [Service] Typesimple ExecStart/usr/local/bin/blackbox_exporter \ --config.file/etc/blackbox/blackbox.yml \ --web.listen-address0.0.0.0:9115 \ --log.levelinfo Restarton-failure [Install] WantedBymulti-user.targetDocker 部署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?targethttps://example.commodulehttp_2xx # 查看探测结果 probe_success 1 probe_duration_seconds 0.234 probe_http_status_code 200 probe_ssl_earliest_cert_expiry 1.738688e09Prometheus 配置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_rawep /usr/local/bin/blackbox_exporter。# 设置 cap_net_raw 权限 sudo setcap cap_net_rawep /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 服务可用性HTTPSTLShttp_2xx_check_tls证书监控TCPtcp_connect数据库/中间件端口SSHssh_bannerSSH 服务检查ICMPicmp主机存活检查DNSdns_checkDNS 解析监控Blackbox 是外部探测无需在目标安装 Agent通过relabel_configs将 target 转为参数传给 BlackboxHTTP 探测可监控状态码、响应时间、SSL 证书ICMP 探测需要 cap_net_raw 权限SSL 证书过期告警是 Blackbox 最常用场景下一篇预告现有 Exporter 覆盖了常见场景但有时需要监控自定义业务指标。下一篇【Prometheus·Exporter 篇】自定义 Exporter 开发Client Library 实战将手把手开发自己的 Exporter。

相关新闻

2026/9/5 8:05:21

蓝牙5.4核心特性解析:PAwR/EANT与ALX420A芯片落地实践

/* 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:05:21

Unity自由视角摄像机实现:从3D数学到实战代码

/* 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 9:00:24

SPC不是画控制图,是一套”防火系统”

【新版SPC手册解读②】一个灵魂拷问:你家是装烟雾报警器,还是等着叫消防车?先问个问题:假设你是开饭馆的,后厨防火有两种策略——策略A:装满烟雾报警器、定期检查燃气管道、给员工做消防培训,火…

2026/9/5 8:55:24

SolidWorks进阶:150道实战练习从基础到精通的系统训练

/* 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 2:46:54

vSound小提琴数字处理器实操指南:从接线到演出的完整配置

电小提琴或者原声小提琴插电演出,第一个绕不开的坎就是声音难听。原声琴的共鸣和空气感一旦进了拾音器,出来的往往是一坨干瘪、发尖、带着奇怪塑料味的信号。我当初第一次把琴接上乐队调音台,直接被主唱吐槽"你这声音像在锯钢丝"。…

2026/9/5 2:46:52

传感器接口IC如何攻克生物化学传感的微弱信号难题?

1. 从电极到比特流:为什么生物化学传感必须依赖专用接口IC 做生物化学传感的人都有过类似的经历:明明传感器本身性能很好,信号输出却一塌糊涂——噪声大、漂移明显、重复性差,怎么调都达不到预期。很多时候问题并不在传感器&#…

2026/9/5 2:44:34

STM32F411CEU6多通道ADC采集:扫描模式+DMA实现详解

1. 多通道 ADC 的用武之地把“Multichannel ADC”和“STM32F411CEU6”这两个关键字放在一起,其实就是嵌入式开发里最常遇到的一类需求:用一块不算贵的 MCU,同时采集多路模拟信号。STM32F411CEU6 是 48 引脚的 Cortex-M4F 主控,主频…

2026/9/5 0:04:47

流式背压机制:避免前端渲染卡死与内存暴涨的滑动窗口限流

流式背压机制:避免前端渲染卡死与内存暴涨的滑动窗口限流在大模型流式输出(Streaming)与智能体实时推流的架构中,生产环境中经常出现一种“上下游生产消费速率严重失衡”的极端情况: 生产端极速产出:大模型…

2026/9/5 2:45:13

USB Type-C PCB布局分区设计:电源、高速信号与PD协议全攻略

做硬件这行,Type-C接口算是典型的“看着简单,做起来全坑”的东西。光引脚就24个,高低速信号、电源、控制线全部塞在一个小小的连接器里,如果PCB布局不做规划,打样回来基本就是“插上没反应”、“高速掉线”、“静电一打…

2026/9/5 2:30:42

系统编程学习原型如何补齐稳定性边界

系统编程学习原型如何补齐稳定性边界预算有限时&#xff0c;我先优化明显多余的复制&#xff0c;而不是猜测性地换容器。用借用传递只读数据通常就能减少分配&#xff1a; fn parse(line: &str) -> Result<Item, Error> { /* ... */ }用基准确认热点确实在分配&am…

2026/9/5 2:46:50

雨花区哪家财务公司代理记账比较好?

在雨花区&#xff0c;企业处理财税事务常常面临诸多挑战&#xff0c;选择一家靠谱的财务公司至关重要。湖南巨勤财务管理咨询有限公司就是本地正规实体财税服务机构&#xff0c;深耕本地工商财税行业多年&#xff0c;熟悉当地工商局、税务局最新政策与申报流程。主营公司注册、…