Prometheus學習之Blackbox

簡介

Prometheus 的探針監控可以在應用程序的外部對應用程序進行探測,比如:探測機器的 http 服務是否工作正常等。 這裏就看看怎樣使用 Prometheus 的 Blackbox Exporter 來實現這個功能。

Prometheus 的 Blackbox Exporter 允許通過 HTTP/HTTPS,TCP 和 ICMP 等來探測端點。

安裝

首先從 https://prometheus.io/download/#blackbox_exporter 地址下載 blackbox_exporter 安裝包,我這裏使用的是 Linux 安裝包 blackbox_exporter-0.16.0.linux-amd64.tar.gz。

$ wget -c https://github.com/prometheus/blackbox_exporter/releases/download/v0.16.0/blackbox_exporter-0.16.0.linux-amd64.tar.gz
$ tar zxvf blackbox_exporter-0.16.0.linux-amd64.tar.gz
$ cd blackbox_exporter-*
$ ./blackbox_exporter --version
blackbox_exporter, version 0.16.0 (branch: HEAD, revision: 991f89846ae10db22a3933356a7d196642fcb9a9)
  build user:       root@64f600555645
  build date:       20191111-16:27:24
  go version:       go1.13.4

運行blackbox_exporter

修改安裝目錄下的 blackbox.yml 文件,如下:

modules:
  http_2xx:
    prober: http
    timeout: 5s
    http:
      valid_status_codes: [200]
      method: GET
  icmp:
    prober: icmp
    timeout: 5s

其中:

  • http_2xx 配置檢查 http 服務是否運行,並返回 2xx 狀態碼。
  • icmp 配置檢查 ICMP ping 是否正常。

現在運行使用此配置文件運行 blackbox_exporter

$ sudo ./blackbox_exporter --config.file=blackbox.yml
level=info ts=2020-02-16T09:09:46.523Z caller=main.go:212 msg="Starting blackbox_exporter" version="(version=0.16.0, branch=HEAD, revision=991f89846ae10db22a3933356a7d196642fcb9a9)"
level=info ts=2020-02-16T09:09:46.523Z caller=main.go:213 msg="Build context" (gogo1.13.4,userroot@64f600555645,date20191111-16:27:24)=(MISSING)
level=info ts=2020-02-16T09:09:46.523Z caller=main.go:225 msg="Loaded config file"
level=info ts=2020-02-16T09:09:46.524Z caller=main.go:369 msg="Listening on address" address=:9115

服務啓動後,可以通過瀏覽器訪問 http://localhost:9115/ 來查看狀態。

服務啓動後,我們可以做一下簡單測試,分別訪問項目兩個鏈接地址
http://localhost:9115/probe?target=localhost&module=http_2xx
http://localhost:9115/probe?target=localhost&module=icmp

然後在 http://localhost:9115/ 頁面查看結果,可以看到類似如下的內容。

Module	Target	Result	Debug
http_2xx	localhost	Success	Logs
icmp	localhost	Success	Logs

啓動Prometheus

現在 blackbox_exporter 服務已經運行了,下面就需要添加兩個作業 blackbox_http 和 blackbox_icmp 來收集這些指標了,下面是完整的 prometheus.yml 文件內容:

global:
  scrape_interval:     15s
  evaluation_interval: 15s

alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'node'
    static_configs:
    - targets: ['localhost:9100']

  - job_name: 'blackbox_http'
    metrics_path: /probe
    static_configs:
      - targets:
        - http://localhost 
        - https://localhost
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: localhost:9115

  - job_name: 'blackbox_icmp'
    metrics_path: /probe
    static_configs:
      - targets:
        - localhost 
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: localhost:9115

啓動 prometheus 服務

$ sudo ./prometheus --config.file=prometheus.yml

服務啓動後,通過 prometheus 監控頁面查詢 {job=‘blackbox_http’} 和 {job=‘blackbox_icmp’} 指標即可看到指標結果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章