什么是 blackbox exporter?

Blackbox Exporter 是 Prometheus 社區(qū)提供的 官方黑盒監(jiān)控解決方案,其允許用戶通過: http\\\\HTTPS\\\\dns\\\\TCP\\\\ICMP的方式對網(wǎng)絡(luò)進行探測.

以 docker 方式運行 blackbox exporter

拉取鏡像

docker pull prom/blackbox-exporter

查看 dockerfile(這是一個好習(xí)慣)

$ cat check_docker_file.sh
#!/bin/bash
export PATH=$PATH
if [ $# -eq 1 ];then
        docker history --format {{.CreatedBy}} --no-trunc=true $1 |sed s/\\\\/bin\\\\/sh\\\\ -c\\\\ \\\\#(nop)\\\\ //g|sed s/\\\\/bin\\\\/sh\\\\ -c/RUN/g | tac
    else
        echo sh Obtain_dockerfile.sh $DOCKER_IMAGE
fi

$ sh check_docker_file.sh prom/blackbox-exporter:latest
ADD file:b265aa0ea2ef7ff1f4a3e087217e75aca2c90f5c345406299664cc7969b2b28e in /
 CMD [sh]
 MAINTAINER The Prometheus Authors <prometheus-developers@googlegroups.com>
COPY dir:b1c1c3c551755544b818d03ad9136b137ca12c48393ba5cdd58d7f845647e042 in /
 LABEL maintainer=The Prometheus Authors <prometheus-developers@googlegroups.com>
 ARG ARCH=amd64
 ARG OS=linux
COPY file:2bfe91827ebb767bc51f40cd84675a3c315d9da8a70f6d8071c806e0b2b1ee73 in /bin/blackbox_exporter
COPY file:6e820c2d591d3433d139b66241b74e9b7ffc90c9e120bac49cf97014e16f070a in /etc/blackbox_exporter/config.yml
 EXPOSE 9115
 ENTRYPOINT [/bin/blackbox_exporter]
 CMD [--config.file=/etc/blackbox_exporter/config.yml]

運行 blackbox exporter

docker run -id --name blackbox-exporter -p 9115:9115  prom/blackbox-exporter

blackbox exporter 配置文件解讀

官方解釋:https://github.com/prometheus/blackbox_exporter/blob/master/CONFIGURATION.md

modules:
  http_2xx:
    prober: http
  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
  ssh_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: ^SSH-2.0-
  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

運行 blackbox exporter 時,需要用戶提供探針的配置信息,這些配置信息可能是一些自定義的 HTTP 頭信息,也可能是探測時需要的一些 TSL(秘鑰證書) 配置,也可能是探針本身的驗證行為.在 blackbox exporter 每一個探針配置稱為一個 module,并且以 YAML 配置文件的形式提供給 blackbox exporter.每一個 module 主要包含以下配置內(nèi)容,探針類型(prober),驗證訪問超時時間(timeout),以及當(dāng)前探針的具體配置項:

# 探針類型: http https tcp dns icmp
prober: <prober_string>   #必選

# 超時時間:
[timeout: <duration>] #默認(rèn)單位秒

# 探針的詳細(xì)配置,最多只能配置其中一個
[ http: <http_probe> ]
[ tcp: <tcp_probe> ]
[ dns: <dns_probe> ]
[ icmp: <icmp_probe> ]

<http_probe>可配置參數(shù)

# 此探針接受的狀態(tài)代碼。 默認(rèn)為2xx。
  [ valid_status_codes: <int>, ... | default = 2xx ]

# 此探針接受的 HTTP 版本.
  [ valid_http_versions: <string>, ... ]

#探針將使用的HTTP方法。
  [ method: <string> | default = GET ]

# 為探針設(shè)置的HTTP標(biāo)頭。
  headers:
    [ <string>: <string> ... ]

# 探針是否將遵循任何重定向
  [ no_follow_redirects: <boolean> | default = false ]

# 如果存在SSL,則探測失敗。
  [ fail_if_ssl: <boolean> | default = false ]

# 如果不存在SSL,則探測失敗。
  [ fail_if_not_ssl: <boolean> | default = false ]

# 如果響應(yīng)主體與正則表達式匹配,則探測失敗。
  fail_if_body_matches_regexp:
    [ - <regex>, ... ]

# 如果響應(yīng)主體與正則表達式不匹配,則探測失敗。
  fail_if_body_not_matches_regexp:
    [ - <regex>, ... ]

# 如果響應(yīng)頭與正則表達式匹配,則探測失敗。 對于具有多個值的標(biāo)頭,如果*至少一個*匹配,則失敗。
  fail_if_header_matches:
    [ - <http_header_match_spec>, ... ]

# 如果響應(yīng)頭與正則表達式不匹配,則探測失敗。 對于具有多個值的標(biāo)頭,如果* none *不匹配,則失敗。
  fail_if_header_not_matches:
    [ - <http_header_match_spec>, ... ]

# HTTP探針的TLS協(xié)議的配置。
  tls_config:
    [ <tls_config> ]

# 目標(biāo)的HTTP基本身份驗證憑據(jù)。
  basic_auth:
    [ <string> ]
    [ <secret> ]

# 目標(biāo)的承載令牌。
  [ bearer_token: <secret> ]

# 目標(biāo)的承載令牌文件
  [ bearer_token_file: <filename> ]

# 用于連接到目標(biāo)的HTTP代理服務(wù)器。
  [ proxy_url: <string> ]

# HTTP探針的IP協(xié)議(ip4,ip6)
  [ preferred_ip_protocol: <string> | default = ip6 ]
  [ ip_protocol_fallback: <boolean> | default = true ]

# 探針中使用的HTTP請求的主體。
  body: [ <string> ]

###################################################################
<http_header_match_spec>
    header: <string>,
    regexp: <regex>,
    [ allow_missing: <boolean> | default = false ]

分別介紹幾種應(yīng)用場景
ping 檢測

可以通過 ping(icmp)檢測服務(wù)器的存活,在 prometheus 配置文件中配置使用 ping module:

  icmp:
    prober: icmp

與 prometheus 集成

  - job_name: \\\'blackbox-ping\\\'
    metrics_path: /probe
    params:
      modelus: [icmp]
    static_configs:
    - targets:
      - 223.5.5.5
      lables:
        instance: aliyun
    - targets:
      - 47.92.229.67
      lables:
        instance: zsf
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.111.65:9115

HTTP

blackbox config file

modules:
  http_2xx:
    prober: http
    http:
      method: GET
  http_post_2xx:
    prober: http
    http:
      method: POST

配置好之后運行如指令

#DNS解析時間,單位 s
probe_dns_lookup_time_seconds 0.039431355
#探測從開始到結(jié)束的時間,單位 s,請求這個頁面響應(yīng)時間
probe_duration_seconds 0.651619323

probe_failed_due_to_regex 0

#HTTP 內(nèi)容響應(yīng)的長度
probe_http_content_length -1
#按照階段統(tǒng)計每階段的時間
probe_http_duration_seconds{phase=connect} 0.050388884   #連接時間
probe_http_duration_seconds{phase=processing} 0.45868667 #處理請求的時間
probe_http_duration_seconds{phase=resolve} 0.040037612  #響應(yīng)時間
probe_http_duration_seconds{phase=tls} 0.145433254    #校驗證書的時間
probe_http_duration_seconds{phase=transfer} 0.000566269 
#重定向的次數(shù)
probe_http_redirects 1
#ssl 指示是否將 SSL 用于最終重定向
probe_http_ssl 1
#返回的狀態(tài)碼
probe_http_status_code 200
#未壓縮的響應(yīng)主體長度
probe_http_uncompressed_body_length 40339
#http 協(xié)議的版本
probe_http_version 1.1
#使用的 ip 協(xié)議的版本號
probe_ip_protocol 4

probe_ssl_earliest_cert_expiry 1.59732e 09
#是否探測成功
probe_success 1
#TLS 的版本號
probe_tls_version_info{version=TLS 1.2} 1

與 prometheus 集成,采用prometheus 的 Relabelinng 能力(服務(wù)發(fā)現(xiàn))

  - job_name: \\\'blackbox-http\\\'
    metrics_path: /probe
    params:
      modelue: [http_2xx]
    static_configs:
    - targets:
      - http://www.zhangshoufu.com
      - http://www.xuliangwei.com
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.111.65:9115  #blackbox-exporter 所在的機器和端口

這里針對每一個探針服務(wù)(如http_2xx)定義一個采集任務(wù),并且直接將任務(wù)的采集目標(biāo)定義為我們需要探測的站點。在采集樣本數(shù)據(jù)之前通過relabel_configs對采集任務(wù)進行動態(tài)設(shè)置。

1, 根據(jù) Target 實例的地址,寫入__param_target 標(biāo)簽中,__param_&lt;name&gt;形式的標(biāo)簽表示,在采集任務(wù)時會在請求目標(biāo)地址中添加<name>參數(shù),等同于 params 的設(shè)置
2, 獲取__param_target的值,并覆寫到instance標(biāo)簽中;
3, 覆寫Target實例的__address__標(biāo)簽值為BlockBox Exporter實例的訪問地址。

自定義 HTTP 請求
HTTP服務(wù)通常會以不同的形式對外展現(xiàn),有些可能就是一些簡單的網(wǎng)頁,而有些則可能是一些基于REST的API服務(wù)。 對于不同類型的HTTP的探測需要管理員能夠?qū)TTP探針的行為進行更多的自定義設(shè)置,包括:HTTP請求方法、HTTP頭信息、請求參數(shù)等。對于某些啟用了安全認(rèn)證的服務(wù)還需要能夠?qū)TTP探測設(shè)置相應(yīng)的Auth支持。對于HTTPS類型的服務(wù)還需要能夠?qū)ψC書進行自定義設(shè)置。
如下所示,這里通過method定義了探測時使用的請求方法,對于一些需要請求參數(shù)的服務(wù),還可以通過headers定義相關(guān)的請求頭信息,使用body定義請求內(nèi)容:

