一个计算机技术爱好者与学习者

0%

Prometheus Blackbox exporter

1. 白盒监控和黑盒监控

我们监控主机的资源用量、容器的运行状态、数据库中间件的运行数据。这些都是支持业务和服务的基础设施,通过白盒能够了解其内部的实际运行状态,通过对监控指标的观察能够预判可能出现的问题,从而对潜在的不确定因素进行优化。而从完整的监控逻辑的角度,除了大量的应用白盒监控以外,还应该添加适当的黑盒监控。黑盒监控即以用户的身份测试服务的外部可见性,常见的黑盒监控包括HTTP探针、TCP探针等用于检测站点或者服务的可访问性,以及访问效率等。

黑盒监控相较于白盒监控最大的不同在于黑盒监控是以故障为导向当故障发生时,黑盒监控能快速发现故障,而白盒监控则侧重于主动发现或者预测潜在的问题。一个完善的监控目标是要能够从白盒的角度发现潜在问题,能够在黑盒的角度快速发现已经发生的问题。

Prometheus Blackbox exporter允许通过HTTP,HTTPS,DNS,TCP和ICMP对端点进行黑盒探测。

更多内容,参考网络探测:Blackbox Exporterprometheus/blackbox_exporter

本文中,我们会安装blackbox exporter,并且使用http探针对一些接口进行探活。

2. 安装使用blackbox exporter

blackbox exporter的常规安装使用方法有三种:直接使用二进制文件、使用docker image、编译安装。
这里我们选择使用docker image的方式来进行安装,默认已经安装了docker。

1、创建配置文件blackbox.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
modules:
http_2xx:
prober: http
http:
method: GET
preferred_ip_protocol: "ip4"
http_post_2xx:
prober: http
http:
method: POST
preferred_ip_protocol: "ip4"
tcp:
prober: tcp
ping:
prober: icmp
timeout: 3s
icmp:
preferred_ip_protocol: "ip4"
dns_baidu:
prober: dns
timeout: 5s
dns:
query_name: "baidu.com"
query_type: "A"
preferred_ip_protocol: "ip4"

探测时默认使用ipv6,探测不支持ipv6的站点都会显示失败,因此在配置时改成了ipv4,更多内容参考UNDERSTANDING AND USING THE MULTI-TARGET EXPORTER PATTERN

2、启动blackbox exporter

1
2
3
4
docker run --name blackbox-exporter -d \
-p 9115:9115 \
-v `pwd`:/config \
prom/blackbox-exporter:v0.16.0 --config.file=/config/blackbox.yml

3、测试

1
2
3
4
5
curl "http://127.0.0.1:9115/probe?module=http_2xx&target=baidu.com"
curl "http://127.0.0.1:9115/probe?module=http_post_2xx&target=baidu.com"
curl "http://127.0.0.1:9115/probe?module=tcp&target=baidu.com:80"
curl "http://127.0.0.1:9115/probe?module=ping&target=baidu.com"
curl "http://127.0.0.1:9115/probe?module=dns_baidu&target=180.76.76.76"

从返回的样本中,可以获取站点的DNS解析耗时、站点响应时间、HTTP响应状态码等等和站点访问质量相关的监控指标,从而帮助管理员主动的发现故障和问题。

3. 与prometheus集成

紧接着《使用Docker安装配置Prometheus》,当前prometheus.yml配置内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
global:
scrape_interval: 15s #默认采集监控数据时间间隔
external_labels:
monitor: 'my-monitor'
scrape_configs: #监控对象设置
- job_name: prometheus #任务名称
scrape_interval: 5s #每隔5s获取一次监控数据
static_configs: #监控对象地址
- targets: ['127.0.0.1:9090'] # 将自己加入到监控对象中
- targets: ['192.168.56.102:9100']
labels:
group: 'client-node-exporter'
- targets: ['192.168.56.102:9091']
labels:
group: 'pushgateway'
rule_files:
- /etc/prometheus/rules.yml #告警规则文件路径
alerting: #告警管理器设置
alertmanagers:
- static_configs:
- targets: ['192.168.56.102:9093'] #告警信息会发送给alertmanager进一步处理

3.1. 简单配置

修改prometheus.yml,添加blackbox exporter相关字段。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- job_name: baidu_http2xx_probe
params:
module:
- http_2xx
target:
- baidu.com
metrics_path: /probe
static_configs:
- targets:
- 127.0.0.1:9115
- job_name: prometheus_http2xx_probe
params:
module:
- http_2xx
target:
- prometheus.io
metrics_path: /probe
static_configs:
- targets:
- 127.0.0.1:9115

这里分别配置了名为baidu_http2x_probe和prometheus_http2xx_probe的采集任务,并且通过params指定使用的探针(module)以及探测目标(target)。

3.2. 高级配置

以上配置会有一个问题,假如我们有N个目标站点且都需要M种探测方式,那么Prometheus中将包含N * M 个采集任务,从配置管理的角度来说显然是不可接受的。这里我们利用Prometheus的Relabeling方式对这些配置进行简化,配置方式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
scrape_configs:  #监控对象设置
- job_name: prometheus #任务名称
scrape_interval: 5s #每隔5s获取一次监控数据
static_configs: #监控对象地址
- targets: ['127.0.0.1:9090'] # 将自己加入到监控对象中
- targets: ['192.168.56.102:9100']
labels:
group: 'client-node-exporter'
- targets: ['192.168.56.102:9091']
labels:
group: 'pushgateway'
- targets: ['192.168.56.102:9115']
- job_name: 'blackbox'
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
- http://prometheus.io # Target to probe with http.
- https://prometheus.io # Target to probe with https.
- http://example.com:8080 # Target to probe with http on port 8080.
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.56.102:9115 # The blackbox exporter's real hostname:port.

这里针对每一个探针服务(如http_2xx)定义一个采集任务,并且直接将任务的采集目标定义为我们需要探测的站点。在采集样本数据之前通过relabel_configs对采集任务进行动态设置。

上面的配置,实际上相当于:

1
2
3
curl "http://192.168.56.102:9115/probe?module=http_2xx&target=http://prometheus.io"
curl "http://192.168.56.102:9115/probe?module=http_2xx&target=https://prometheus.io"
curl "http://192.168.56.102:9115/probe?module=http_2xx&target=http://example.com:8080"

这个配置实际上是很奇怪的,因为第一个job中 static_configs.targets 代表的是用来exporter对外暴露的接口,第二个job中 static_configs.targets 却代表blackbox要探测的站点。而且第二个job的 relabel_configs.replacement 居然用来指定blackbox的url,我也是服气了,说好的用来替换标签值的呢?

重启prometheus,然后进行验证:
(1)访问blackbox-exporter页面,可以看到探测记录的日志。
(2)访问prometheus的target页面,即可看到blackbox-exporter。
(3)访问prometheus的graph页面,可以查看blackbox-exporter指标。

1
{job=~"blackbox",__name__!~"^go.*"}