文章摘要:
本文系统介绍了Prometheus生态中的四大监控组件:

  1. Blackbox_exporter:实现HTTP/TCP/ICMP等黑盒探测,通过relabel_configs实现多目标监控,关键指标包括probe_success、响应耗时等,支持SSL证书过期告警。
  2. Domain_exporter:监控域名过期时间,通过WHOIS协议查询并暴露domain_expiry_days指标,告警规则覆盖域名失效风险。
  3. SNMP_exporter:采集网络设备数据,通过if_mib模块获取接口指标,需配置SNMP团体名(community)。
  4. PushGateway:作为短期任务的中转站,支持手动推送和清理指标,适用于备份脚本等场景,需注意单点风险和数据持久化问题。
    各组件均提供Grafana模板与告警规则,形成完整的监控链路。

一、Blackbox_exporter 黑盒监控(6.1 节)

1.1 核心概念

黑盒监控:从外部探测服务是否可达,不关注服务内部状态,只关注「能不能通」「响应如何」。

概念

说明

黑盒监控

从用户视角探测服务可用性(HTTP/TCP/ICMP/DNS/SSH)

白盒监控

从内部暴露指标(node_exporter、mysqld_exporter 等)

/probe 接口

Blackbox_exporter 的结果存储接口,每次探测结果存储于此

target

需要探测的目标(URL / IP / 域名)

module

探测使用的模块(http_2xx / tcp_connect / icmp / ssh_banner 等)

Prometheus 与 Blackbox 配合工作流程

  1. Prometheus 告诉 Blackbox_exporter 要监控的目标(target)和检测方法(module)
  2. Blackbox_exporter 执行检测,将结果存入 /probe 接口
  3. Prometheus 定期从 /probe 接口抓取指标

1.2 部署要点

步骤

关键信息

安装节点

prom-node04(192.168.200.40)

默认监听端口

9115

配置文件

/etc/blackbox_exporter/blackbox.yml

访问方式

http://192.168.200.40:9115/

1.3 支持的探测模块

模块名称

探测协议

用途

http_2xx

HTTP GET

探测 HTTP/HTTPS 站点是否正常(返回 2xx)

http_post_2xx

HTTP POST

探测 POST 接口是否正常

tcp_connect

TCP

探测端口是否可达(如数据库端口、应用端口)

icmp

ICMP

Ping 探测,判断主机是否存活

ssh_banner

TCP + SSH 协议

探测 SSH 服务是否正常

dns_tcp

DNS

探测 DNS 解析是否正常

1.4 关键配置:使用 relabel_configs 做多目标监控

黑盒监控的关键难点:如何让 instance 标签显示为被探测目标(而不是 Blackbox 自身地址)

- job_name: "blackbox_http"
  metrics_path: "/probe"
  params:
    module: [http_2xx]
  static_configs:
    - targets: ["http://www.chensiqi.vip", "https://www.baidu.com", "https://www.jd.com"]
  relabel_configs:
    - source_labels: [__address__]
      target_label: __param_target    # 1. 将目标地址赋给 target 参数
    - source_labels: [__param_target]
      target_label: instance          # 2. 用目标地址覆盖 instance 标签(UI 中显示真实目标)
    - target_label: __address__
      replacement: prom-node04:9115  # 3. 将实际请求地址改为 Blackbox 的地址和端口

三次 relabel 操作含义

步骤

操作

目的

1

__address____param_target

告知 Blackbox 要探测的目标 URL

2

__param_targetinstance

让 UI 显示被探测站点而不是 Blackbox 地址

3

__address__prom-node04:9115

使 Prometheus 向 Blackbox 发起请求

1.5 TCP / ICMP / SSH 监控配置

# TCP 端口探测
- job_name: "blackbox_tcp"
  metrics_path: "/probe"
  params:
    module: [tcp_connect]
  static_configs:
    - targets: ["prom-node02:3306", "prom-node04:6379"]
  relabel_configs: [同上三步]

# ICMP Ping 探测
- job_name: "blackbox_icmp"
  metrics_path: "/probe"
  params:
    module: [icmp]
  static_configs:
    - targets: ["prom-node01", "prom-node02", "prom-node03", "prom-node04"]
  relabel_configs: [同上三步]