http_post_2xx:
    prober: http
    timeout: 5s
    http:
      method: POST
      headers:
        Content-Type: application/json
      body: \\\'{}\\\'

如果HTTP服務(wù)啟用了安全認(rèn)證,Blockbox Exporter內(nèi)置了對basic_auth的支持,可以直接設(shè)置相關(guān)的認(rèn)證信息即可:

http_basic_auth_example:
    prober: http
    timeout: 5s
    http:
      method: POST
      headers:
        Host: login.example.com
      basic_auth:
        
        

對于使用了Bear Token的服務(wù)也可以通過bearer_token配置項直接指定令牌字符串,或者通過bearer_token_file指定令牌文件。
對于一些啟用了HTTPS的服務(wù),但是需要自定義證書的服務(wù),可以通過tls_config指定相關(guān)的證書信息:

http_custom_ca_example:
    prober: http
    http:
      method: GET
      tls_config:
        ca_file: /certs/my_cert.crt

自定義探針行為
在默認(rèn)情況下HTTP探針只會對HTTP返回狀態(tài)碼進行校驗,如果狀態(tài)碼為2XX(200 <= StatusCode < 300)則表示探測成功,并且探針返回的指標(biāo)probe_success值為1。
如果用戶需要指定HTTP返回狀態(tài)碼,或者對HTTP版本有特殊要求,如下所示,可以使用valid_http_versions和valid_status_codes進行定義:

