# Prometheus-黑盒blackbox

### **安装** <a href="#e5-ae-89-e8-a3-85" id="e5-ae-89-e8-a3-85"></a>

```
[ -d "/data/monitor" ] || mkdir /data/monitor -p
cd /data/monitor

wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.24.0/blackbox_exporter-0.24.0.linux-amd64.tar.gz
tar fx blackbox_exporter-0.24.0.linux-amd64.tar.gz
mv blackbox_exporter-0.24.0.linux-amd64 blackbox_exporter
cd blackbox_exporter
```

### 配置文件： blackbox.yml <a href="#e9-85-8d-e7-bd-ae-e6-96-87-e4-bb-b6-ef-bc-9a-blackbox.yml" id="e9-85-8d-e7-bd-ae-e6-96-87-e4-bb-b6-ef-bc-9a-blackbox.yml"></a>

```
modules:
  http_2xx:
    prober: http
    timeout: 15s  
    http:
      fail_if_not_ssl: true
      ip_protocol_fallback: false
      method: GET
      follow_redirects: false
      preferred_ip_protocol: ip4
      enable_http2: true
      tls_config:
        insecure_skip_verify: true
      valid_http_versions:
        - HTTP/1.1
        - HTTP/2.0
      valid_status_codes:
        - 200
        - 201
        - 204
        - 301
        - 302
        - 307
        - 400
        - 401
        - 403
        - 404
        - 405
  http_post_2xx:
    prober: http
    http:
      method: POST
  tcp_connect:
    prober: tcp
  pop3s_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: "^+OK"
      tls: true
      tls_config:
        insecure_skip_verify: false
  grpc:
    prober: grpc
    grpc:
      tls: true
      preferred_ip_protocol: "ip4"
  grpc_plain:
    prober: grpc
    grpc:
      tls: false
      service: "service1"
  ssh_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: "^SSH-2.0-"
      - send: "SSH-2.0-blackbox-ssh-check"
  irc_banner:
    prober: tcp
    tcp:
      query_response:
      - send: "NICK prober"
      - send: "USER prober prober prober :prober"
      - expect: "PING :([^ ]+)"
        send: "PONG ${1}"
      - expect: "^:[^ ]+ 001"
  icmp:
    prober: icmp
  icmp_ttl5:
    prober: icmp
    timeout: 5s
    icmp:
      ttl: 5
```

### **方式一： 终端方式** <a href="#e6-96-b9-e5-bc-8f-e4-b8-80-ef-bc-9a-e7-bb-88-e7-ab-af-e6-96-b9-e5-bc-8f" id="e6-96-b9-e5-bc-8f-e4-b8-80-ef-bc-9a-e7-bb-88-e7-ab-af-e6-96-b9-e5-bc-8f"></a>

```
blackbox_exporter --config.file=blackbox.yml
```

### **方式二： systemd 服务方式** <a href="#e6-96-b9-e5-bc-8f-e4-ba-8c-ef-bc-9a-systemd-e6-9c-8d-e5-8a-a1-e6-96-b9-e5-bc-8f" id="e6-96-b9-e5-bc-8f-e4-ba-8c-ef-bc-9a-systemd-e6-9c-8d-e5-8a-a1-e6-96-b9-e5-bc-8f"></a>

```
cat > /usr/lib/systemd/system/blackbox_exporter.service << EOF
[Unit]
Description=blackbox_exporter
After=network.target 
[Service]
WorkingDirectory=/data/monitor/blackbox_exporter
ExecStart=blackbox_exporter          --config.file=blackbox.yml
[Install]
WantedBy=multi-user.target
EOF
```

### **方式三： supervisor 进程方式** <a href="#e6-96-b9-e5-bc-8f-e4-b8-89-ef-bc-9a-supervisor-e8-bf-9b-e7-a8-8b-e6-96-b9-e5-bc-8f" id="e6-96-b9-e5-bc-8f-e4-b8-89-ef-bc-9a-supervisor-e8-bf-9b-e7-a8-8b-e6-96-b9-e5-bc-8f"></a>

```
cat > /etc/supervisor/conf.d/blackbox.conf 
[program:blackbox]
directory = /data/monitor/blackbox_exporter/
command = /data/monitor/blackbox_exporter/blackbox_exporter --config.file=/data/monitor/blackbox_exporter/blackbox.yml
autostart = true
startsecs = 5
autorestart = true
startretries = 3
user = root
redirect_stderr = true
stdout_logfile = /data/monitor/logs/blackbox.log
EOF
```

### **参考配置格式** <a href="#e5-8f-82-e8-80-83-e9-85-8d-e7-bd-ae-e6-a0-bc-e5-bc-8f" id="e5-8f-82-e8-80-83-e9-85-8d-e7-bd-ae-e6-a0-bc-e5-bc-8f"></a>

> 新建一个 http\_<自定义的名字>.yml 的配置文件，格式复制，域名扔进去， 不加 https:// ，默认 http（如果没有强制 ssl,  则不会检查https）

### **检查配置，设置立即生效** <a href="#e6-a3-80-e6-9f-a5-e9-85-8d-e7-bd-ae-ef-bc-8c-e8-ae-be-e7-bd-ae-e7-ab-8b-e5-8d-b3-e7-94-9f-e6-95-88" id="e6-a3-80-e6-9f-a5-e9-85-8d-e7-bd-ae-ef-bc-8c-e8-ae-be-e7-bd-ae-e7-ab-8b-e5-8d-b3-e7-94-9f-e6-95-88"></a>

```
cd /data/monitor/prometheus/
./promtool check config ./prometheus.yml
curl -XPOST 127.0.0.1:8090/-/reload
```

\
