Files
common-server-public/certbot/check-cert.sh
张威33321 fb87be680d 0908
2026-09-08 20:36:17 +08:00

35 lines
1.0 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
#
# 查看证书状态;任一证书剩余有效期低于阈值时以非 0 退出,便于接入告警。
# 证书过期是静默事故,光有续签日志不够,必须有一个独立的检查点。
#
set -euo pipefail
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin${PATH:+:$PATH}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WARN_DAYS="${WARN_DAYS:-20}"
cd "$SCRIPT_DIR"
if ! out=$(docker compose run --rm certbot certificates 2>&1); then
echo "$out"
echo "获取证书信息失败" >&2
exit 1
fi
echo "$out"
# certbot 输出形如Expiry Date: 2026-12-07 02:41:23+00:00 (VALID: 89 days)
min="$(echo "$out" | grep -o 'VALID: [0-9]*' | awk '{print $2}' | sort -n | head -1 || true)"
if [ -z "$min" ]; then
echo "未能解析证书有效期" >&2
exit 1
fi
if [ "$min" -lt "$WARN_DAYS" ]; then
echo "警告:证书最少剩余 $min 天,低于阈值 $WARN_DAYS" >&2
exit 1
fi
echo "证书状态正常,最少剩余 $min"