http_2xx_example:
    prober: http
    timeout: 5s
    http:
      valid_http_versions: [HTTP/1.1, HTTP/2]
      valid_status_codes: [200,301,302]

默認(rèn)情況下,Blockbox返回的樣本數(shù)據(jù)中也會包含指標(biāo)probe_http_ssl,用于表明當(dāng)前探針是否使用了SSL

# HELP probe_http_ssl Indicates if SSL was used for the final redirect
# TYPE probe_http_ssl gauge
probe_http_ssl 0

而如果用戶對于HTTP服務(wù)是否啟用SSL有強制的標(biāo)準(zhǔn)。則可以使用fail_if_ssl和fail_if_not_ssl進行配置。fail_if_ssl為true時,表示如果站點啟用了SSL則探針失敗,反之成功。fail_if_not_ssl剛好相反。

  http_2xx_example:
    prober: http
    timeout: 5s
    http:
      valid_status_codes: []
      method: GET
      no_follow_redirects: false
      fail_if_ssl: false
      fail_if_not_ssl: false

除了基于HTTP狀態(tài)碼,HTTP協(xié)議版本以及是否啟用SSL作為控制探針探測行為成功與否的標(biāo)準(zhǔn)以外,還可以匹配HTTP服務(wù)的響應(yīng)內(nèi)容。使用fail_if_matches_regexp和fail_if_not_matches_regexp用戶可以定義一組正則表達式,用于驗證HTTP返回內(nèi)容是否符合或者不符合正則表達式的內(nèi)容。

  http_2xx_example:
    prober: http
    timeout: 5s
    http:
      method: GET
      fail_if_matches_regexp:
        - Could not connect to database
      fail_if_not_matches_regexp:
        - Download the latest version here

