云原生监控系统Prometheus——黑盒监控(blackbox_exporter)

黑盒监控(blackbox_exporter)

  之前几个 exporter 的使用,我们可以称为 "白盒监控",即需要把对应的 expoter 程序安装到被监控的目标主机上,从而实现对主机各种资源及其状态的数据采集工作。但是由于某些清下操作技术或其他原因,不是所有的 expoter 都能部署到被监控的主机环境中,最典型的例子是监控全国网络质量的稳定性,通常的方法是使用 ping 操作,对选取的节点进行 ICMP 测试,此时不可能在他人的环境中部署相关的 expoter 程序。

  通过 HTTP、HTTPS、DNS、TCP、ICMP、GRPC 等方式对网络进行探测监控,还可以探测 SSL 证书过期时间。

  在安装之前,首先在官方下载页面 https://github.com/prometheus/blackbox_exporter/releases 下找到 blackbox_exporter,下载最新版本中特定平台的二进制文件。如下图所示:

一、部署 blackbox_exporter

  这里以官方 docker 安装方式为例(https://github.com/prometheus/blackbox_exporter):

docker run --rm -d -p 9115:9115 --name blackbox_exporter -v `pwd`:/config prom/blackbox-exporter:master --config.file=/config/blackbox.yml

  Note: You may want to enable ipv6 in your docker configuration

    • Edit /etc/docker/daemon.json, set the ipv6 key to true and the fixed-cidr-v6 key to your IPv6 subnet. In this example we are setting it to 2001:db8:1::/64.
      {
        "ipv6": true,
        "fixed-cidr-v6": "2001:db8:1::/64"
      }
    • Reload the Docker configuration file.
      systemctl reload docker

  You can now create networks with the --ipv6 flag and assign containers IPv6 addresses using the --ip6 flag.

二、Checking the results

  访问 http://localhost:9115/probe?target=google.com&module=http_2xx 将返回针对 google.com 的 HTTP 探测的指标。该 probe_success 指标指示探测是否成功。添加 debug=true 参数将返回该探测器的调试信息。 

三、TLS and basic authentication

  Blackbox Exporter 支持 TLS 和基本身份验证。这可以更好地控制各种 HTTP 端点。

  要使用 TLS 和/或基本身份验证,您需要使用--web.config.file参数传递配置文件。文件的格式 在 exporter-toolkit 存储库中描述。

  请注意,TLS 和基本身份验证设置会影响所有 HTTP 端点:用于抓取的 /metrics、用于探测的 /probe 和 Web UI。

四、配置 blackbox_exporter

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
  grpc:
    prober: grpc
    grpc:
      tls: true
      preferred_ip_protocol: "ip4"
  grpc_plain:
    prober: grpc
    grpc:
      tls: false
      service: "service1"
  ssh_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: "^SSH-2.0-"
      - send: "SSH-2.0-blackbox-ssh-check"
  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
  icmp_ttl5:
    prober: icmp
    timeout: 5s
    icmp:
      ttl: 5

五、与 Prometheus 集成

  • 5.1、ping 测试
    - job_name: 'ping_all'
        scrape_interval: 1m
        metrics_path: /probe
        params:
          module: [ping]
        static_configs:
         - targets:
            - 192.168.1.2
           labels:
             instance: node2
         - targets:
            - 192.168.1.3
           labels:
             instance: node3
        relabel_configs:
          - source_labels: [__address__]
            target_label: __param_target
          - target_label: __address__
            replacement: 127.0.0.1:9115
    

    下面是一个http://127.0.0.1:9115/probe?module=ping&target=192.168.1.2返回的是192.168.1.2这个target的metrics。

    #DNS解析时间,单位 s
    probe_dns_lookup_time_seconds 0.039431355
    #探测从开始到结束的时间,单位 s,请求这个页面响应时间
    probe_duration_seconds 0.651619323
    
    probe_failed_due_to_regex 0
    
    #HTTP 内容响应的长度
    probe_http_content_length -1
    #按照阶段统计每阶段的时间
    probe_http_duration_seconds{phase="connect"} 0.050388884   #连接时间
    probe_http_duration_seconds{phase="processing"} 0.45868667 #处理请求的时间
    probe_http_duration_seconds{phase="resolve"} 0.040037612  #响应时间
    probe_http_duration_seconds{phase="tls"} 0.145433254    #校验证书的时间
    probe_http_duration_seconds{phase="transfer"} 0.000566269
    #重定向的次数
    probe_http_redirects 1
    #ssl 指示是否将 SSL 用于最终重定向
    probe_http_ssl 1
    #返回的状态码
    probe_http_status_code 200
    #未压缩的响应主体长度
    probe_http_uncompressed_body_length 40339
    #http 协议的版本
    probe_http_version 1.1
    #使用的 ip 协议的版本号
    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  
  • 5.2、http 监测
    - job_name: 'http_get_all'  # blackbox_export module
        scrape_interval: 30s
        metrics_path: /probe
        params:
          module: [http_2xx]
        static_configs:
          - targets:
            - https://frognew.com
        relabel_configs:
          - source_labels: [__address__]
            target_label: __param_target
          - source_labels: [__param_target]
            target_label: instance
          - target_label: __address__
            replacement: 127.0.0.1:9115
    

    http检测除了可以探测http服务的存活外,还可以根据指标probe_ssl_earliest_cert_expiry进行ssl证书有效期预警。

    # 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.581897599e+09
  • 5.3、监控主机存活状态
    - job_name: node_status
        metrics_path: /probe
        params:
          module: [icmp]
        static_configs:
          - targets: ['10.165.94.31']
            labels:
              instance: node_status
              group: 'node'
        relabel_configs:
          - source_labels: [__address__]
            target_label: __param_target
          - target_label: __address__
            replacement: 172.19.155.133:9115
    10.165.94.31是被监控端ip,172.19.155.133是Blackbox_exporter
  • 5.4、监控主机端口存活状态
    - job_name: 'prometheus_port_status'
        metrics_path: /probe
        params:
          module: [tcp_connect]
        static_configs:
          - targets: ['172.19.155.133:8765']
            labels:
              instance: 'port_status'
              group: 'tcp'
        relabel_configs:
          - source_labels: [__address__]
            target_label: __param_target
          - source_labels: [__param_target]
            target_label: instance
          - target_label: __address__
            replacement: 172.19.155.133:9115
  • 5.5、监控网站状态
    - job_name: web_status
        metrics_path: /probe
        params:
          module: [http_2xx]
        static_configs:
          - targets: ['http://www.baidu.com']
            labels:
              instance: user_status
              group: 'web'
        relabel_configs:
          - source_labels: [__address__]
            target_label: __param_target
          - target_label: __address__
            replacement: 172.19.155.133:9115

六、告警

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: '业务报警: 网站不可访问'
      description: '{{$labels.instance}} 不可访问,请及时查看,当前状态码为{{$value}}'

七、Grafana

  grafana模板号:9965。下载地址 https://grafana.com/grafana/plugins/grafana-piechart-panel
  安装插件,重启grafana生效。

grafana-cli plugins install grafana-piechart-panel
posted @ 2022-07-15 23:29  左扬  阅读(4306)  评论(0编辑  收藏  举报
levels of contents