$ run as a normal user# run as root (or with sudo)
The quick answer
groups:
- name: tls
rules:
- alert: TLSCertExpiresSoon
expr: probe_ssl_earliest_cert_expiry - time() < 14 * 86400
for: 1h
labels:
severity: ticket
annotations:
summary: "Certificate of {{ $labels.instance }} expires in {{ $value | humanizeDuration }}"
- alert: TLSCertFileExpiresSoon
expr: tls_cert_not_after_seconds - time() < 14 * 86400
for: 1h
labels:
severity: ticketpromtool check rules cert-alerts.ymlStep by step
- Public endpoints: blackbox exporter — probe each HTTPS URL (module
http_2xx, see "Monitor a website from outside"). It exportsprobe_ssl_earliest_cert_expiry(Unix time of the first certificate in the chain to expire). - List all endpoints — APIs, admin panels, mail (
tcpmodule withtls: truefor SMTPS/IMAPS), internal services with their own CA (tls_config.ca_filein the module). - Certificate files on servers — a script writes a metric file for node_exporter's textfile collector:
#!/usr/bin/env bash
set -euo pipefail
out=/var/lib/node_exporter/textfile/certs.prom
tmp=$(mktemp)
for f in /etc/ssl/certs/app-*.pem /etc/letsencrypt/live/*/cert.pem; do
[ -f "$f" ] || continue
end=$(openssl x509 -enddate -noout -in "$f" | cut -d= -f2)
echo "tls_cert_not_after_seconds{file=\"$f\"} $(date -d "$end" +%s)" >> "$tmp"
done
chmod 644 "$tmp"
mv "$tmp" "$out"Run it every hour with a systemd timer; start node_exporter with --collector.textfile.directory=/var/lib/node_exporter/textfile.
- Kubernetes with cert-manager — it exports
certmanager_certificate_expiration_timestamp_seconds; use the same expression with that metric. Also alert oncertmanager_certificate_ready_status{condition="False"} == 1. - Alert rules (quick answer) — 14 days is enough time to fix a broken renewal. Add a
pagealert at 3 days. - Test the rules with promtool — a unit test proves the alert fires:
rule_files: [cert-alerts.yml]
evaluation_interval: 1m
tests:
- interval: 1m
input_series:
- series: 'probe_ssl_earliest_cert_expiry{instance="https://www.example.com/"}'
values: '86400+0x120'
alert_rule_test:
- eval_time: 90m
alertname: TLSCertExpiresSoon
exp_alerts:
- exp_labels: {severity: ticket, instance: "https://www.example.com/"}
exp_annotations:
summary: "Certificate of https://www.example.com/ expires in 22h 30m 0s"(Test time starts at 0: the certificate "expires" at 86400, one day later — less than 14 days, so the rule fires after its for: 1h. At 90 minutes 22.5 hours are left.)
promtool test rules cert-alerts-test.ymlCheck that it worked
In Prometheus, (probe_ssl_earliest_cert_expiry - time()) / 86400 shows the days left for each endpoint, and tls_cert_not_after_seconds shows each file. promtool test rules passes. A test certificate that expires in 10 days fires the alert.
Common problems
| Problem | Cause | Fix |
|---|---|---|
probe_ssl_earliest_cert_expiry missing | The probe failed before TLS, or plain HTTP. | Check probe_success; use an https:// target. |
| Value is the CA, not the server certificate | The metric is the earliest in the chain (an intermediate expires first). | That is correct — the whole chain must be renewed. |
| Textfile metrics do not appear | Wrong directory, or file not readable. | Same directory as --collector.textfile.directory; chmod 644. |
date: invalid date in the script | Not GNU date (macOS, Alpine busybox). | Run on Linux with GNU coreutils, or parse with Python. |
| Alert never fires in the test | Expected labels or annotation text do not match exactly. | Copy the labels from the rule; check the summary text. |
| Internal CA endpoints fail | Prober does not trust the private CA. | tls_config.ca_file in the blackbox module. |
Go further
- Command pages: promtool, openssl, node_exporter.
- Next how-tos: Check when a TLS certificate expires, Monitor a website from outside, Get a free TLS certificate with Let's Encrypt, Create a systemd timer.
- Learn it: Observability tools at work — Prometheus, Observability services — node exporter. Full guide: Prometheus.
1Why monitor certificates when auto-renewal is on?
Renewal fails silently (DNS change, firewall, rate limit); only a check on the real expiry date catches it in time.
2How do certificate files become Prometheus metrics?
A script writes them as a .prom file into node_exporter's textfile directory, and node_exporter exports them.