具體可以看我上面的配置詳解

在 grafana 里面展示

在 grafana web 界面里導(dǎo)入9965 這個 dashboards,記得要安裝圖餅
手工安裝

wget  https://grafana.com/api/plugins/grafana-piechart-panel/versions/latest/download -O ./grafana-piechart-panel.zip
下載到插件目錄,然后解壓成目錄名稱為grafana-piechart-panel 目錄,然后重新啟動 grafana

prometheus 告警規(guī)則

groups:
- name: example
  rules:
  - alert: curlHttpStatus
    expr:  probe_http_status_code{job=blackbox-http}>=400 and probe_success{job=blackbox-http}==0
    #for: 1m
    labels:
      docker: number
    annotations:
      summary: \\\'業(yè)務(wù)報警: 網(wǎng)站不可訪問\\\'
      description: \\\'{{$labels.instance}} 不可訪問,請及時查看,當(dāng)前狀態(tài)碼為{{$value}}\\\'

參考:

blackbox-configs --> https://github.com/prometheus/blackbox_exporter/blob/master/CONFIGURATION.md
prometheus-configs --> https://prometheus.io/docs/prometheus/latest/configuration/configuration/
https://yunlzheng.gitbook.io/prometheus-book/part-ii-prometheus-jin-jie/exporter/commonly-eporter-usage/install_blackbox_exporter
https://www.li-rui.top/2018/11/23/monitor/blackbox_exporter使用/

更多關(guān)于云服務(wù)器域名注冊,虛擬主機的問題,請訪問三五互聯(lián)官網(wǎng):m.shinetop.cn

贊(0)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡(luò)內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。郵箱:3140448839@qq.com。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明出處:三五互聯(lián)知識庫 » 網(wǎng)絡(luò)探測:Blackbox Exporter

登錄

找回密碼

注冊

主站蜘蛛池模板: 99精品偷自拍| 余姚市| 一级做a爰片在线播放| 精品久久久久久国产| 亚洲精品免费一二三区| 国产精品久久久久鬼色| 欧洲亚洲成av人片天堂网| 亚洲成av一区二区三区| 亚洲高清国产自产拍av| 在线 欧美 中文 亚洲 精品| 欧美黑吊大战白妞| 蜜桃无码一区二区三区| 成人午夜在线观看日韩| 久久精品国产亚洲av天海翼| 男女性杂交内射女bbwxz| 国产精品青青青高清在线| 中文字幕一区二区网站| 日韩不卡在线观看视频不卡| 蜜臀av久久国产午夜| 亚洲精品久久久久国产 | 成人免费ā片在线观看| 亚洲sm另类一区二区三区| 国产精品毛片久久久久久久| 人妻无码∧V一区二区| 视频一区视频二区在线视频| 亚洲综合伊人久久大杳蕉| 色猫咪av在线观看| 精品无码国产不卡在线观看| 人人入人人爱| 国产午夜亚洲精品国产成人| av色蜜桃一区二区三区| av大片在线无码免费| 亚洲精品久荜中文字幕| 日韩av片无码一区二区不卡| 国产精品多p对白交换绿帽| 国产又爽又黄又刺激的视频| 在线涩涩免费观看国产精品 | 国产成人a在线观看视频| 人妻夜夜爽天天爽三区麻豆av | 国产精品亚洲аv无码播放| 爱色精品视频一区二区|