Prometheus提供了一个blackbox_exporter可以实现网络监控,支持http、dns、tcp、icmp等监控。
blackbox_exporter的配置
blackbox_exporter以下面的命令运行:
blackbox_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 configuration和Blackbox exporter configuration Exmaple。下面的例子是一个最基本的配置:
modules:
http_2xx: # http 监测模块
prober: http
http:
http_post_2xx: # http post 监测模块
prober: http
http:
method: POST
tcp_connect: # tcp 监测模块
prober: tcp
ping: # icmp 检测模块
prober: icmp
timeout: 5s
icmp:
preferred_ip_protocol: "ip4"一般情况下都会以非root用户运行
blackbox_exporter,这里使用的prometheus用户,Wie了使用icmp prober,需要设置CAP_NET_RAW,即对可执行文件blackbox_exporter执行下面的命令:
setcap cap_net_raw+ep blackbox_exporter使用场景
ping检测
在内网可以通过ping (icmp)检测服务器的存活,以前面的最基本的module配置为例,在Prometheus的配置文件中配置使用ping module:
- job_name: 'ping_all'
scrape_interval: 1m
metrics_path: /probe
params:
module: [ping]
static_configs:
- targets:
- 192.168.1.2
labels:
instance: node2
- targets:
- 192.168.1.3
labels:
instance: node3
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- target_label: __address__
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。
curl "http://127.0.0.1:9115/probe?module=ping&target=192.168.1.2"
# HELP probe_dns_lookup_time_seconds Returns the time taken for probe dns lookup in seconds
# TYPE probe_dns_lookup_time_seconds gauge
probe_dns_lookup_time_seconds 2.6453e-05
# HELP probe_duration_seconds Returns how long the probe took to complete in seconds
# TYPE probe_duration_seconds gauge
probe_duration_seconds 0.000351649
# HELP probe_ip_protocol Specifies whether probe ip protocol is IP4 or IP6
# TYPE probe_ip_protocol gauge
probe_ip_protocol 4
# HELP probe_success Displays whether or not the probe was a success
# TYPE probe_success gauge
probe_success 1http检测
以前面的最基本的module配置为例,在Prometheus的配置文件中配置使用http_2xx module:
- job_name: 'http_get_all'
scrape_interval: 30s
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
- https://frognew.com
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115http检测除了可以探测http服务的存活外,还可以根据指标probe_ssl_earliest_cert_expiry进行ssl证书有效期预警。