Skip to main content

Data and monitoring · Intermediate · 25 min

Check that sites respond with Prometheus Blackbox Exporter

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Prometheus 3.16.0 · Blackbox Exporter 0.29.1

You will run Blackbox Exporter 0.29 next to Prometheus 3.16 on Ubuntu 26.04, probe web pages for an expected string, a database port and a TLS certificate, and alert on two failed probes in a row and on certificates that expire within 14 days.

On this page

Node Exporter tells you how a server feels from the inside. It cannot tell you whether a customer can open your shop. The web server can be running while the page shows a database error, the certificate can run out on a Sunday, or the database port can stop answering after an upgrade. To catch those, something has to act like a visitor: open the page, read what comes back and report the result.

Blackbox Exporter does exactly that for Prometheus. You give it a list of modules, each one a kind of check ("fetch this over HTTP and expect these words", "open a TCP connection"). Prometheus then asks it to run a module against a target on every scrape, and stores the result as metrics such as probe_success and probe_ssl_earliest_cert_expiry. Your existing alert rules and Alertmanager do the rest.

In this tutorial you build three small demo sites on the server so you can see both passing and failing checks: a healthy shop page, a copy of it that shows an error, and an HTTPS page whose certificate expires in seven days. You also check a database port on 127.0.0.1:5432. Once it works, you swap the demo addresses for your own. Blackbox Exporter is installed from the official release file, so it gets no apt updates; the last steps say how to upgrade it.

Step 1 · Start three demo sites

Make a folder for the demo and two versions of the same page. The healthy one says Shop is open. The broken one shows an error message but is still served with status 200, which is what many apps do when their database fails. A check that only looks at the status code would call both of them fine:

Bash
mkdir -p ~/blackbox-demo/healthy ~/blackbox-demo/broken ~/blackbox-demo/tls
echo '<h1>Shop is open</h1>' > ~/blackbox-demo/healthy/index.html
echo '<h1>Error establishing a database connection</h1>' > ~/blackbox-demo/broken/index.html
nohup python3 -m http.server 8081 --bind 127.0.0.1 --directory ~/blackbox-demo/healthy > ~/blackbox-demo/healthy.log 2>&1 &
nohup python3 -m http.server 8082 --bind 127.0.0.1 --directory ~/blackbox-demo/broken > ~/blackbox-demo/broken.log 2>&1 &
sleep 1

The third site uses HTTPS with a certificate from a private certificate authority (CA) you create now. The CA is valid for 30 days and the site certificate for 7, so it falls inside the 14-day warning you set up later. The keys are written with umask 077, so only you can read them:

Bash
cd ~/blackbox-demo/tls
(
  umask 077
  openssl req -x509 -newkey rsa:2048 -nodes -days 30 -subj "/CN=Blackbox demo CA" -keyout ca.key -out ca.crt
  openssl req -newkey rsa:2048 -nodes -subj "/CN=localhost" -keyout site.key -out site.csr
)
printf 'subjectAltName=DNS:localhost\n' > site.ext
openssl x509 -req -in site.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 7 -extfile site.ext -out site.crt
openssl x509 -in site.crt -noout -enddate

The last line prints notAfter= and a date seven days from now. Serve the page with a few lines of Python:

Bash
cat > ~/blackbox-demo/tls-site.py <<'PY'
import functools
import ssl
from http.server import HTTPServer, SimpleHTTPRequestHandler
from pathlib import Path

demo = Path.home() / "blackbox-demo"
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(demo / "tls" / "site.crt", demo / "tls" / "site.key")
handler = functools.partial(SimpleHTTPRequestHandler, directory=str(demo / "healthy"))
server = HTTPServer(("127.0.0.1", 8443), handler)
server.socket = context.wrap_socket(server.socket, server_side=True)
server.serve_forever()
PY
nohup python3 ~/blackbox-demo/tls-site.py > ~/blackbox-demo/tls.log 2>&1 &
sleep 1

Check it worked

Fetch each site. The --cacert option tells curl to trust your demo CA for the HTTPS one:

Bash
curl -s http://127.0.0.1:8081/
curl -s http://127.0.0.1:8082/
curl -s --cacert ~/blackbox-demo/tls/ca.crt https://localhost:8443/

You see Shop is open, then the database error, then Shop is open again. If the third command prints nothing, run it with -v instead of -s to see the TLS error.

Step 2 · Install Blackbox Exporter

Set the version and your architecture, then download the release file with its checksum list from the official GitHub release, and check it:

