Prometheus Exporters 导出器指南
本文档涵盖常用 Exporters 的安装配置、核心指标和实用 PromQL 查询。
符号说明
| 符号 | 含义 |
|---|---|
| 🚨 | 陷阱 / Pitfall |
| 💡 | 最佳实践 / Best practice |
| 🔬 | 深入原理 / Deep-dive |
| ⚡ | 性能提示 / Performance note |
1. 常用 Exporters 一览
| Exporter | 端口 | 用途 |
|---|---|---|
| Node Exporter | 9100 | Linux/Unix 系统指标(CPU、内存、磁盘、网络) |
| Blackbox Exporter | 9115 | HTTP/HTTPS、DNS、TCP、ICMP 端点探测 |
| cAdvisor | 8080 | 容器资源使用(Docker 内置集成) |
| MySQL Exporter | 9104 | MySQL 数据库指标 |
| Redis Exporter | 9121 | Redis 指标 |
| PostgreSQL Exporter | 9187 | PostgreSQL 指标 |
| Kafka Exporter | 9308 | Kafka 消费者组偏移 |
| RabbitMQ Exporter | 9419 | RabbitMQ 队列指标 |
| JMX Exporter | 可配置 | JVM 应用指标(Java) |
| Windows Exporter | 9182 | Windows 系统指标 |
| SNMP Exporter | 9116 | 网络设备 SNMP 指标 |
| Process Exporter | 9256 | 进程级别指标 |
2. Node Exporter(系统监控)
安装(Linux):
# 下载
wget https://github.com/prometheus/node_exporter/releases/download/v1.9.1/node_exporter-1.9.1.linux-amd64.tar.gz
tar -xvf node_exporter-1.9.1.linux-amd64.tar.gz
sudo cp node_exporter-1.9.1.linux-amd64/node_exporter /usr/local/bin/
# 创建 systemd 服务
sudo tee /etc/systemd/system/node_exporter.service <<'EOF'
[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter \
--web.listen-address=":9100" \
--collector.systemd \
--collector.processes
Restart=always
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter核心指标:
| 指标 | 说明 |
|---|---|
node_cpu_seconds_total |
CPU 各模式时间 |
node_memory_MemTotal_bytes / MemAvailable_bytes |
总 / 可用内存 |
node_filesystem_size_bytes / avail_bytes |
磁盘容量 / 可用 |
node_network_receive_bytes_total / _transmit_bytes_total |
网络收/发字节 |
node_disk_io_time_seconds_total |
磁盘 IO 时间 |
node_boot_time_seconds |
启动时间戳 |
node_load1 / node_load5 / node_load15 |
系统负载 |
实用 PromQL:
# CPU 使用率 %
100 - (avg by(instance)(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
# 内存使用率 %
(1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100
# 磁盘使用率 %
(1 - node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}) * 100
# 运行时长(天)
(time() - node_boot_time_seconds) / 86400
# 网络带宽使用率(bps)
rate(node_network_receive_bytes_total[5m]) * 83. Blackbox Exporter(端点探测)
用途: 从 Prometheus 服务器外部探测目标 —— HTTP 可用性、DNS 解析、TCP 端口连通、ICMP ping、TLS 证书过期。
配置(blackbox.yml):
modules:
http_2xx:
prober: http
timeout: 5s
http:
valid_http_versions: ["HTTP/1.1", "HTTP/2.0"]
method: GET
follow_redirects: true
preferred_ip_protocol: "ip4"
http_post_2xx:
prober: http
timeout: 5s
http:
method: POST
headers:
Content-Type: application/json
body: '{}'
tcp_connect:
prober: tcp
timeout: 5s
dns_lookup:
prober: dns
timeout: 5s
dns:
query_name: "example.com"
query_type: "A"
icmp:
prober: icmp
timeout: 5sPrometheus 抓取配置:
scrape_configs:
- job_name: 'blackbox-http'
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
- https://example.com
- https://api.example.com/health
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: localhost:9115 # Blackbox 地址
- job_name: 'blackbox-tcp'
metrics_path: /probe
params:
module: [tcp_connect]
static_configs:
- targets:
- '192.168.1.10:3306' # MySQL
- '192.168.1.10:6379' # Redis
- '192.168.1.10:22' # SSH
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: localhost:9115核心指标:
| 指标 | 说明 |
|---|---|
probe_success |
探测是否成功 (1/0) |
probe_duration_seconds |
总耗时 |
probe_http_status_code |
HTTP 状态码 |
probe_dns_lookup_time_seconds |
DNS 解析时间 |
probe_tcp_connection_duration_seconds |
TCP 建连时间 |
probe_ssl_earliest_cert_expiry |
最早过期证书时间戳 |
🚨 Blackbox 的关键:
relabel_configs中将原始 target 写入__param_target,把__address__替换成 Blackbox Exporter 的地址,这样 Prometheus 先请求 Blackbox,Blackbox 再去探测真正的目标。
参考资料: