Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 184Observability · Prometheus

How to monitor TLS certificates with Prometheus

An expired certificate breaks a site, an API or an internal service at a random hour — and auto-renewal fails silently more often than people think. Prometheus can watch every certificate: public endpoints with the blackbox exporter, certificate files on servers with a small textfile script, and Kubernetes certificates from cert-manager. One alert rule then warns 14 days before any of them expires. This how-to sets up all three.

  • 5 Lessen
  • 4 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

184.1

The quick answer

cert-alerts.yml
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: ticket
promtool check rules cert-alerts.yml

184.2

Step by step

  1. Public endpoints: blackbox exporter — probe each HTTPS URL (module http_2xx, see "Monitor a website from outside"). It exports probe_ssl_earliest_cert_expiry (Unix time of the first certificate in the chain to expire).
  2. List all endpoints — APIs, admin panels, mail (tcp module with tls: true for SMTPS/IMAPS), internal services with their own CA (tls_config.ca_file in the module).
  3. Certificate files on servers — a script writes a metric file for node_exporter's textfile collector:
/usr/local/bin/cert-metrics.sh
#!/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.

  1. Kubernetes with cert-manager — it exports certmanager_certificate_expiration_timestamp_seconds; use the same expression with that metric. Also alert on certmanager_certificate_ready_status{condition="False"} == 1.
  2. Alert rules (quick answer) — 14 days is enough time to fix a broken renewal. Add a page alert at 3 days.
  3. Test the rules with promtool — a unit test proves the alert fires:
cert-alerts-test.yml
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.yml

184.3

Check 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.

184.4

Common problems

ProblemCauseFix
probe_ssl_earliest_cert_expiry missingThe probe failed before TLS, or plain HTTP.Check probe_success; use an https:// target.
Value is the CA, not the server certificateThe metric is the earliest in the chain (an intermediate expires first).That is correct — the whole chain must be renewed.
Textfile metrics do not appearWrong directory, or file not readable.Same directory as --collector.textfile.directory; chmod 644.
date: invalid date in the scriptNot GNU date (macOS, Alpine busybox).Run on Linux with GNU coreutils, or parse with Python.
Alert never fires in the testExpected labels or annotation text do not match exactly.Copy the labels from the rule; check the summary text.
Internal CA endpoints failProber does not trust the private CA.tls_config.ca_file in the blackbox module.

Commando's in deze les

184.5

Go further

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.

Alle commando's in dit hoofdstuk