tjinjin's blog

インフラ要素多めの個人メモ

prometheusのexporter紹介シリーズ1〜blackbox_exporter

About

こちらはprometheus Advent Calender6日目の記事です。prometheusにはexporterがたくさんあるので、アドベントカレンダーの穴埋め的に紹介していきます。

blackbox_exporterとは

簡単にいうと外形監視のためのexporterです。L7のHTTP・HTTPSだけでなくTCPDNS・ICMP・TLSなどのレイヤーでの監視が可能です。

設定ファイルの書き方

設定はblackbox_exporter側でmoduleの定義をし、prometheus側でどこに対して監視を投げるかを指定するようになっています。簡単な例だと下記のような設定ファイルを作成します。

# blackbox_exporter用の設定
modules:
  http_get_2xx:
    prober: http
    timeout: 5s
    http:
      preferred_ip_protocol: ip4

次にprometheus側の設定をします。これはhttp/httpsを使ってリクエストを投げる設定です。http_get_2xxという部分がmodule名です。これをprometheus側で指定します。

# prometheusの設定
scrape_configs:
  - job_name: 'blackbox'
    metrics_path: /probe
    params:
      module: [http_get_2xx]
    static_configs:
      - targets:
        - http://prometheus.io    # Target to probe with http.
        - https://prometheus.io   # Target to probe with https.
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: blackbox_exporter:9115  # Blackbox exporter.

blackbox_exporterの場合、metricsの情報は/probeのエンドポイントを叩く必要があります(/metricsblackbox_exporter本体の情報です)。

取得できるメトリクス

/probeを叩けばメトリクスがとれますが、parameterを指定する必要があります。

$ curl 'localhost:9115/probe?target=prometheus.io&module=http_get_2xx'
# HELP content_length Length of http content response
# TYPE content_length gauge
content_length -1
# HELP probe_dns_lookup_time_seconds Returns the time taken for probe dns lookup in seconds
# TYPE probe_dns_lookup_time_seconds gauge
probe_dns_lookup_time_seconds 0.006658805
# HELP probe_duration_seconds Returns how long the probe took to complete in seconds
# TYPE probe_duration_seconds gauge
probe_duration_seconds 0.517688144
# HELP probe_failed_due_to_regex Indicates if probe failed due to regex
# TYPE probe_failed_due_to_regex gauge
probe_failed_due_to_regex 0
# HELP probe_http_duration_seconds Duration of http request by phase, summed over all redirects
# TYPE probe_http_duration_seconds gauge
probe_http_duration_seconds{phase="connect"} 0.056152808
probe_http_duration_seconds{phase="processing"} 0.312427949
probe_http_duration_seconds{phase="resolve"} 0.009857009
probe_http_duration_seconds{phase="tls"} 0.193371334
probe_http_duration_seconds{phase="transfer"} 0.00012905
# HELP probe_http_redirects The number of redirects
# TYPE probe_http_redirects gauge
probe_http_redirects 1
# HELP probe_http_ssl Indicates if SSL was used for the final redirect
# TYPE probe_http_ssl gauge
probe_http_ssl 1
# HELP probe_http_status_code Response HTTP status code
# TYPE probe_http_status_code gauge
probe_http_status_code 200
# HELP probe_http_version Returns the version of HTTP of the probe response
# TYPE probe_http_version gauge
probe_http_version 1.1
# HELP probe_ip_protocol Specifies whether probe ip protocol is IP4 or IP6
# TYPE probe_ip_protocol gauge
probe_ip_protocol 4
# HELP probe_ssl_earliest_cert_expiry Returns earliest SSL cert expiry in unixtime
# TYPE probe_ssl_earliest_cert_expiry gauge
probe_ssl_earliest_cert_expiry 1.528156799e+09
# HELP probe_success Displays whether or not the probe was a success
# TYPE probe_success gauge
probe_success 1

細かい単位で情報が取れるので便利かなーと思います。

できること

実際に試してないですが、細かい制御ができます。http_proveの場合だと下記の指定ができるようです。

  • ステータスコード
  • HTTPのバージョン
  • HTTP method
  • HTTP header
  • redirectsの有無
  • SSL/TLSの有無によっての制御
  • responseの内容での制御
  • TLSの制御(各種ファイルの指定など)
  • basic認証
  • bearer_token
  • proxyのURL
  • ipv4/ipv6
  • request body

詳細はこちら まで

github.com