You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Blackbox Exporter将网页内嵌状态值导入Prometheus?

Great question—abusing HTTP status codes is definitely a hack that causes more headaches than it solves, so I’m glad you’re hunting for a cleaner approach. Here are a few robust, semantic ways to get those page states into Prometheus without mucking up standard HTTP behavior:

1. Use Blackbox Exporter's Built-in Body Matching Metrics

Blackbox actually comes with a probe_http_response_body_matches metric that’s perfect for this scenario. You can configure your Blackbox module to check for each of your page states via regex, and the metric will return 1 (match found) or 0 (no match) for each pattern.

First, update your Blackbox config.yml with a module that targets your page and checks for all three states:

modules:
  page_state_prober:
    prober: http
    timeout: 5s
    http:
      valid_status_codes: [200]  # Keep standard 200 for all page states
      method: GET
      body_regex:
        - 'standby'
        - 'fetching from external source'
        - 'done'

When Blackbox runs this probe, it’ll generate three instances of probe_http_response_body_matches, each tagged with the regex pattern that was checked (via the regex label). You can then write PromQL queries or alert rules based on these, like:

# Alert when the page is in standby state
probe_http_response_body_matches{job="your_page_job", regex="standby"} == 1

2. Extract State from a Custom HTTP Header (If You Control the Backend)

If you have access to modify the page’s backend service, add a custom HTTP header like X-Page-State that directly exposes the current state (e.g., X-Page-State: done). Blackbox can capture this header, and you can use Prometheus relabeling to turn it into a reusable label.

First, adjust your Blackbox module to ensure it fetches headers normally:

modules:
  page_header_prober:
    prober: http
    http:
      valid_status_codes: [200]
      method: GET
      headers:
        Accept: text/html

Then, update your Prometheus prometheus.yml to extract the header value into a label:

scrape_configs:
  - job_name: 'blackbox_page_state'
    metrics_path: /probe
    params:
      module: [page_header_prober]
    static_configs:
      - targets: ['your-target-page.com']
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: blackbox-exporter:9115  # Replace with your Blackbox address
    metric_relabel_configs:
      - source_labels: [probe_http_header_X_Page_State]
        target_label: page_state

Now all your Blackbox metrics for this job will have a page_state tag with values like standby, fetching from external source, or done—making it easy to write queries and alerts.

3. Use Textfile Exporter for Custom Parsing (Most Flexible)

If the above methods don’t work (e.g., you can’t modify the backend, or the page state is buried in complex HTML), the Textfile Exporter (or Node Exporter’s textfile collector) lets you run custom scripts to parse the page and export Prometheus-compatible metrics.

For example, create a simple shell script that fetches the page, extracts the state, and writes it to a .prom file:

#!/bin/bash
# Fetch the page and extract the state
PAGE_STATE=$(curl -s http://your-target-page.com | grep -oE '(standby|fetching from external source|done)')
# Write the metric to a file (match the path your exporter uses)
echo "page_state{state=\"$PAGE_STATE\"} 1" > /var/lib/node_exporter/textfile_collector/page_state.prom

Then set up a cron job to run this script at your desired interval. Configure Node Exporter with --collector.textfile.directory=/var/lib/node_exporter/textfile_collector (or use standalone Textfile Exporter) and have Prometheus scrape it. This method gives you full control over parsing logic for even the trickiest page structures.


内容的提问来源于stack exchange,提问作者Alen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 14:32:40