Blackbox Exporter 是 Prometheus 生态系统中用于探测和监测目标的工具,可以检测 HTTP、TCP、ICMP 等服务的可用性。下面是一篇博文,演示如何使用 Docker 部署 Blackbox Exporter。
安装 Blackbox Exporter:
docker run -d -p 9115:9115 --name blackbox_exporter prom/blackbox-exporter
创建配置文件
以下是一个包含 HTTP、HTTPS、TCP、DNS 和 ICMP 配置的 Blackbox Exporter 配置文件示例。这个示例假设你有一些要监控的网络终端,并且希望使用不同的 prober 模块进行探测。
modules:
http:
prober: http
timeout: 5s
http:
method: GET
valid_status_codes: []
tls_config:
insecure_skip_verify: false
https:
prober: http
timeout: 5s
http:
method: GET
valid_status_codes: []
tls_config:
insecure_skip_verify: false
tcp:
prober: tcp
timeout: 1s
tcp:
tls: false
dns:
prober: dns
timeout: 1s
dns:
transport_protocol: udp
preferred_ip_protocol: ip4
icmp:
prober: icmp
timeout: 2s
在这个示例中:
http
模块使用 HTTP prober,通过 GET 请求检查目标是否支持 HTTP。https
模块类似于http
模块,但用于检查 HTTPS。tcp
模块使用 TCP prober,检查目标是否支持 TCP 连接。dns
模块使用 DNS prober,检查目标是否支持 DNS 查询。icmp
模块使用 ICMP prober,检查目标是否支持 ICMP。
你可以根据需要添加更多的模块或调整参数。配置文件的具体内容取决于你想要监控的目标和监控的需求。确保根据实际情况调整 timeout
和其他参数。
使用配置文件启动 Blackbox Exporter 时,确保将配置文件挂载到容器内的 /etc/blackbox_exporter/config.yml
。
启动 Blackbox Exporter
docker run -d -p 9115:9115 -v /path/to/config.yml:/etc/blackbox_exporter/config.yml --name blackbox_exporter prom/blackbox-exporter
这是一个基本的配置,你可以根据需要调整和扩展。在实际使用中,请确保理解每个 prober 模块的参数和配置选项,以满足你的监控需求。详细的配置选项和说明可以在 Blackbox Exporter GitHub 页面的文档中找到。
验证 Blackbox Exporter 是否正常运行
访问 http://localhost:9115/metrics
来查看 Blackbox Exporter 的指标数据。你应该能够看到来自探测的各种指标。
配置 Prometheus 抓取 Blackbox Exporter 数据
在这个示例中,我们定义了五个不同的 scrape_configs
,分别用于 HTTP、HTTPS、TCP、DNS 和 ICMP 的监控。确保将目标主机和端口替换为你实际要监控的目标。
将上述配置添加到 Prometheus 的配置文件中,然后重新启动 Prometheus。Prometheus 将按照配置的间隔定期抓取 Blackbox Exporter 的数据。
请注意,配置中的 module
参数指定了要使用的 Blackbox Exporter 模块,与 Blackbox Exporter 配置文件中的模块名称匹配。
global:
scrape_interval: 10s
scrape_timeout: 10s
evaluation_interval: 10s
#以下对接alertmanager
alerting:
alertmanagers:
- static_configs:
- targets:
- alertmanager:9093
#添加告警策略
rule_files:
- "/etc/prometheus-rules/*.rules"
#添加抓取
scrape_configs:
- job_name: 'blackbox_http'
metrics_path: /probe
params:
module: [http]
static_configs:
- targets:
- 'example.com:80' # 替换为你要监控的 HTTP 目标主机和端口
relabel_configs:
- source_labels: [__address__]
target_label: instance
regex: (.*)
replacement: "$1"
- source_labels: [__param_module]
target_label: module
- target_label: __param_target
replacement: example.com:80
- target_label: __address__
replacement: blackbox_exporter:9115
- job_name: 'blackbox_https'
metrics_path: /probe
params:
module: [https]
static_configs:
- targets:
- 'example.com:443' # 替换为你要监控的 HTTPS 目标主机和端口
relabel_configs:
- source_labels: [__address__]
target_label: instance
regex: (.*)
replacement: "$1"
- source_labels: [__param_module]
target_label: module
- target_label: __param_target
replacement: example.com:443
- target_label: __address__
replacement: blackbox_exporter:9115
- job_name: 'blackbox_tcp'
metrics_path: /probe
params:
module: [tcp]
static_configs:
- targets:
- 'example.com:22' # 替换为你要监控的 TCP 目标主机和端口
relabel_configs:
- source_labels: [__address__]
target_label: instance
regex: (.*)
replacement: "$1"
- source_labels: [__param_module]
target_label: module
- target_label: __param_target
replacement: example.com:22
- target_label: __address__
replacement: blackbox_exporter:9115
- job_name: 'blackbox_dns'
metrics_path: /probe
params:
module: [dns]
static_configs:
- targets:
- 'example.com' # 替换为你要监控的 DNS 目标主机
relabel_configs:
- source_labels: [__address__]
target_label: instance
regex: (.*)
replacement: "$1"
- source_labels: [__param_module]
target_label: module
- target_label: __param_target
replacement: example.com
- target_label: __address__
replacement: blackbox_exporter:9115
- job_name: 'blackbox_icmp'
metrics_path: /probe
params:
module: [icmp]
static_configs:
- targets:
- 'example.com' # 替换为你要监控的 ICMP 目标主机
relabel_configs:
- source_labels: [__address__]
target_label: instance
regex: (.*)
replacement: "$1"
- source_labels: [__param_module]
target_label: module
- target_label: __param_target
replacement: example.com
- target_label: __address__
replacement: blackbox_exporter:9115
配置 Prometheus警报规则
groups:
- name: example
rules:
- alert: HTTPProbeFailed
expr: probe_success == 0
for: 5m
labels:
severity: critical
instance: '{{ $labels.instance }}'
module: '{{ $labels.module }}'
annotations:
summary: "HTTP probe failed for {{ $labels.instance }}"
description: "HTTP probe failed for {{ $labels.instance }} in module {{ $labels.module }}"
- alert: TCProbeFailed
expr: probe_success == 0
for: 5m
labels:
severity: critical
instance: '{{ $labels.instance }}'
module: '{{ $labels.module }}'
annotations:
summary: "TCP probe failed for {{ $labels.instance }}"
description: "TCP probe failed for {{ $labels.instance }} in module {{ $labels.module }}"
- alert: DNSProbeFailed
expr: probe_success == 0
for: 5m
labels:
severity: critical
instance: '{{ $labels.instance }}'
module: '{{ $labels.module }}'
annotations:
summary: "DNS probe failed for {{ $labels.instance }}"
description: "DNS probe failed for {{ $labels.instance }} in module {{ $labels.module }}"
- alert: ICMPProbeFailed
expr: probe_success == 0
for: 5m
labels:
severity: critical
instance: '{{ $labels.instance }}'
module: '{{ $labels.module }}'
annotations:
summary: "ICMP probe failed for {{ $labels.instance }}"
description: "ICMP probe failed for {{ $labels.instance }} in module {{ $labels.module }}"
在上述示例中,我们定义了四个不同的警报规则,分别对应于 HTTP、TCP、DNS 和 ICMP 的探测失败。这些规则检查 probe_success
指标是否为零,并在连续 5 分钟内持续满足条件时触发警报。根据你的实际需求,你可能需要进一步调整这些规则。
将上述规则添加到 Prometheus 的警报规则文件中(通常是 prometheus.rules
),并确保 Prometheus 的配置文件中包含了警报规则文件的路径。重新加载 Prometheus 配置后,警报规则将生效。
加载Prometheus配置
curl -XPOST http://prometheus:9090/-/reload -v
配置 Grafana 或其他可视化工具
- 创建警报规则(可选):
如果你希望根据探测结果创建警报规则,你可以在 Prometheus 或 Alertmanager 中配置相应的警报规则。 - 监控和调整:
监控 Blackbox Exporter 的指标,根据需要调整配置文件,确保它符合你的监控需求。 - 模板
blackbox_exporter监控模板:9965 7587
这是一个简单的使用流程,实际上,根据你的监控需求和环境,可能需要进行更详细的配置和定制。请参考 Blackbox Exporter 的文档以获取更多详细信息和示例:Blackbox Exporter GitHub 页面。
通过以上步骤,你已经成功部署了 Blackbox Exporter 并集成到 Prometheus 中,为你的应用提供了更全面的监控能力。