109 lines
2.9 KiB
Bash
109 lines
2.9 KiB
Bash
#!/usr/bin/env bash
|
|
# Vérifie l'expiration des certificats Let's Encrypt et alerte si proche.
|
|
#
|
|
# Usage (VPS) :
|
|
# export NTFY_TOPIC=tloto-certs-CHOISIS-UN-SECRET # app ntfy (téléphone)
|
|
# export ALERT_EMAIL=admin@tloto.net # optionnel si `mail` est dispo
|
|
# export WARN_DAYS=30
|
|
# ./scripts/check-ssl-expiry.sh
|
|
#
|
|
# Crontab (tous les jours à 09:00) :
|
|
# 0 9 * * * WARN_DAYS=30 NTFY_TOPIC=tloto-certs-XXX \
|
|
# /home/projects/projects_repository/games-project/scripts/check-ssl-expiry.sh \
|
|
# >> /var/log/tloto-cert-check.log 2>&1
|
|
#
|
|
set -euo pipefail
|
|
|
|
WARN_DAYS="${WARN_DAYS:-30}"
|
|
CRITICAL_DAYS="${CRITICAL_DAYS:-7}"
|
|
NTFY_TOPIC="${NTFY_TOPIC:-}"
|
|
ALERT_EMAIL="${ALERT_EMAIL:-}"
|
|
|
|
# volume:chemin_pem relatif sous /etc/letsencrypt/live/...
|
|
CHECKS=(
|
|
"games-project_letsencrypt:tloto.net/fullchain.pem"
|
|
"salon-management-full-project_letsencrypt:business-mgt.redirectme.net/fullchain.pem"
|
|
)
|
|
|
|
need_alert=0
|
|
report=""
|
|
|
|
days_left_for() {
|
|
local volume="$1"
|
|
local rel="$2"
|
|
docker run --rm -v "${volume}:/etc/letsencrypt:ro" alpine:3.20 \
|
|
sh -c "
|
|
apk add --no-cache openssl >/dev/null 2>&1
|
|
pem=/etc/letsencrypt/live/${rel}
|
|
if [ ! -f \"\$pem\" ]; then echo MISSING; exit 0; fi
|
|
end=\$(openssl x509 -in \"\$pem\" -noout -enddate | cut -d= -f2)
|
|
end_ts=\$(date -d \"\$end\" +%s)
|
|
now=\$(date +%s)
|
|
echo \$(( (end_ts - now) / 86400 ))
|
|
"
|
|
}
|
|
|
|
send_alert() {
|
|
local subject="$1"
|
|
local body="$2"
|
|
echo "ALERT: $subject"
|
|
echo "$body"
|
|
|
|
if [ -n "$NTFY_TOPIC" ]; then
|
|
curl -fsS -H "Title: $subject" -H "Priority: high" \
|
|
-d "$body" "https://ntfy.sh/${NTFY_TOPIC}" >/dev/null || true
|
|
fi
|
|
|
|
if [ -n "$ALERT_EMAIL" ] && command -v mail >/dev/null 2>&1; then
|
|
printf '%s\n' "$body" | mail -s "$subject" "$ALERT_EMAIL" || true
|
|
fi
|
|
}
|
|
|
|
for item in "${CHECKS[@]}"; do
|
|
vol="${item%%:*}"
|
|
rel="${item#*:}"
|
|
name="${rel%%/*}"
|
|
|
|
if ! docker volume inspect "$vol" >/dev/null 2>&1; then
|
|
line="SKIP $name — volume $vol introuvable"
|
|
report+="$line"$'\n'
|
|
echo "$line"
|
|
continue
|
|
fi
|
|
|
|
left="$(days_left_for "$vol" "$rel" | tr -d '\r' | tail -n1)"
|
|
if [ "$left" = "MISSING" ] || ! [[ "$left" =~ ^-?[0-9]+$ ]]; then
|
|
line="ERROR $name — certificat illisible ($left)"
|
|
report+="$line"$'\n'
|
|
echo "$line"
|
|
need_alert=1
|
|
continue
|
|
fi
|
|
|
|
line="$name : ${left} jour(s) restant(s)"
|
|
report+="$line"$'\n'
|
|
echo "$line"
|
|
|
|
if [ "$left" -lt "$CRITICAL_DAYS" ]; then
|
|
need_alert=1
|
|
elif [ "$left" -lt "$WARN_DAYS" ]; then
|
|
need_alert=1
|
|
fi
|
|
done
|
|
|
|
if [ "$need_alert" -eq 1 ]; then
|
|
send_alert "Tloto/Salon — certificat SSL à renouveler" \
|
|
"Au moins un certificat expire bientôt (< ${WARN_DAYS} jours).
|
|
|
|
${report}
|
|
Action VPS Tloto :
|
|
cd ~/projects_repository/games-project && npm run ssl:dns-renew
|
|
(Puis TXT GoDaddy, Entrée seulement quand Dig/8.8.8.8 est OK.)
|
|
|
|
Date check : $(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
exit 2
|
|
fi
|
|
|
|
echo "OK — aucun certificat sous le seuil ${WARN_DAYS} jours."
|
|
exit 0
|