Bash
BB_VERSION=0.29.1
ARCH="$(dpkg --print-architecture)"
mkdir -p ~/monitoring-install
cd ~/monitoring-install
BASE="https://github.com/prometheus/blackbox_exporter/releases/download/v${BB_VERSION}"
curl -fsSLO "${BASE}/blackbox_exporter-${BB_VERSION}.linux-${ARCH}.tar.gz"
curl -fsSL -o blackbox-sha256sums.txt "${BASE}/sha256sums.txt"
sha256sum --ignore-missing -c blackbox-sha256sums.txt
tar -xzf "blackbox_exporter-${BB_VERSION}.linux-${ARCH}.tar.gz"
sudo install -o root -g root -m 0755 "blackbox_exporter-${BB_VERSION}.linux-${ARCH}/blackbox_exporter" /usr/local/bin/blackbox_exporter
blackbox_exporter --version | head -n 1

Check https://github.com/prometheus/blackbox_exporter/releases first and use a newer version if there is one.

Create a system user with no login for it, and its configuration folder. Copy in the demo CA certificate, which is public, so the exporter can trust the HTTPS site:

Bash
sudo useradd --system --no-create-home --shell /usr/sbin/nologin blackbox_exporter
sudo install -d -o root -g root -m 0755 /etc/blackbox_exporter
sudo install -o root -g root -m 0644 ~/blackbox-demo/tls/ca.crt /etc/blackbox_exporter/demo-ca.crt

Check it worked

Confirm the download, the installed program and the new user:

Bash
cd ~/monitoring-install
sha256sum --ignore-missing -c blackbox-sha256sums.txt
blackbox_exporter --version | head -n 1
id blackbox_exporter
ls -l /etc/blackbox_exporter/

The checksum line ends in OK, the version line starts with blackbox_exporter, version 0.29.1, id prints the new user and group, and the folder holds demo-ca.crt.

Step 3 · Describe the checks as modules

A module says how to check something, not what to check. The target comes later, from Prometheus. Write three modules:

Bash
sudo tee /etc/blackbox_exporter/blackbox.yml > /dev/null <<'YAML'
modules:
  http_2xx_shop:
    prober: http
    timeout: 5s
    http:
      preferred_ip_protocol: ip4
      fail_if_body_not_matches_regexp:
        - "Shop is open"

  https_demo_ca:
    prober: http
    timeout: 5s
    http:
      preferred_ip_protocol: ip4
      fail_if_not_ssl: true
      tls_config:
        ca_file: /etc/blackbox_exporter/demo-ca.crt

  tcp_connect:
    prober: tcp
    timeout: 5s
    tcp:
      preferred_ip_protocol: ip4
YAML

What each module does:

  • http_2xx_shop fetches the page, follows redirects, and passes only when the final status is in the 2xx range (the default) and the body contains Shop is open. Pick a string that only appears when the page really works, such as a product heading or a footer line, rather than one that also shows on error pages.
  • https_demo_ca fails if the page is not served over HTTPS, and trusts your demo CA. Sites with a public certificate, such as one from Let's Encrypt, need no tls_config: the system's trusted CAs are used.
  • tcp_connect passes when a TCP connection opens. It proves the database port answers, not that queries work.
  • preferred_ip_protocol: ip4 tries IPv4 first. The default is IPv6, so on a server without IPv6 connectivity a site that has an IPv6 address would fail the probe even though it works over IPv4. Set ip6 for sites you serve over IPv6 and want checked that way.

Check it worked

Ask the exporter to read the file without starting:

Bash
blackbox_exporter --config.check --config.file=/etc/blackbox_exporter/blackbox.yml

It logs that the configuration is fine and exits. A typo is reported with the line it is on; fix it and run the check again.

Step 4 · Run Blackbox Exporter as a service

The exporter has no login, and its /probe address makes your server connect to any target a caller names. Keep it on 127.0.0.1:

Bash
sudo tee /etc/systemd/system/blackbox_exporter.service > /dev/null <<'UNIT'
[Unit]
Description=Prometheus Blackbox Exporter
After=network-online.target
Wants=network-online.target

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

[Install]
WantedBy=multi-user.target
UNIT
Bash
sudo systemctl daemon-reload
sudo systemctl enable --now blackbox_exporter
sudo systemctl status blackbox_exporter --no-pager

The status shows active (running). After a change to blackbox.yml, run the --config.check command again, then sudo systemctl reload blackbox_exporter.

Check it worked

Run a probe by hand. The /probe address takes a module and a target, and answers with the result as metrics:

