blackbox_exporter又称黑盒测试,可以提供 http、dns、tcp、icmp 的监控数据采集,SSL证书过期时间采集等。

安装blackbox_exporter

#1、部署blackbox_exporter

官方下载网址:https://prometheus.io/download/

  1. mkdir /usr/local/monitor/ && cd /usr/local/monitor/
  2. wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.19.0/blackbox_exporter-0.19.0.linux-amd64.tar.gz
  3. tar xf blackbox_exporter-0.19.0.linux-amd64.tar.gz

#2、通过systemctl管理blackbox_exporter

  1. #增加系统管理软件文件
  2. vim/etc/systemd/system/blackbox_exporter.service
  3. [Unit]
  4. Description=blackbox_exporter
  5. After=network.target
  6. [Service]
  7. Type=simple
  8. User=root
  9. ExecStart=/usr/local/monitor/blackbox_exporter-0.19.0.linux-amd64/blackbox_exporter --config.file=/usr/local/monitor/blackbox_exporter-0.19.0.linux-amd64/blackbox.yml
  10. Rastart=on-failure
  11. [Install]
  12. WantedBy=multi-user.target

#3、重新加载配置,启动服务

  1. systemctl daemon-reload
  2. systemctl restart blackbox_exporter.service
  3. systemctl enable blackbox_exporter.service

配置prometheus

ref:https://github.com/prometheus/blackbox_exporter

需要根据情况在prometheus.yml配置文件中添加下面配置

http测试(监控网站状态)

  1. scrape_configs:
  2. - job_name: 'web_status'
  3. metrics_path: /probe
  4. params:
  5. module: [http_2xx] # Look for a HTTP 200 response.
  6. static_configs:
  7. - targets:
  8. - http://prometheus.io # Target to probe with http.
  9. - https://prometheus.io # Target to probe with https.
  10. - http://example.com:8080 # Target to probe with http on port 8080.
  11. relabel_configs:
  12. - source_labels: [__address__]
  13. target_label: __param_target
  14. - source_labels: [__param_target]
  15. target_label: instance
  16. - target_label: __address__
  17. replacement: 127.0.0.1:9115 # The blackbox exporter's real hostname:port.
  • 手动测试

手动访问 http_2xx 这个模板(在blackbox.yml配置文件中执行的),判断 域名http://example.com:8080是否能访问

curl http://127.0.0.1:9115/probe?target=http://example.com:8080&module=http_2xx&debug=true

icmp测试(监控主机存活状态)

  1. scrape_configs:
  2. - job_name: 'node_status'
  3. metrics_path: /probe
  4. params:
  5. module: [icmp]
  6. static_configs:
  7. - targets: ['10.165.94.31']
  8. labels:
  9. instance: node_status
  10. group: 'node'
  11. relabel_configs:
  12. - source_labels: [__address__]
  13. target_label: __param_target
  14. - source_labels: [__param_target]
  15. target_label: instance
  16. - target_label: __address__
  17. replacement: 127.0.0.1:9115 # The blackbox exporter's real hostname:port.

tcp测试(监控主机端口存活状态)

  1. scrape_configs:
  2. - job_name: 'port_status'
  3. metrics_path: /probe
  4. params:
  5. module: [tcp_connect]
  6. static_configs:
  7. - targets: ['172.19.155.133:8765']
  8. labels:
  9. instance: 'port_status'
  10. group: 'tcp'
  11. relabel_configs:
  12. - source_labels: [__address__]
  13. target_label: __param_target
  14. - source_labels: [__param_target]
  15. target_label: instance
  16. - target_label: __address__
  17. replacement: 127.0.0.1:9115 # The blackbox exporter's real hostname:port.

post测试(接口联通性)

  1. scrape_configs:
  2. - job_name: 'blackbox_http_2xx_post'
  3. scrape_interval: 10s
  4. metrics_path: /probe
  5. params:
  6. module: [http_post_2xx_query]
  7. static_configs:
  8. - targets:
  9. - https://xx.xxx.com/api/xx/xx/fund/query.action
  10. labels:
  11. group: 'Interface monitoring'
  12. relabel_configs:
  13. - source_labels: [__address__]
  14. target_label: __param_target
  15. - source_labels: [__param_target]
  16. target_label: instance
  17. - target_label: __address__
  18. replacement: 127.0.0.1:9115 # The blackbox exporter's real hostname:port.

配置blackbox_exporter 图表

grafana网站上搜索一个 blackbox_exporter 的图表,此处我选择的是 7587;然后import进入prometheus grafana

image.png
从图标中可以看出请求的URL的状态,状态码,SSL状态,请求的平均返回时间等信息。