Prometheus提供了一个blackbox_exporter可以实现网络监控,支持http、dns、tcp、icmp等监控。

blackbox_exporter的配置

blackbox_exporter以下面的命令运行:

1blackbox_exporter --web.listen-address=:9115 --config.file=blackbox.yml

其中9115是这个exporter的http端点的监听端口,blackbox.yml是它的配置文件,需要在其中使用blackbox_exporter的http、dns、tcp、icmp等prober定制配置出各种监测模块(module)。关于blackbox_exporter的配置具体参考Blackbox exporter configurationBlackbox exporter configuration Exmaple。下面的例子是一个最基本的配置:

 1modules:
 2  http_2xx:  # http 监测模块
 3    prober: http
 4    http:
 5  http_post_2xx: # http post 监测模块
 6    prober: http
 7    http:
 8      method: POST
 9  tcp_connect: # tcp 监测模块
10    prober: tcp
11  ping: # icmp 检测模块
12    prober: icmp
13    timeout: 5s
14    icmp:
15      preferred_ip_protocol: "ip4"

一般情况下都会以非root用户运行blackbox_exporter,这里使用的prometheus用户,Wie了使用icmp prober,需要设置CAP_NET_RAW,即对可执行文件blackbox_exporter执行下面的命令:

1setcap cap_net_raw+ep blackbox_exporter

使用场景

ping检测

在内网可以通过ping (icmp)检测服务器的存活,以前面的最基本的module配置为例,在Prometheus的配置文件中配置使用ping module:

 1- job_name: 'ping_all'
 2    scrape_interval: 1m
 3    metrics_path: /probe
 4    params:
 5      module: [ping]
 6    static_configs:
 7     - targets:
 8        - 192.168.1.2
 9       labels:
10         instance: node2
11     - targets:
12        - 192.168.1.3
13       labels:
14         instance: node3
15    relabel_configs:
16      - source_labels: [__address__]
17        target_label: __param_target
18      - target_label: __address__
19        replacement: 127.0.0.1:9115 # black_exporter 这里和Prometheus在一台机器上

通过配置文件可以很直接的看出Prometheus使用black_exporter作为代理使用black_exporter配置的module检测各个target的状态。 下面是一个http://127.0.0.1:9115/probe?module=ping&target=192.168.1.2返回的是192.168.1.2这个target的metrics。

 1curl "http://127.0.0.1:9115/probe?module=ping&target=192.168.1.2"
 2# HELP probe_dns_lookup_time_seconds Returns the time taken for probe dns lookup in seconds
 3# TYPE probe_dns_lookup_time_seconds gauge
 4probe_dns_lookup_time_seconds 2.6453e-05
 5# HELP probe_duration_seconds Returns how long the probe took to complete in seconds
 6# TYPE probe_duration_seconds gauge
 7probe_duration_seconds 0.000351649
 8# HELP probe_ip_protocol Specifies whether probe ip protocol is IP4 or IP6
 9# TYPE probe_ip_protocol gauge
10probe_ip_protocol 4
11# HELP probe_success Displays whether or not the probe was a success
12# TYPE probe_success gauge
13probe_success 1

http检测

以前面的最基本的module配置为例,在Prometheus的配置文件中配置使用http_2xx module:

 1- job_name: 'http_get_all'
 2    scrape_interval: 30s
 3    metrics_path: /probe
 4    params:
 5      module: [http_2xx]
 6    static_configs:
 7      - targets:
 8        - https://frognew.com
 9    relabel_configs:
10      - source_labels: [__address__]
11        target_label: __param_target
12      - source_labels: [__param_target]
13        target_label: instance
14      - target_label: __address__
15        replacement: 127.0.0.1:9115

http检测除了可以探测http服务的存活外,还可以根据指标probe_ssl_earliest_cert_expiry进行ssl证书有效期预警。

参考