一、概述

blackbox_exporter 官方文档

blackbox_exporter是Prometheus 官方提供的 exporter 之一,可以提供 http、dns、tcp、icmp 的监控数据采集。
Blackbox_exporter 应用场景

1、 HTTP 测试

  • 定义 Request Header 信息
  • 判断 Http status / Http Respones Header / Http Body 内容

2、 TCP 测试

  • 业务组件端口状态监听
  • 应用层协议定义与监听

3、 ICMP 测试

  • 主机探活机制

4、 POST 测试

  • 接口联通性

5、 SSL 证书过期时间

二、安装

blackbox_exporter安装包

以linux系统为例,下载编译好的二进制包,解压使用:

1、下载并解压

# wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.19.0/blackbox_exporter-0.19.0.linux-amd64.tar.gz
# tar -zxvf blackbox_exporter-0.19.0.linux-amd64.tar.gz 
# cd blackbox_exporter-0.19.0.linux-amd64/

2、验证是否安装成功

# ./blackbox_exporter --version
blackbox_exporter, version 0.19.0 (branch: HEAD, revision: 5d575b88eb12c65720862e8ad2c5890ba33d1ed0)
  build user:       root@2b0258d5a55a
  build date:       20210510-12:56:44
  go version:       go1.16.4
  platform:         linux/amd64

3、启动blackbox_exporter

nohup ./blackbox_exporter &

4、验证是否启动成功

# netstat -utpln|grep blackbox
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp6       0      0 :::9115                 :::*                    LISTEN      25185/./blackbox_ex 

5、创建systemcd服务(centos7系统可以用)

$ vim /lib/systemd/system/blackbox_exporter.service

[Unit]
Description=blackbox_exporter
After=network.target

[Service]
User=root
Type=simple
ExecStart=/usr/local/blackbox_exporter/blackbox_exporter --config.file=/usr/local/blackbox_exporter/blackbox.yml
Restart=on-failure

[Install]
WantedBy=multi-user.target

三、prometheus.yml中加入blackbox_exporter

1、监控主机存活状态

# vim prometheus.yml
- job_name: crawler_status
    metrics_path: /probe
    params:
      module: [icmp]
    static_configs:
      - targets: ['10.45.3.8','10.45.3.9','10.45.3.10','10.45.3.11']
        labels:
          instance: node_status
          group: 'node'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 127.0.0.1:9115

注意:10.45.3.8是被监控端ip,127.0.0.1是Blackbox_exporter

2、监控主机端口存活状态

- job_name: 'crawler_port_status'
    metrics_path: /probe
    params:
      module: [tcp_connect]
    static_configs:
      - targets: ['10.45.3.8:3128']
        labels:
          instance: 'port_status'
          group: 'tcp'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 127.0.0.1:9115

3、监控网站状态

- job_name: qianxin_web_status
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets: ['https://www.qianxin.com']
        labels:
          instance: qianxin_web_status
          group: 'web'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - target_label: __address__
        replacement: 127.0.0.1:9115

- job_name: baidu_web_status
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets: ['https://www.baidu.com']
        labels:
          instance: baidu_web_status
          group: 'web'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - target_label: __address__
        replacement: 127.0.0.1:9115

- job_name: jd_web_status
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets: ['https://www.jd.com']
        labels:
          instance: jd_web_status
          group: 'web'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - target_label: __address__
        replacement: 127.0.0.1:9115

4、检查配置文件是否书写正确

# ./promtool check config prometheus.yml

5、重启prometheus

6、查看targets界面

访问 http://127.0.0.1:9090/targets ,确保状态为 UP
20211009112025.png

四、grafana中加入blackbox_exporter监控数据

1、安装grafana

# docker run -d -p 3000:3000 --name grafana grafana/grafana

运行完成打开,127.0.0.1:3000

登录页面输入 超级管理员的账号密码

第一次登录 默认 账号 admin 密码admin

2、添加数据源

grafana添加数据源20211009113127.png
数据源选择grafana20211009113153.png
配置数据源详细信息20211009113225.png

五、grafana中加入blackbox_exporter监控数据

1、添加模版

https://grafana.com/grafana/dashboards/9965
https://grafana.com/grafana/dashboards/9719
9965.png

此模板需要安装饼状图插件 下载地址 https://grafana.com/grafana/plugins/grafana-piechart-panel
安装插件,重启grafana生效。

# docker exec -it grafana grafana-cli plugins install grafana-piechart-panel
# docker restart grafana

效果图.png