网络探测:Blackbox Exporter
系統(tǒng)運(yùn)維
網(wǎng)絡(luò)探測:Blackbox Exporter
什么是 blackbox exporter?
Blackbox Exporter 是 Prometheus 社區(qū)提供的 官方黑盒監(jiān)控解決方案,其允許用戶通過: http\\HTTPS\\dns\\TCP\\ICMP的方式對網(wǎng)絡(luò)進(jìn)行探測.
以 docker 方式運(yùn)行 blackbox exporter
拉取鏡像
docker pull prom/blackbox-exporter
查看 dockerfile(這是一個(gè)好習(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]
運(yùn)行 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
運(yùn)行 blackbox exporter 時(shí),需要用戶提供探針的配置信息,這些配置信息可能是一些自定義的 HTTP 頭信息,也可能是探測時(shí)需要的一些 TSL(秘鑰證書) 配置,也可能是探針本身的驗(yàn)證行為.在 blackbox exporter 每一個(gè)探針配置稱為一個(gè) module,并且以 YAML 配置文件的形式提供給 blackbox exporter.每一個(gè) module 主要包含以下配置內(nèi)容,探針類型(prober),驗(yàn)證訪問超時(shí)時(shí)間(timeout),以及當(dāng)前探針的具體配置項(xiàng):
# 探針類型: http https tcp dns icmp
prober: <prober_string> #必選
# 超時(shí)時(shí)間:
[timeout: <duration>] #默認(rèn)單位秒
# 探針的詳細(xì)配置,最多只能配置其中一個(gè)
[ 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)主體與正則表達(dá)式匹配,則探測失敗。
fail_if_body_matches_regexp:
[ - <regex>, ... ]
# 如果響應(yīng)主體與正則表達(dá)式不匹配,則探測失敗。
fail_if_body_not_matches_regexp:
[ - <regex>, ... ]
# 如果響應(yīng)頭與正則表達(dá)式匹配,則探測失敗。 對于具有多個(gè)值的標(biāo)頭,如果*至少一個(gè)*匹配,則失敗。
fail_if_header_matches:
[ - <http_header_match_spec>, ... ]
# 如果響應(yīng)頭與正則表達(dá)式不匹配,則探測失敗。 對于具有多個(gè)值的標(biāo)頭,如果* none *不匹配,則失敗。
fail_if_header_not_matches:
[ - <http_header_match_spec>, ... ]
# HTTP探針的TLS協(xié)議的配置。
tls_config:
[ <tls_config> ]
# 目標(biāo)的HTTP基本身份驗(yàn)證憑據(jù)。
basic_auth:
[ username: <string> ]
[ password: <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
配置好之后運(yùn)行如指令
#DNS解析時(shí)間,單位 s
probe_dns_lookup_time_seconds 0.039431355
#探測從開始到結(jié)束的時(shí)間,單位 s,請求這個(gè)頁面響應(yīng)時(shí)間
probe_duration_seconds 0.651619323
probe_failed_due_to_regex 0
#HTTP 內(nèi)容響應(yīng)的長度
probe_http_content_length -1
#按照階段統(tǒng)計(jì)每階段的時(shí)間
probe_http_duration_seconds{phase=connect} 0.050388884 #連接時(shí)間
probe_http_duration_seconds{phase=processing} 0.45868667 #處理請求的時(shí)間
probe_http_duration_seconds{phase=resolve} 0.040037612 #響應(yīng)時(shí)間
probe_http_duration_seconds{phase=tls} 0.145433254 #校驗(yàn)證書的時(shí)間
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 所在的機(jī)器和端口
這里針對每一個(gè)探針服務(wù)(如http_2xx)定義一個(gè)采集任務(wù),并且直接將任務(wù)的采集目標(biāo)定義為我們需要探測的站點(diǎn)。在采集樣本數(shù)據(jù)之前通過relabel_configs對采集任務(wù)進(jìn)行動(dòng)態(tài)設(shè)置。
1, 根據(jù) Target 實(shí)例的地址,寫入
__param_target標(biāo)簽中,__param_<name>形式的標(biāo)簽表示,在采集任務(wù)時(shí)會(huì)在請求目標(biāo)地址中添加<name>參數(shù),等同于 params 的設(shè)置
2, 獲取__param_target的值,并覆寫到instance標(biāo)簽中;
3, 覆寫Target實(shí)例的__address__標(biāo)簽值為BlockBox Exporter實(shí)例的訪問地址。
自定義 HTTP 請求
HTTP服務(wù)通常會(huì)以不同的形式對外展現(xiàn),有些可能就是一些簡單的網(wǎng)頁,而有些則可能是一些基于REST的API服務(wù)。 對于不同類型的HTTP的探測需要管理員能夠?qū)TTP探針的行為進(jìn)行更多的自定義設(shè)置,包括:HTTP請求方法、HTTP頭信息、請求參數(shù)等。對于某些啟用了安全認(rèn)證的服務(wù)還需要能夠?qū)TTP探測設(shè)置相應(yīng)的Auth支持。對于HTTPS類型的服務(wù)還需要能夠?qū)ψC書進(jìn)行自定義設(shè)置。
如下所示,這里通過method定義了探測時(shí)使用的請求方法,對于一些需要請求參數(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:
username: username
password: mysecret
對于使用了Bear Token的服務(wù)也可以通過bearer_token配置項(xiàng)直接指定令牌字符串,或者通過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探針只會(huì)對HTTP返回狀態(tài)碼進(jìn)行校驗(yàn),如果狀態(tài)碼為2XX(200 <= StatusCode < 300)則表示探測成功,并且探針返回的指標(biāo)probe_success值為1。
如果用戶需要指定HTTP返回狀態(tài)碼,或者對HTTP版本有特殊要求,如下所示,可以使用valid_http_versions和valid_status_codes進(jìn)行定義:
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ù)中也會(huì)包含指標(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有強(qiáng)制的標(biāo)準(zhǔn)。則可以使用fail_if_ssl和fail_if_not_ssl進(jìn)行配置。fail_if_ssl為true時(shí),表示如果站點(diǎn)啟用了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用戶可以定義一組正則表達(dá)式,用于驗(yàn)證HTTP返回內(nèi)容是否符合或者不符合正則表達(dá)式的內(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 這個(gè) dashboards,記得要安裝圖餅
手工安裝
wget https://grafana.com/api/plugins/grafana-piechart-panel/versions/latest/download -O ./grafana-piechart-panel.zip
下載到插件目錄,然后解壓成目錄名稱為grafana-piechart-panel 目錄,然后重新啟動(dòng) 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ù)報(bào)警: 網(wǎng)站不可訪問\'
description: \'{{$labels.instance}} 不可訪問,請及時(shí)查看,當(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%E4%BD%BF%E7%94%A8/
總結(jié)
以上是生活随笔為你收集整理的网络探测:Blackbox Exporter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Centos7下面如何配置静态IP
- 下一篇: MOCTF-Web-Flag在哪?