# SSH 服务探测
- job_name: "blackbox_ssh"
  metrics_path: "/probe"
  params:
    module: [ssh_banner]
  static_configs:
    - targets: ["prom-node01:22", "prom-node02:22", "prom-node03:22", "prom-node04:22"]
  relabel_configs: [同上三步]

1.6 Blackbox 核心指标

通用探测指标

指标名称

类型

含义

probe_success

Gauge

探测是否成功(1 成功 / 0 失败)

probe_duration_seconds

Gauge

探测总耗时(秒),含 DNS + TCP + HTTP 各阶段

probe_dns_lookup_time_seconds

Gauge

DNS 查询耗时(秒)

# 探测失败的目标(按 job 汇总)
sum(probe_success == 0) by (job)

# DNS 解析响应时间超过 100ms
probe_dns_lookup_time_seconds > 0.1

# 1 分钟平均探测耗时超过 1s(TCP/DNS)
avg_over_time(probe_duration_seconds{job=~"blackbox_tcp|blackbox_dns"}[1m]) > 1
HTTP 探测专用指标

指标名称

类型

含义

probe_http_status_code

Gauge

HTTP 响应状态码

probe_http_ssl

Gauge

是否使用 HTTPS(1 是 / 0 否)

probe_http_redirects

Gauge

HTTP 重定向次数

probe_http_duration_seconds

Gauge

HTTP 各阶段耗时(connect/resolve/tls/processing/transfer)

probe_ssl_earliest_cert_expiry

Gauge

SSL 证书过期时间戳

# 重定向次数超过 5 次
probe_http_redirects > 5

# 各阶段耗时超过 200ms
sum(probe_http_duration_seconds) by (instance, phase) > 0.2

# 状态码异常(<200 或 >=400)
probe_http_status_code <= 199 or probe_http_status_code >= 400

# SSL 证书剩余天数
(probe_ssl_earliest_cert_expiry - time()) / 86400

1.7 Blackbox 告警规则汇总

告警名称

触发条件

严重级别

探测失败

probe_success == 0

critical

站点平均请求时间过长

1min 平均耗时 > 3s

warning

重定向次数过多

probe_http_redirects > 5

warning

站点阶段耗时过长

单阶段耗时 > 0.5s

warning

站点响应状态码异常

状态码 ≤199 或 ≥400

critical

SSL 证书即将过期 < 30 天

(cert_expiry - time()) / 86400 < 30

critical

SSL 证书即将过期 < 7 天

(cert_expiry - time()) / 86400 < 7

critical

Grafana 模板13659(HTTP Prober)、75879965


二、domain_exporter 域名监控(6.2 节)

2.1 核心原理

domain_exporter 通过 WHOIS 协议 查询域名注册信息,解析出域名过期时间,并格式化为 Prometheus 指标暴露在 /probe 接口。

步骤

说明

1. 收集域名信息

通过 WHOIS 协议查询 target 参数指定的域名

2. 解析域名信息

从 WHOIS 数据中解析出过期时间

3. 导出指标

格式化为 Prometheus 格式,通过 /probe 接口输出

2.2 部署要点

步骤

关键信息

默认监听端口

9222

接口路径

/probe?target=域名

Prometheus 抓取间隔

建议 scrape_interval: 1m(WHOIS 查询较慢)

Grafana 模板 ID

1460513924

2.3 Prometheus 配置

- job_name: "domain_exporter"
  metrics_path: /probe
  scrape_interval: 1m
  scrape_timeout: 30s
  static_configs:
  - targets: ["www.chensiqi.vip", "www.jd.com", "www.baidu.com"]
  relabel_configs:
  - source_labels: [__address__]
    target_label: __param_target
  - source_labels: [__param_target]
    target_label: instance
  - target_label: __address__
    replacement: prom-node04:9222

2.4 domain 核心指标

指标名称

类型

含义

domain_expiry_days

Gauge

域名距过期剩余天数

domain_probe_success

Gauge

域名检测是否成功(1 成功 / 0 失败)

domain_probe_duration_seconds

Gauge

完成一次 WHOIS 查询所需时间(秒)

2.5 域名告警规则

告警名称

触发条件

严重级别

域名即将过期 < 100 天

domain_expiry_days < 100

warning

域名即将过期 < 30 天

domain_expiry_days < 30

critical

域名检测失败

domain_probe_success == 0

critical


