Files
Malio-ops/RecetteScripts/check-statut-recette.sh
2026-03-10 15:54:22 +01:00

193 lines
4.3 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
set -uo pipefail
###############################################################################
# check-statut-recette.sh
#
# Ce script vérifie la disponibilité de plusieurs applications web définies
# dans le fichier .env.
#
# Fonctionnement global :
# 1. charge la configuration depuis le fichier .env ;
# 2. vérifie que le DNS du site est résolu ;
# 3. effectue une requête HTTP avec curl ;
# 4. analyse le code HTTP retourné ;
# 5. écrit le résultat dans un fichier de log local ;
# 6. envoie une notification Discord avec létat du service.
###############################################################################
#######################################
# Chargement du .env
#######################################
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="${SCRIPT_DIR}/.env"
if [[ ! -f "$ENV_FILE" ]]; then
echo "ERROR: fichier .env introuvable : $ENV_FILE" >&2
exit 1
fi
set -a
# shellcheck disable=SC1090
source "$ENV_FILE"
set +a
#######################################
# Vérification des variables requises
#######################################
: "${ENV_NAME:?Variable ENV_NAME manquante}"
: "${APP_LOG_DIR:?Variable APP_LOG_DIR manquante}"
: "${CHECK_CONNECT_TIMEOUT:?Variable CHECK_CONNECT_TIMEOUT manquante}"
: "${CHECK_MAX_TIME:?Variable CHECK_MAX_TIME manquante}"
: "${APP_URLS:?Variable APP_URLS manquante}"
#######################################
# Sites à vérifier
#######################################
read -r -a SITES <<< "$APP_URLS"
SCHEME="http"
CONNECT_TIMEOUT="${CHECK_CONNECT_TIMEOUT}"
MAX_TIME="${CHECK_MAX_TIME}"
#######################################
# Logs
#######################################
LOG_DIR="${APP_LOG_DIR}"
mkdir -p "$LOG_DIR"
LOG_FILE="${LOG_DIR}/app_health_$(date +'%Y-%m-%d').log"
#######################################
# Discord
#######################################
DISCORD_WEBHOOK_URL="${DISCORD_WEBHOOK_URL:-}"
DISCORD_PING="${DISCORD_PING:-@here}"
discord_ping() {
local site="$1"
local status="$2"
local detail="$3"
[[ -z "${DISCORD_WEBHOOK_URL:-}" ]] && return 0
local color icon ping_prefix=""
if [[ "$status" == "OK" ]]; then
color="🟢"
icon="✅"
else
color="🔴"
icon="❌"
ping_prefix="${DISCORD_PING} "
fi
local msg="**${ping_prefix}CHECK APP ${ENV_NAME} $color**\n"
msg+="Application: ${site}\n"
msg+="Details: ${detail}"
local payload
payload="$(jq -n --arg content "$msg" '{content: $content}')"
curl -fsS -H "Content-Type: application/json" \
-d "$payload" \
"$DISCORD_WEBHOOK_URL" >/dev/null || true
}
#######################################
# Logging
#######################################
log_line() {
printf "%s | %s | %s | %s\n" \
"$(date +'%Y-%m-%d %H:%M:%S')" "$1" "$2" "$3" | tee -a "$LOG_FILE"
discord_ping "$2" "$1" "$3"
}
#######################################
# DNS
#######################################
dns_ok() {
getent hosts "$1" >/dev/null 2>&1
}
#######################################
# Check application
#######################################
check_site() {
local host="$1"
local url="${SCHEME}://${host}/"
if ! dns_ok "$host"; then
log_line "DOWN" "$host" "Résolution impossible (getent hosts)"
return 1
fi
local http_code curl_exit stderr
stderr="$(mktemp)"
http_code="$(
curl -sS -o /dev/null \
-w '%{http_code}' \
--connect-timeout "$CONNECT_TIMEOUT" \
--max-time "$MAX_TIME" \
"$url" 2>"$stderr"
)"
curl_exit=$?
if [ $curl_exit -ne 0 ]; then
local err
err="$(head -n 1 "$stderr" | tr -d '\r')"
rm -f "$stderr"
log_line "DOWN" "$host" "curl exit=$curl_exit : ${err:-"(aucun)"}"
return 1
fi
rm -f "$stderr"
if [[ "$http_code" =~ ^[0-9]{3}$ ]]; then
if [ "$http_code" -ge 200 ] && [ "$http_code" -le 399 ]; then
log_line "OK" "$host" "HTTP $http_code"
return 0
fi
log_line "DOWN" "$host" "HTTP $http_code (erreur appli)"
return 1
fi
log_line "DOWN" "$host" "Code HTTP inattendu: $http_code"
return 1
}
#######################################
# Main
#######################################
main() {
local failures=0
for site in "${SITES[@]}"; do
if ! check_site "$site"; then
failures=$((failures + 1))
fi
done
if [ "$failures" -gt 0 ]; then
exit 2
fi
exit 0
}
main "$@"