Renewal & Expiry Monitoring
Automated renewal (ACME & Let's Encrypt) fails silently more often than people expect: a firewall change blocks port 80, a DNS API token expires, the deploy hook never reloads the proxy, a certificate was issued by hand once and nobody remembers. The defence is a check that is independent of the renewal job: look at the certificate that is actually served (or on disk), and alert while there are still weeks left.
Checking a certificate file
openssl x509 -checkend <seconds> answers "will this certificate have expired in N seconds?" with an exit code (0 = no, 1 = yes), which makes it a ready-made check. To have certificates with a known state, the session makes three: one valid for 90 days, one for 20 days, and one whose validity ended in 2025 (signed with openssl ca, which accepts fixed start and end dates):
openssl req -x509 -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout ca.key -out ca.crt -days 3650 -subj "/CN=Lab CA" 2>/dev/null
mkcert() { # name days
openssl req -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout "$1.key" -subj "/CN=$1.example.test" 2>/dev/null |
openssl x509 -req -CA ca.crt -CAkey ca.key -set_serial "0x10$2" -days "$2" -out "$1.crt" 2>/dev/null
}
mkcert shop 90
mkcert api 20
# a certificate that expired on 2025-04-01, with fixed dates via `openssl ca`
mkdir -p ca/new && touch ca/index.txt && echo 1000 > ca/serial
printf '[ca]\ndefault_ca=lab\n[lab]\ndir=./ca\ndatabase=$dir/index.txt\nnew_certs_dir=$dir/new\nserial=$dir/serial\ndefault_md=sha256\npolicy=p\n[p]\ncommonName=supplied\n' > ca.cnf
openssl req -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout legacy.key -out legacy.csr -subj "/CN=legacy.example.test" 2>/dev/null
openssl ca -batch -config ca.cnf -cert ca.crt -keyfile ca.key -in legacy.csr -out legacy.crt \
-startdate 20250101000000Z -enddate 20250401000000Z > /dev/null 2>&1
openssl x509 -in legacy.crt -noout -enddate
for c in shop api legacy; do
if openssl x509 -in $c.crt -noout -checkend $((30 * 86400)) > /dev/null; then
echo "$c: valid for at least 30 more days"
else
echo "$c: expires within 30 days (or already has)"
fi
done
openssl verify -CAfile ca.crt legacy.crt 2>&1 | tail -2notAfter=Apr 1 00:00:00 2025 GMT
shop: valid for at least 30 more days
api: expires within 30 days (or already has)
legacy: expires within 30 days (or already has)
error 10 at 0 depth lookup: certificate has expired
error legacy.crt: verification failed
A check script with thresholds
A monitoring check should say how long is left and map that to a state. This one reads every *.crt in a directory, computes the days left from notAfter, and exits with the Nagios-style codes that most monitoring systems understand (0 OK, 1 WARNING, 2 CRITICAL):
openssl req -x509 -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout ca.key -out ca.crt -days 3650 -subj "/CN=Lab CA" 2>/dev/null
mkdir -p certs
for spec in shop:90 api:20 admin:5; do
n=${spec%%:*}; d=${spec##*:}
openssl req -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout certs/$n.key -subj "/CN=$n.example.test" 2>/dev/null |
openssl x509 -req -CA ca.crt -CAkey ca.key -set_serial 0x10$d -days $d -out certs/$n.crt 2>/dev/null
done
cat > check_certs.sh <<'EOF'
#!/usr/bin/env bash
# usage: check_certs.sh DIR [WARN_DAYS] [CRIT_DAYS]
set -euo pipefail
dir=$1; warn=${2:-30}; crit=${3:-7}; status=0
now=$(date +%s)
for f in "$dir"/*.crt; do
end=$(date -d "$(openssl x509 -in "$f" -noout -enddate | cut -d= -f2)" +%s)
days=$(( (end - now + 43200) / 86400 )) # rounded to whole days
cn=$(openssl x509 -in "$f" -noout -subject -nameopt multiline | sed -n 's/ *commonName *= //p')
if (( days < crit )); then state=CRITICAL; (( status < 2 )) && status=2
elif (( days < warn )); then state=WARNING; (( status < 1 )) && status=1
else state=OK; fi
printf '%-8s %-18s %3d days left\n' "$state" "$cn" "$days"
done
exit $status
EOF
chmod +x check_certs.sh
shellcheck check_certs.sh && echo "shellcheck: clean"
./check_certs.sh certs; echo "exit code: $?"shellcheck: clean
CRITICAL admin.example.test 5 days left
WARNING api.example.test 20 days left
OK shop.example.test 90 days left
exit code: 2
On a server, run it from a systemd timer daily and route a non-zero exit to your alerting, or let a monitoring agent run it as a check. Also check fullchain.pem symlinks under /etc/letsencrypt/live/, not old copies lying around.
Checking what is actually served
A renewed file on disk doesn't help if the proxy still serves the old certificate from memory. Check the endpoint, through the same name and port clients use:
openssl req -x509 -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout ca.key -out ca.crt -days 3650 -subj "/CN=Lab CA" 2>/dev/null
openssl req -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout old.key -subj "/CN=shop.example.test" 2>/dev/null |
openssl x509 -req -CA ca.crt -CAkey ca.key -set_serial 1 -days 10 -out served.crt -extfile <(echo subjectAltName=DNS:shop.example.test) 2>/dev/null
cp old.key served.key
printf 'pid nginx.pid;\nerror_log stderr;\nevents {}\nhttp { server { listen 127.0.0.1:8443 ssl; ssl_certificate served.crt; ssl_certificate_key served.key; return 200; } }\n' > nginx.conf
nginx -p "$PWD" -c nginx.conf -e stderr
for i in $(seq 50); do curl -sk https://127.0.0.1:8443/ > /dev/null && break; sleep 0.1; done
served() { openssl s_client -connect 127.0.0.1:8443 -servername shop.example.test < /dev/null 2>/dev/null | openssl x509 -noout -serial -checkend $((30 * 86400)) | tr '\n' ' '; echo; }
echo "before renewal: $(served)"
# "renewal": a new 90-day certificate is written to disk...
openssl req -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout served.key -subj "/CN=shop.example.test" 2>/dev/null |
openssl x509 -req -CA ca.crt -CAkey ca.key -set_serial 2 -days 90 -out served.crt -extfile <(echo subjectAltName=DNS:shop.example.test) 2>/dev/null
echo "file renewed: $(served)"
nginx -p "$PWD" -c nginx.conf -e stderr -s reload 2>/dev/null; sleep 0.5
echo "after reload: $(served)"
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/nullbefore renewal: serial=01 Certificate will expire
file renewed: serial=01 Certificate will expire
after reload: serial=02 Certificate will not expire
Until the reload, clients kept getting serial 01, the certificate with 10 days left, although the file on disk was already renewed. That is exactly the failure a deploy hook prevents, and exactly what an endpoint check catches when the hook is missing.
At scale
# Sample: Prometheus blackbox exporter + an alert on the served certificate (see the Observability course)
groups:
- name: tls
rules:
- alert: TLSCertificateExpiresSoon
expr: probe_ssl_earliest_cert_expiry - time() < 14 * 86400
for: 1h
labels: { severity: warning }
annotations:
summary: "{{ $labels.instance }}: certificate expires in {{ $value | humanizeDuration }}"probe_ssl_earliest_cert_expiry is the earliest notAfter in the chain the blackbox exporter saw, so an expiring intermediate is caught too. Two more sources of truth: Certificate Transparency logs (search your domain on crt.sh) list every public certificate issued for it, including ones issued outside your automation; and your CA's expiry e-mails, which Let's Encrypt stopped sending in 2025, so don't rely on them.
Test yourself
openssl x509 -in cert.pem -noout -checkend 2592000 exits with 1. What does that mean?
- The file is invalid
- The certificate will have expired within 30 days (2,592,000 seconds), or already has
- It is valid for 30 more days
- The chain is incomplete
The certificate file on disk was renewed yesterday, yet the endpoint check reports it expiring in 3 days. Why?
- The check is wrong
- The web server still serves the old certificate from memory; it needs a reload
- DNS caching
- The CA revoked it
Why monitor expiry independently of the renewal job?
- Renewal jobs never fail
- Renewal can fail silently (blocked port, expired DNS token, missing reload); an independent check catches it while there is still time
- Monitoring renews certificates
- It is required by ACME