三、snmp_exporter 网络设备监控(6.3 节)

3.1 核心原理

snmp_exporter 将从网络设备(交换机、路由器、防火墙)通过 SNMP 协议获取的数据,转换为 Prometheus 兼容的格式。

Prometheus 向 snmp_exporter 发起探测需要传递的参数

参数

说明

target

需要监控的网络设备地址

module

使用的模块(默认 if_mib 覆盖大部分场景)

auth

SNMP 认证信息(团体名称 community)

3.2 部署要点

步骤

关键信息

默认监听端口

9116

配置文件

/etc/snmp_exporter/snmp.yml

关键配置

修改 community 为目标设备的 SNMP 团体名称

默认模块

if_mib(网络接口指标,覆盖大部分场景)

3.3 Prometheus 配置

- job_name: "snmp_exporter"
  scrape_interval: 1m
  scrape_timeout: 30s
  metrics_path: "/snmp"
  params:
    auth: [public_v2]
    module: [if_mib]
  static_configs:
  - targets: ["192.168.1.1"]   # 目标交换机/路由器 IP
  relabel_configs:
  - source_labels: [__address__]
    target_label: __param_target
  - source_labels: [__param_target]
    target_label: instance
  - target_label: __address__
    replacement: prom-node04:9116

Grafana 模板 ID11169


四、PushGateway 推送网关(6.4 节)

4.1 核心原理

PushGateway 允许**短期任务(Short-lived Job)**通过 Push 方式推送指标。Prometheus 不会主动抓取这些短期任务(任务执行完就消失了),因此借助 PushGateway 作为中间存储,等待 Prometheus 来拉取。

特性

说明

数据保存方式

默认内存,可用 --persistence.file 持久化到文件

数据清理

不自动删除,需手动或通过 API 删除

单点风险

使用单个 PushGateway 会成为单点

默认端口

9091

4.2 PushGateway 的三个注意事项

  1. 单点问题:若单个 PushGateway 接收多个实例/脚本的数据,会成为单点故障
  2. 数据不过期:推送的数据不会自动删除,Prometheus 会持续从中抓取
  3. 删除不影响历史:删除 PushGateway 中的数据不影响 Prometheus 已抓取的历史数据

4.3 推送指标格式

# 手动推送单个指标
echo "some_metric 3.14" | curl --data-binary @- http://localhost:9091/metrics/job/some_job

# 脚本推送多个指标
cat << EOF | curl --data-binary @- http://$PUSH_ADDR:9091/metrics/job/backup/instance/$APP_NAME
# HELP backup_duration_seconds The duration of the last backup in seconds.
# TYPE backup_duration_seconds gauge
backup_duration_seconds{application="$APP_NAME"} $DURATION
# HELP backup_success Last backup was success(1) or ERROR(0).
# TYPE backup_success gauge
backup_success{application="$APP_NAME"} $STATUS
EOF

4.4 清理 PushGateway 数据

# 手动 API 删除
curl -X DELETE http://prom-node04:9091/metrics/job/${JOB_NAME}/instance/${INSTANCE_NAME}

# 通过 crontab 定时清理
59 23 * * * /bin/bash /root/clear_pushgateway_job.sh backup shop

4.5 典型使用场景:备份脚本监控

# 关键推送指标
backup_duration_seconds   # 备份耗时(秒)
backup_success            # 备份状态(1 成功 / 0 失败)

对应告警规则

- alert: 备份告警超时
  expr: backup_duration_seconds > 6 and backup_success == 1
  for: 1m
  labels:
    severity: warning

- alert: 备份任务失败
  expr: backup_success == 0
  for: 1m
  labels:
    severity: critical

五、快速参考

组件

用途

端口

Grafana 模板

blackbox_exporter

HTTP/TCP/ICMP/SSH 黑盒探测

9115

13659 / 7587 / 9965

domain_exporter

域名注册过期监控

9222

14605 / 13924

snmp_exporter

网络设备(交换机/路由器)监控

9116

11169

pushgateway

短期任务指标推送中转

9091

-

黑盒探测模块

适用场景

http_2xx

HTTP/HTTPS 站点可用性

tcp_connect

数据库端口、应用端口可达性

icmp

主机存活(Ping)探测

ssh_banner

SSH 服务可用性

dns_tcp

DNS 解析可用性

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