If you already run Prometheus, the blackbox exporter can watch your certificates. Here's the setup and an alert rule that also catches the case where the probe itself fails.
Blackbox exporter module
In blackbox.yml, one module for HTTPS sites and one for other TLS ports like IMAPS 993:
modules:
http_2xx:
prober: http
timeout: 10s
tls_connect:
prober: tcp
timeout: 10s
tcp:
tls: true
Scrape config
scrape_configs:
- job_name: blackbox-https
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets: [https://example.com, https://www.example.com]
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
For mail servers, copy the job with module: [tls_connect] and targets like mail.example.com:993.
Alert rules
groups:
- name: certificates
rules:
- alert: CertificateExpiresSoon
expr: (probe_ssl_earliest_cert_expiry - time()) / 86400 < 14
for: 1h
annotations:
summary: "{{ $labels.instance }}: certificate expires in {{ $value | humanize }} days"
- alert: ProbeFailing
expr: probe_success == 0
for: 30m
annotations:
summary: "{{ $labels.instance }}: probe failing"
The second rule matters. When the TLS handshake fails, for example because the certificate already expired or the chain is broken,
the probe fails and probe_ssl_earliest_cert_expiry isn't there at all. The expiry rule then has nothing to compare and stays quiet.
Things that trip people up
- Earliest means the whole chain. The metric is the earliest expiry of every certificate the server sends, intermediates included. An old intermediate in your chain file can fire the alert while your own certificate is fine.
- 14 days with Let's Encrypt. certbot and most ACME clients renew at about 30 days left. Anything under 30 days already means renewal failed. An alert at 21 days gives you a week more to fix it.
- Domains aren't covered. The exporter checks certificates, not when the domain registration runs out. See domain expiry lookup.
- Alerting has to work too. If Alertmanager or its mail route breaks, nothing reaches you. A check that runs outside your own setup covers that case.