blackbox_exporter 配置POST提交application/x-www-form-urlencoded表单数据

使用blackbox_exporter 监控post路由资源

配置blackbox.yml

modules:
  post_two_2xx:
    prober: http
    timeout: 5s
    http:
      valid_http_versions: ["HTTP/1.1", "HTTP/2.0"]
      valid_status_codes: []
      method: POST
      headers:
        Accept: application/json, text/javascript, */*; q=0.01
        Content-Type: 'application/x-www-form-urlencoded; charset=UTF-8'
        Sec-Fetch-Dest: empty
        Sec-Fetch-Mode: cors
        Sec-Fetch-Site: same-origin
        User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
        X-Requested-With: XMLHttpRequest
      body: 'code=200&type=0'
      preferred_ip_protocol: "ip4"
      ip_protocol_fallback: false
      fail_if_body_not_matches_regexp: [".*success.*"]

Prometheus配置

  - job_name: blackbox-http-post
    metrics_path: /probe
    scrape_interval: 10s
    params:
      module: [post_two_2xx]
    static_configs:
      - targets:
        - http://192.168.1.120:8080/v1/
        - https://监控站点

重启Prometheus 和 blackbox_exporter 就可以了

如果从probe_http_status_code获取到的状态码为500,则是提交的body参数可能出现了问题,如果Content-Type是application/json,body直接为 ‘{“code”:200}’ 即可,如果Content-Type为application/x-www-form-urlencoded,参数格式为 ‘code=2000&type=11’ ,必要时可进行url编码