Prometheus监控 Blackbox_exporter黑盒监测

参考
https://blog.51cto.com/shoufu/2469397
https://www.lmonkey.com/t/8ZBW0AxEP

一、Blackbox_exporter应用场景

blackbox_exporter是Prometheus 官方提供的 exporter 之一,可以提供 http、dns、tcp、icmp 的监控数据采集。

  • HTTP 测试
    定义 Request Header 信息
    判断 Http status / Http Respones Header / Http Body 内容
  • TCP 测试
    业务组件端口状态监听
    应用层协议定义与监听
  • ICMP 测试
    主机探活机制
  • POST 测试
    接口联通性
    SSL 证书过期时间

二、prometheus安装

https://prometheus.io/download/
1、下载安装包然后解压

cd /usr/local
wget https://github.com/prometheus/prometheus/releases/download/v2.8.1/prometheus-2.8.1.linux-amd64.tar.gz
tar -zxvf prometheus-2.8.1.linux-amd64.tar.gz
mv prometheus-2.8.1.linux-amd64/ prometheus
cd prometheus/
./prometheus --version

useradd  -s /sbin/nologin -M prometheus 
chown -R prometheus:prometheus /usr/local/prometheus
yum -y install ntp
ntpdate ntp1.aliyun.com

2、创建Systemd服务启动prometheus

vim /etc/systemd/system/prometheus.service
在service文件里面,定义了启动的命令,可以定义数据存储路径,否则默认会在prometheus二进制的目录的data下。

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl start prometheus
systemctl status prometheus
systemctl enable prometheus

3、打开prometheus的web页面
http://10.0.0.102:9090/graph
在这里插入图片描述

三、Blackbox_exporter 安装

https://github.com/prometheus/blackbox_exporter/releases
1、安装解压

wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.16.0/blackbox_exporter-0.16.0.linux-amd64.tar.gz
tar xf blackbox_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local/
ln -s /usr/local/blackbox_exporter-0.16.0.linux-amd64/ /usr/local/blackbox_exporter

2、使用systemd进行管理blackbox_exporter服务
vim /etc/systemd/system/blackbox_exporter.service

[Unit]
Description=blackbox_exporter
After=network.target

[Service]
Restart=on-failure
ExecStart=/usr/local/blackbox_exporter/blackbox_exporter --web.listen-address=0.0.0.0:9115 --config.file=/usr/local/blackbox_exporter/blackbox.yml

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start blackbox_exporter.service 
systemctl enable blackbox_exporter.service 

四、blackbox exporter 配置文件解读

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

1、默认配置

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 主要包含以下配置内容:探针类型(prober),验证访问超时时间(timeout),以及当前探针的具体配置项

# 探针类型: http https tcp dns icmp
prober: <prober_string>   #必选

# 超时时间:
[timeout: <duration>] #默认单位秒

# 探针的详细配置,最多只能配置其中一个
[ http: <http_probe> ]
[ tcp: <tcp_probe> ]
[ dns: <dns_probe> ]
[ icmp: <icmp_probe> ]

2、<http_probe>可配置参数

# 此探针接受的状态代码。 默认为2xx。
  [ valid_status_codes: <int>, ... | default = 2xx ]

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

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

# 为探针设置的HTTP标头。
  headers:
    [ <string>: <string> ... ]

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

# 如果存在SSL,则探测失败。
  [ fail_if_ssl: <boolean> | default = false ]

# 如果不存在SSL,则探测失败。
  [ fail_if_not_ssl: <boolean> | default = false ]

# 如果响应主体与正则表达式匹配,则探测失败。
  fail_if_body_matches_regexp:
    [ - <regex>, ... ]

# 如果响应主体与正则表达式不匹配,则探测失败。
  fail_if_body_not_matches_regexp:
    [ - <regex>, ... ]

# 如果响应头与正则表达式匹配,则探测失败。 对于具有多个值的标头,如果*至少一个*匹配,则失败。
  fail_if_header_matches:
    [ - <http_header_match_spec>, ... ]

# 如果响应头与正则表达式不匹配,则探测失败。 对于具有多个值的标头,如果* none *不匹配,则失败。
  fail_if_header_not_matches:
    [ - <http_header_match_spec>, ... ]

