Prometheus07 blackbox

blackbox的部署和使用

内省对于收集关于系统的数据是非常宝贵的,但是有时我们需要从系统用户的角度进行度量。在这种情况下,探测是模拟用户交互的一个很好的选择。由于探测是在外部进行的,并且不了解系统内部的工作情况,因此将其归类为黑河监视

1.黑盒导出器

blackbox_exporters是普罗米修斯生态系统中所有现有出口商中最奇特的一家。它的使用模式是巧妙的,通常,新来的人会感到困惑。我们将深入研究这个出口商,希望尽可能直接地利用它。

blackbox_exporter服务暴露两个主要的端点:

  • /metrics:暴露自己的度量
  • /probe:正式查询端点启用了blackbox探测,以Prometheus显示格式返回它们的结果。

除了前面的两个端点之外,/ 服务还提供了有价值的信息,包括执行探测的日志。

blackbox导出器支持通过各种各样的本地协议探测端点,比如TCP、ICMP、DNS、HTTP(版本1和2),以及大多数探测上的TLS。此外,他还支持脚本化的基于文本的协议,如IRC、IMAP或SMTP,通过TCP连接并撇脂应该发送哪些消息和需要哪些响应;即使是普通的HTTP也可以编写脚本,但是由于HTTP探测是如此常见的用例,它已经内置了。

尽管如此,这个出口商并不能满足所有blackbox类型的监视需求。对于这些情况,可能需要编写自己的出口商。例如,你不能使用blackbox_exports来从头到尾地测试一个Kafka主题,所以你可能需要寻找一个能够生成一条消息给Kafka并将其消费掉的出口商:

2.blackbox_exporter部署

useradd --system blackbox_exporter
tar zxf blackbox_exporter-0.19.0.linux-amd64.tar.gz -C /usr/bin --strip-components=1 --wildcards */blackbox_exporter
install -d -m 0755 -o blackbox_exporter -g blackbox_exporter /etc/blackbox_exporter
vim blackbox.yml
modules:
  http_2xx:
    prober: http
    timeout: 30s
    http:
      valid_http_vcaersions: ["HTTP/1.1","HTTP/2"]
      valid_status_codes: []
      method: GET
      headers:
        Content-Type: application/json
      no_follow_redirects: false
      fail_if_ssl: false
      fail_if_not_ssl: false
      tls_config:
        insecure_skip_verify: false
      preferred_ip_protocol: ip4
      ip_protocol_fallback: false

  http_simple_get:
    prober: http
    timeout: 5s
    http:
      valid_http_versions: ["HTTP/1.1", "HTTP/2"]
      valid_status_codes: []  # Defaults to 2xx
      method: GET
      no_follow_redirects: false
      tls_config:
        insecure_skip_verify: false
      preferred_ip_protocol: "ip4" # defaults to "ip6"
      ip_protocol_fallback: false  # no fallback to "ip6"

install -m 0644 -D blackbox.yml /etc/blackbox_exporter/blackbox.yml

vim blackbox_exporter.service
[Unit]
Description=blackbox_exporter
After=network.target

[Service]
User=blackbox_exporter
Group=blackbox_exporter
ExecStart=/usr/bin/blackbox_exporter \
    --config.file=/etc/blackbox_exporter/blackbox.yml \
    --web.listen-address=:9115
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target


install -m 0644 blackbox_exporter.service /etc/systemd/system/

# ICMP probe requires access to raw sockets 
setcap cap_net_raw+ep /usr/bin/blackbox_exporter

vim prometheus.yml
- job_name: 'blackbox-http'
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
    - targets:
      - https://baidu.com
      - http://192.168.1.102:80
      labels:
          group: wind-app
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.1.121:9115

systemctl daemon-reload
systemctl enable blackbox_exporter
systemctl start blackbox_exporter
systemctl status blackbox_exporter

promtool check config prometheus.yml
systemctl restart prometheus

3.创建Prometheus作业

在下面的单元文件片段中,我们可以看到blackbox_exporter服务所需的所有参数,包括配置文件的路径:

ExecStart=/usr/bin/black_exporter --config.file=/etc/blackbox_exporter/blackbox.yml --web.listen-address=:9115

我们为示例定制的配置可以在以下代码片段中找到:

modules:
  http_2xx:
    prober: http
    http:
      preferred_ip_protocol: ip4

Prometheus的实例中,我们添加了以下工作:

# 探测网站
- job-name: 'blackbox-http'
  metrics_path: /probe
  params:
    module: [http_2xx]
  static_configs:
    - targets:
      - http://example.com
      - https://example.com:443
  relabel_configs:
    - source_labels: [__address__]
      target_label: __param_target
    - source_labels: [__param_target]
      target_label: instance
    - target_label:  __address__
      replacement: 192.168.1.121:9115

# 探测icmp
- job-name: 'blackbox-icmp'
  metrics_path: /probe
  params:
    module: [icmp]
  static_configs:
    - targets:
      - http://example.com
      - https://example.com:443
  relabel_configs:
    - source_labels: [__address__]
      target_label: __param_target
    - source_labels: [__param_target]
      target_label: instance
    - target_label:  __address__
      replacement: 192.168.1.121:9115

验证结果如下:

如前所述,/targets页面不会告诉您探测是否成功。这需要在表达式浏览器中通过查询probe_success 度量来验证:

可以从blackbox_importer收集到的一些有趣的度量(关于导出器本身和探测)是:

blackbox_exporter_config_last_reload_successful,这将暴露出口商的配置文件是否在SIGHUP后成功重新加载

原文地址:https://www.cnblogs.com/zhangchaocoming/p/15605024.html