Bash
probe() { curl -s -G http://127.0.0.1:9115/probe --data-urlencode "module=$1" --data-urlencode "target=$2" | grep -E '^probe_(success|http_status_code) '; }
probe http_2xx_shop http://127.0.0.1:8081/
probe http_2xx_shop http://127.0.0.1:8082/
probe tcp_connect 127.0.0.1:5432
ss -tln | grep 9115

The healthy page gives probe_http_status_code 200 and probe_success 1. The broken page also gives status 200, but probe_success 0: the body check caught what the status code hid. The database port gives probe_success 1, and the listener is 127.0.0.1:9115.

To see why a probe failed, add debug=true. The answer then includes the probe's log:

Bash
curl -s -G http://127.0.0.1:9115/probe --data-urlencode module=http_2xx_shop --data-urlencode target=http://127.0.0.1:8082/ --data-urlencode debug=true | grep -i 'regular expression'

You get a line saying the body did not match the regular expression, with the pattern you set.

Step 5 · Point Prometheus at the targets

Prometheus normally scrapes the address in targets. For probes, it has to scrape the exporter instead and pass the target along as a parameter. Three relabelling rules do that, in this order:

  1. Copy the target address into __param_target, which Prometheus sends as ?target=....
  2. Copy it into the instance label, so the stored metrics name the site, not the exporter.
  3. Replace the address to scrape with the exporter's, 127.0.0.1:9115.

This is the configuration from the Alertmanager tutorial plus three probe jobs and a job for the exporter's own metrics. If you have added other jobs since, keep them. The probe jobs use a 30-second interval, which is often enough for an uptime check and halves the traffic to your sites:

Bash
sudo tee /etc/prometheus/prometheus.yml > /dev/null <<'YAML'
global:
  scrape_interval: 15s
  evaluation_interval: 15s

rule_files:
  - /etc/prometheus/rules/*.yml

alerting:
  alertmanagers:
    - static_configs:
        - targets: ["127.0.0.1:9093"]

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ["127.0.0.1:9090"]

  - job_name: node
    static_configs:
      - targets: ["127.0.0.1:9100"]

  - job_name: blackbox_http
    metrics_path: /probe
    scrape_interval: 30s
    params:
      module: [http_2xx_shop]
    static_configs:
      - targets:
          - http://127.0.0.1:8081/
          - http://127.0.0.1:8082/
    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

  - job_name: blackbox_tls
    metrics_path: /probe
    scrape_interval: 30s
    params:
      module: [https_demo_ca]
    static_configs:
      - targets: [https://localhost:8443/]
    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

  - job_name: blackbox_tcp
    metrics_path: /probe
    scrape_interval: 30s
    params:
      module: [tcp_connect]
    static_configs:
      - targets: [127.0.0.1:5432]
    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

  - job_name: blackbox_exporter
    static_configs:
      - targets: ["127.0.0.1:9115"]
YAML
promtool check config /etc/prometheus/prometheus.yml

promtool prints SUCCESS for the configuration and for the rule files it loads. Reload Prometheus:

Bash
sudo systemctl reload prometheus

Check it worked

Wait a minute so each probe has run twice, then ask Prometheus for the latest result of every probe:

Bash
curl -s http://127.0.0.1:9090/api/v1/query --data-urlencode 'query=probe_success' |
  jq -r '.data.result[] | "\(.metric.job) \(.metric.instance) \(.value[1])"' | sort

You get four lines: blackbox_http http://127.0.0.1:8081/ 1, blackbox_http http://127.0.0.1:8082/ 0, blackbox_tcp 127.0.0.1:5432 1 and blackbox_tls https://localhost:8443/ 1. If a line is missing, up{job=~"blackbox_.*"} shows whether Prometheus could reach the exporter at all.

Now ask how many days the HTTPS certificate has left. probe_ssl_earliest_cert_expiry is the expiry time, in seconds since 1970, of the certificate that runs out first among those the site sends:

Bash
curl -s http://127.0.0.1:9090/api/v1/query --data-urlencode 'query=(probe_ssl_earliest_cert_expiry - time()) / 86400' |
  jq -r '.data.result[] | "\(.metric.instance) \(.value[1])"'

The answer is https://localhost:8443/ followed by a number just under 7.

Step 6 · Alert on failures and expiring certificates

Write two rules. A single failed probe is often a blip on the network between the exporter and the site, so SiteDown waits for a second failure. With probes every 30 seconds, for: 30s means the alert goes pending at the first failure and fires only if the next probe fails too. CertExpiringSoon fires when a certificate has less than 14 days left, and waits an hour so a brief probe error never pages anyone:

Bash
sudo tee /etc/prometheus/rules/blackbox.yml > /dev/null <<'YAML'
groups:
  - name: blackbox
    rules:
      - alert: SiteDown
        expr: probe_success{job=~"blackbox_.*"} == 0
        for: 30s
        labels:
          severity: critical
        annotations:
          summary: "{{ $labels.instance }} failed two probes in a row"

      - alert: CertExpiringSoon
        expr: probe_ssl_earliest_cert_expiry - time() < 14 * 86400
        for: 1h
        labels:
          severity: warning
        annotations:
          summary: "TLS certificate for {{ $labels.instance }} expires within 14 days"
YAML
promtool check rules /etc/prometheus/rules/blackbox.yml

promtool prints SUCCESS: 2 rules found. Fourteen days leaves time to fix a stuck renewal: automatic renewal tools such as Certbot renew well before the last two weeks, so a certificate this close to expiry usually means renewal has been failing for a while. An expired certificate also makes the probe itself fail, so SiteDown covers that case as well.

Test both rules on made-up data before trusting them. The first test has one site that fails twice and one that fails once and recovers; only the first may fire. The second has one certificate with about 11 days left and one with about 58:

Bash
cat > ~/blackbox-demo/blackbox-test.yml <<'YAML'
rule_files:
  - /etc/prometheus/rules/blackbox.yml

evaluation_interval: 15s

tests:
  - interval: 30s
    input_series:
      - series: 'probe_success{job="blackbox_http", instance="https://down.example/"}'
        values: '1 0 0 0'
      - series: 'probe_success{job="blackbox_http", instance="https://blip.example/"}'
        values: '1 0 1 1'
    alert_rule_test:
      - eval_time: 45s
        alertname: SiteDown
        exp_alerts: []
      - eval_time: 1m
        alertname: SiteDown
        exp_alerts:
          - exp_labels:
              severity: critical
              job: blackbox_http
              instance: "https://down.example/"
            exp_annotations:
              summary: "https://down.example/ failed two probes in a row"

  - interval: 1m
    input_series:
      - series: 'probe_ssl_earliest_cert_expiry{job="blackbox_tls", instance="https://soon.example/"}'
        values: '1000000+0x90'
      - series: 'probe_ssl_earliest_cert_expiry{job="blackbox_tls", instance="https://later.example/"}'
        values: '5000000+0x90'
    alert_rule_test:
      - eval_time: 30m
        alertname: CertExpiringSoon
        exp_alerts: []
      - eval_time: 70m
        alertname: CertExpiringSoon
        exp_alerts:
          - exp_labels:
              severity: warning
              job: blackbox_tls
              instance: "https://soon.example/"
            exp_annotations:
              summary: "TLS certificate for https://soon.example/ expires within 14 days"
YAML
promtool test rules ~/blackbox-demo/blackbox-test.yml

The output ends in SUCCESS. In the test, time starts at 0, so an expiry value of 1,000,000 seconds is about 11.6 days away and 5,000,000 is about 58. After 30 minutes the certificate alert is still pending; after 70 it fires for soon.example only.

Reload Prometheus so it loads the new rule file:

Bash
sudo systemctl reload prometheus

Check it worked

The broken demo page fails every probe, so SiteDown fires for it within about a minute. This loop waits for that, for up to three minutes, then lists the two alerts with their state:

Bash
for i in $(seq 1 36); do
  if curl -s http://127.0.0.1:9090/api/v1/alerts | jq -e '.data.alerts[] | select(.labels.alertname == "SiteDown" and .state == "firing")' > /dev/null; then
    break
  fi
  sleep 5
done
curl -s http://127.0.0.1:9090/api/v1/alerts |
  jq -r '.data.alerts[] | select(.labels.alertname == "SiteDown" or .labels.alertname == "CertExpiringSoon") | "\(.labels.alertname) \(.labels.instance) \(.state)"'

You see SiteDown http://127.0.0.1:8082/ firing and CertExpiringSoon https://localhost:8443/ pending. The certificate alert turns to firing after an hour. Prometheus sends both to Alertmanager, which routes them to the receiver you set up in the Alertmanager tutorial.

Fix the broken page and watch the alert clear. The page now holds the expected string, so the next probe passes:

Bash
echo '<h1>Shop is open</h1>' > ~/blackbox-demo/broken/index.html
for i in $(seq 1 24); do
  if ! curl -s http://127.0.0.1:9090/api/v1/alerts | jq -e '.data.alerts[] | select(.labels.alertname == "SiteDown")' > /dev/null; then
    echo "SiteDown has cleared"
    break
  fi
  sleep 5
done

Within a minute the loop prints SiteDown has cleared.

Step 7 · Check your real sites and probe from a second location

Replace the demo targets with your own. For a public HTTPS site, add a module like http_2xx_shop with your own expected string, and leave out tls_config. Each site whose page differs gets its own module, or you can share one module whose string appears on all of them, such as your company name in the footer. For the database, put its real address and port in the blackbox_tcp targets.

Two more checks are useful once the basics work:

  • Slow pages: probe_duration_seconds is how long the whole probe took. A rule such as probe_duration_seconds{job="blackbox_http"} > 3 with for: 5m warns you before visitors give up. Many people in Africa and elsewhere open sites on low-end Android phones over prepaid mobile data, where a page that is slow from a data centre is far slower for them.
  • Ping: the icmp prober checks that a host answers ping. On Linux it needs the exporter's user to be in the range allowed by net.ipv4.ping_group_range, or the CAP_NET_RAW capability, which you would add to the unit as AmbientCapabilities=CAP_NET_RAW. Most uptime questions are better answered by HTTP or TCP checks, because a host can answer ping with its web server down.

So far everything runs on one server, which means the checks travel no further than the machine itself. They prove the site works, not that the internet can reach it. A firewall mistake, a DNS error or an outage at your hosting provider would go unnoticed, and if that server goes down, so do the checks. For a real outside view, run a second Blackbox Exporter on a small server with a different provider, ideally in the region your visitors are in, for example a server hosted in Lagos, Nairobi or Johannesburg if most of your visitors are in Africa.

On that second server, install the exporter as above, but listen on its private or VPN address instead of 127.0.0.1. Allow only your Prometheus server to reach port 9115, because /probe makes the server fetch any address it is given. Replace 10.0.0.2 with your Prometheus server's address, and see the UFW tutorial for the rest of the firewall. The exporter also accepts --web.config.file for TLS and a password, if the traffic crosses the public internet:

Bash
sudo ufw allow from 10.0.0.2 to any port 9115 proto tcp

In Prometheus, copy the blackbox_http job as blackbox_http_remote, point its last relabelling rule at the second exporter (for example replacement: 10.0.0.9:9115), and add a label that names the location:

YAML
      - target_label: location
        replacement: second-site

With two locations, decide what deserves a page. One location failing usually means trouble on that path; both failing means the site is down for everyone. This expression is 0 only when every location fails for a site, so you can use it in place of probe_success in a SiteDownEverywhere rule:

Text
max by (instance) (probe_success{job=~"blackbox_http.*"}) == 0

Check it worked

After you reload Prometheus with your real targets, wait a minute and list the latest HTTP probe result for each site and location:

Bash
curl -s http://127.0.0.1:9090/api/v1/query --data-urlencode 'query=probe_success{job=~"blackbox_http.*"}' |
  jq -r '.data.result[] | "\(.metric.instance) \(.metric.location // "local") \(.value[1])"' | sort

You see one local line per real site, ending in 1. Once the remote job is added, each site also has a second-site line. A 0 on only the second-site line points to the network path, the firewall or DNS as seen from outside, not the site itself.

Step 8 · Remove the demo

When you have moved to your real targets, take the demo out of the configuration first. Remove the https_demo_ca module from blackbox.yml and the demo targets from prometheus.yml, then check both files and reload both services. Do this before deleting the demo CA file: while the module still points at it, the next reload would fail.

Bash
blackbox_exporter --config.check --config.file=/etc/blackbox_exporter/blackbox.yml
promtool check config /etc/prometheus/prometheus.yml
sudo systemctl reload blackbox_exporter prometheus

Then stop the demo sites and delete their files. The pkill pattern only matches programs started from the demo folder:

Bash
pkill -f "$HOME/blackbox-demo" || true
rm -rf ~/blackbox-demo
sudo rm /etc/blackbox_exporter/demo-ca.crt

If this was only a trial and you want Blackbox Exporter gone as well, this stops it and deletes its unit, program, configuration, user and rules. Afterwards, write back the prometheus.yml from the Alertmanager tutorial and reload Prometheus:

Bash
sudo systemctl disable --now blackbox_exporter
sudo rm /etc/systemd/system/blackbox_exporter.service
sudo systemctl daemon-reload
sudo rm /usr/local/bin/blackbox_exporter /etc/prometheus/rules/blackbox.yml
sudo rm -r /etc/blackbox_exporter
sudo userdel blackbox_exporter
rm -rf ~/monitoring-install

To upgrade later, set BB_VERSION to the new release, repeat the download and checksum commands, stop the service, install the new binary over the old one, and start it again. Read the release notes first: version 0.29.0, for example, made the exporter refuse a configuration with an invalid prober value that older versions accepted.

Check it worked

Bash
sleep 1
ss -tln | grep -E ':(8081|8082|8443) ' || echo "demo sites stopped"

The output is demo sites stopped.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.