blackbox_ The use of exporters

Time:2021-6-10

1、 Demand

Sometimes, we want to monitor whether a certain port of the server exists and whether the HTTP request is correctblackbox_exporterTo achieve.

be careful:
Blackbox exporter can implement thehttphttpsTCP (can realize whether the server interface is online)ICMP (implement host detection)dnsIt’s a good detection.

2、 Bldeckbox_ Installation of exporter

1. Download

#Download
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.18.0/blackbox_exporter-0.18.0.darwin-amd64.tar.gz
#Decompression
tar -zxvf blackbox_exporter-0.18.0.darwin-amd64.tar.gz
#Rename
mv blackbox_exporter-0.18.0.darwin-amd64/ blackbox_exporter
#Go to blackbox_ Exporter directory
cd blackbox_exporter

blackbox_ The use of exporters

Download address- https://github.com/prometheus/blackbox_ exporter/releases

2. Writing blackbox.yml configuration file

modules:
  http_ 2XX: # this name is written casually, but it needs to be corresponding in the prometheus.yml configuration file.
    Prober: http # protocol for detection, which can be HTTP, TCP, DNS, ICMP
    timeout: 10s
  http_post_2xx:
    prober: http
    http:
      method: POST
  tcp_ Connect: # TCP detection, such as detecting whether a port is online
    prober: tcp
  ICMP: # ICMP detection, such as ping a server
    prober: icmp

be careful:
1. In the upper modulehttporicmpEtc. are not fully configured. This can be accessed throughhttps://github.com/prometheus/blackbox_exporter/blob/master/CONFIGURATION.mdView the complete configuration.

2. If you want to useicmp, then it may be necessary to enhance the permissions for different operating systems.
blackbox_ The use of exporters

3. Start blackbox_ exporter

#!/usr/bin

nohup /Users/huan/soft/prometheus/blackbox_exporter/blackbox_exporter \
--config.file="/Users/huan/soft/prometheus/blackbox_exporter/blackbox.yml" \
--web.listen-address=":9098" \
--log.level=debug \
> logs/blackbox.out 2>&1 &

visithttp://localhost:9098

blackbox_ The use of exporters

4. Integration with Prometheus

prometheus.ymlThe compilation of configuration file.

scrape_configs:
  - job_ name: 'blackbox_ http_ 2XX '# configure get request detection
    scrape_interval: 30s
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      -Targets: # test whether the following requests are accessible
        - 127.0.0.1:10005
        - http://127.0.0.1:10005/hello/zhangsan
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        Replacement: 127.0.0.1:9098 ᦇ the machine and port of the blackbox exporter service
  - job_ name: 'blackbox_ http_ post_ 2XX '# configure post request detection
    scrape_interval: 30s
    metrics_path: /probe
    params:
      module: [http_post_2xx]
    static_configs:
      -Targets: # test whether the following post request can be accessed. The post request has no parameters
        - 127.0.0.1:10005
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        Replacement: 127.0.0.1:9098 ᦇ the machine and port of the blackbox exporter service
  - job_ name: 'blackbox_ http_ Ping '# check whether some machines can be pinged
    scrape_interval: 30s
    metrics_path: /probe
    params:
      module: [icmp]
    static_configs:
      - targets:
        - 127.0.0.1
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        Replacement: 127.0.0.1:9098 ᦇ the machine and port of the blackbox exporter service
  - job_ name: 'blackbox_ tcp_ Connect '# check whether some ports are online
    scrape_interval: 30s
    metrics_path: /probe
    params:
      module: [tcp_connect]
    static_configs:
      - targets:
        - 127.0.0.1:10006
        - 127.0.0.1:10005
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        Replacement: 127.0.0.1:9098 ᦇ the machine and port of the blackbox exporter service

Manual access

Manual accesstcp_connectThis template (executed in the blackbox.yml configuration file) determines whether port 10005 on 127.0.0.1 machine is connected.

curl http://127.0.0.1:9098/probe?target=127.0.0.1:10005&module=tcp_connect&debug=true

Explanation:
1. When job equals blackbox_ http_ 2XX and blackbox_ http_ post_ 2XX let’s test whether a get and post request can be accessed.
2. When job equals blackbox_ tcp_ Connect to test whether a port is connected.

5. See in grafana.

1. First inGrafana websiteSearch for a blackbox on_ The chart of the exporter, which I choose here is12275

2. Import it into grafana, and make some minor changes according to your actual situation.

blackbox_ The use of exporters

6. If necessary, monitor the alarm

1、probe_successIndicators can judge whether our ICMP, TCP, HTTP, post monitoring is normal. The value of 1 is normal and 0 is abnormal. 2、probe_http_status_codeThe indicator can know the response code returned by an HTTP request.
3、probe_ssl_earliest_cert_expiryThe indicator can know when the SSL certificate will expire.

3、 Reference link

1、blackbox_ Download address of the exporter
2、Example of writing blackbox.yml
3、Configuration properties that can be written in blackbox.yml