美文网首页
使用blackbox监控Web应用

使用blackbox监控Web应用

作者: aneirin | 来源:发表于2022-06-11 14:56 被阅读0次

在promtheus+grafana监控栈,blackbox是一款出镜率比较高的exporter。blackbox支持使用HTTP、HTTPS, DNS, TCP, ICMP和gRPC对目标进行请求,根据响应结果来侦测目标的健康状况。实际使用可以选择不同的探测器,常用的blackbox探测器有http、tcp、icmp和dns。不同的侦测器,有不同的配置参数。

本文以常用的http探测器为例,探测的URL为百度主页:https://www.baidu.com

1. blackbox
使用docker-compose来管理blackbox容器的运行。创建一个目录blackbox用来存放相关文件,

blackbox目录内容.png
docker-compose.yaml
version: '3.3'
services:
  blackbox_exporter:
    image: prom/blackbox-exporter:v0.19.0
    ports:
      - "9115:9115"
    restart: always
    volumes:
      - "./config:/config"
    command: "--config.file=/config/blackbox.yaml"

config/blackbox.yaml

[root@a blackbox]# cat config/blackbox.yaml 
modules:
  http_get:
    prober: http
    timeout: 5s
    http:
      valid_http_versions: ["HTTP/1.1", "HTTP/2.0"]
      valid_status_codes: [200]
      no_follow_redirects: false
      tls_config:
        insecure_skip_verify: true

配置文件很好理解, 其中配置项”no_follow_redirects:false“主要是防止误判,很多站点会配置http到https的跳转,这时http的返回状态码不是200,但站点是正常的,加这个选项让请求根据响应头”Location“的值再发起新的请求,以最终的响应状态码为准。

2. prometheus
prometheus的安装和运行比较简单。prometheus配置文件如下,
prometheus.yaml

# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

  - job_name: 'blackbox-access-baidu'
    scrape_interval: 1m
    metrics_path: /probe
    params:
      module: [http_get]
    static_configs:
      - targets:
        - https://www.baidu.com
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 10.0.1.5:9115  # The blackbox exporter's real hostname:port.

有一处不要写错了,

   params:
      module: [http_get]

3. grafana
grafana官网“
https://grafana.com/grafana/dashboards/”有专门针对blackbox exporter的dashboard,根据需要选择,选定后直接通过ID在grafana上导入。效果图,

监控效果图.png
欢迎评论、交流!

相关文章

网友评论

      本文标题:使用blackbox监控Web应用

      本文链接:https://www.haomeiwen.com/subject/ahtwmrtx.html