# HTTP探针的TLS协议的配置。
  tls_config:
    [ <tls_config> ]

# 目标的HTTP基本身份验证凭据。
  basic_auth:
    [ username: <string> ]
    [ password: <secret> ]

# 目标的承载令牌。
  [ bearer_token: <secret> ]

# 目标的承载令牌文件
  [ bearer_token_file: <filename> ]

# 用于连接到目标的HTTP代理服务器。
  [ proxy_url: <string> ]

# HTTP探针的IP协议(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 ]

五、分别介绍几种应用场景

1、HTTP

(1) blackbox 配置

/usr/local/blackbox_exporter/blackbox.yml

modules:
  http_2xx:
    prober: http
    http:
      method: GET
  http_post_2xx:
    prober: http
    http:
      method: POST
(2) prometheus配置

/usr/local/prometheus/prometheus.yml

  - job_name: 'blackbox_http'
    scrape_interval: 30s
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets:
         - https://www.baidu.com
         - https://www.lkcoffee.com
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        #blackbox exporter 所在节点
        replacement: 10.0.0.103:9115

这里针对每一个探针服务(如http_2xx)定义一个采集任务,并且直接将任务的采集目标定义为我们需要探测的站点。在采集样本数据之前通过relabel_configs对采集任务进行动态设置。

1, 根据 Target 实例的地址,写入__param_target 标签中,__param_<name>形式的标签表示,在采集任务时会在请求目标地址中添加<name>参数,等同于 params 的设置
2, 获取__param_target的值,并覆写到instance标签中;
3, 覆写Target实例的__address__标签值为BlockBox Exporter实例的访问地址。
(3)访问prometheus页面

http://10.0.0.102:9090/targets
在这里插入图片描述

#查询数据
{job="blackbox_http"}

在这里插入图片描述

(4)自定义 HTTP 请求

HTTP服务通常会以不同的形式对外展现,有些可能就是一些简单的网页,而有些则可能是一些基于REST的API服务。 对于不同类型的HTTP的探测需要管理员能够对HTTP探针的行为进行更多的自定义设置,包括:HTTP请求方法、HTTP头信息、请求参数等。对于某些启用了安全认证的服务还需要能够对HTTP探测设置相应的Auth支持。对于HTTPS类型的服务还需要能够对证书进行自定义设置。

如下所示,这里通过method定义了探测时使用的请求方法,对于一些需要请求参数的服务,还可以通过headers定义相关的请求头信息,使用body定义请求内容:

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

如果HTTP服务启用了安全认证,Blockbox Exporter内置了对basic_auth的支持,可以直接设置相关的认证信息即可:

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

对于使用了Bear Token的服务也可以通过bearer_token配置项直接指定令牌字符串,或者通过bearer_token_file指定令牌文件。
对于一些启用了HTTPS的服务,但是需要自定义证书的服务,可以通过tls_config指定相关的证书信息:

http_custom_ca_example:
    prober: http
    http:
      method: GET
      tls_config:
        ca_file: "/certs/my_cert.crt"
(5)自定义探针行为

在默认情况下HTTP探针只会对HTTP返回状态码进行校验,如果状态码为2XX(200 <= StatusCode < 300)则表示探测成功,并且探针返回的指标probe_success值为1。

  • 如果用户需要指定HTTP返回状态码,或者对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]
  • 默认情况下,Blockbox返回的样本数据中也会包含指标probe_http_ssl,用于表明当前探针是否使用了SSL:
# HELP probe_http_ssl Indicates if SSL was used for the final redirect
# TYPE probe_http_ssl gauge
probe_http_ssl 0
  • 而如果用户对于HTTP服务是否启用SSL有强制的标准。则可以使用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状态码,HTTP协议版本以及是否启用SSL作为控制探针探测行为成功与否的标准以外,还可以匹配HTTP服务的响应内容。使用fail_if_matches_regexp和fail_if_not_matches_regexp用户可以定义一组正则表达式,用于验证HTTP返回内容是否符合或者不符合正则表达式的内容。
  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"

2、ping 检测

可以通过 ping(icmp)检测服务器的存活,在 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
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值