Interview Prep/Shell Scripting/Scenarios: health checks, monitoring and alerts

Alert when any certificate under `/etc/letsencrypt/live` expires within 21 days.

IntermediateScenarioScenarios: health checks, monitoring and alerts
Answer

Situation read → openssl x509 -checkend does the date math and returns a clean exit code.

What I'd do →

rc=0
for cert in /etc/letsencrypt/live/*/fullchain.pem; do
  [[ -e $cert ]] || continue
  if ! openssl x509 -checkend $((21 * 86400)) -noout -in "$cert" >/dev/null; then
    end=$(openssl x509 -enddate -noout -in "$cert" | cut -d= -f2)
    echo "CRIT ${cert%/fullchain.pem}: expires $end"
    rc=2
  fi
done
exit "$rc"
  1. -checkend N exits 1 if the cert expires within N seconds.
  2. Also check what the server actually serves (openssl s_client -connect host:443 -servername host), since a renewed file is useless if nginx was never reloaded.

Why → Expired certificates cause total outages and are 100% predictable.

Interview tip → Mention checking the served certificate, not only the file; renewal without reload is a common miss.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: health checks, monitoring and alerts