feat : rebuild-bdd-recette

This commit is contained in:
AkiNoKure
2026-03-11 11:14:44 +01:00
parent 5729d0d484
commit 9d4a5050e9
2 changed files with 580 additions and 42 deletions

View File

@@ -9,11 +9,11 @@ set -uo pipefail
#
# Fonctionnement global :
# 1. charge la configuration depuis le fichier .env ;
# 2. vérifie que le DNS du site est résolu ;
# 2. vérifie le DNS de chaque application ;
# 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.
# 4. écrit le résultat dans un fichier de log local ;
# 5. construit un message récapitulatif unique ;
# 6. envoie une seule notification Discord avec tous les statuts.
###############################################################################
#######################################
@@ -68,34 +68,12 @@ LOG_FILE="${LOG_DIR}/app_health_$(date +'%Y-%m-%d').log"
DISCORD_WEBHOOK_URL="${DISCORD_WEBHOOK_URL:-}"
DISCORD_PING="${DISCORD_PING:-@here}"
discord_ping() {
local site="$1"
local status="$2"
local detail="$3"
#######################################
# Variables globales de synthèse
#######################################
[[ -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
}
SUMMARY_LINES=()
FAILURES=0
#######################################
# Logging
@@ -104,8 +82,6 @@ discord_ping() {
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"
}
#######################################
@@ -116,22 +92,72 @@ dns_ok() {
getent hosts "$1" >/dev/null 2>&1
}
#######################################
# Ajout au résumé Discord
#######################################
add_summary_line() {
local site="$1"
local status="$2"
local detail="$3"
local icon
if [[ "$status" == "OK" ]]; then
icon="✅"
else
icon="❌"
fi
SUMMARY_LINES+=("${icon} ${site} : ${detail}")
}
#######################################
# Envoi du message Discord récapitulatif
#######################################
send_discord_summary() {
[[ -z "${DISCORD_WEBHOOK_URL:-}" ]] && return 0
local header_icon ping_prefix=""
if [[ "$FAILURES" -eq 0 ]]; then
header_icon="🟢"
else
header_icon="🔴"
ping_prefix="${DISCORD_PING} "
fi
local msg="**${ping_prefix}CHECK APP ${ENV_NAME} ${header_icon}**"
msg+="\n"
local line
for line in "${SUMMARY_LINES[@]}"; do
msg+="\n${line}"
done
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
}
#######################################
# 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)"
add_summary_line "$host" "DOWN" "DOWN - DNS"
return 1
fi
local http_code curl_exit stderr
local http_code curl_exit err
local stderr
stderr="$(mktemp)"
http_code="$(
@@ -141,31 +167,33 @@ check_site() {
--max-time "$MAX_TIME" \
"$url" 2>"$stderr"
)"
curl_exit=$?
if [ $curl_exit -ne 0 ]; then
local err
if [[ "$curl_exit" -ne 0 ]]; then
err="$(head -n 1 "$stderr" | tr -d '\r')"
rm -f "$stderr"
log_line "DOWN" "$host" "curl exit=$curl_exit : ${err:-"(aucun)"}"
add_summary_line "$host" "DOWN" "DOWN - curl"
return 1
fi
rm -f "$stderr"
if [[ "$http_code" =~ ^[0-9]{3}$ ]]; then
if [ "$http_code" -ge 200 ] && [ "$http_code" -le 399 ]; then
if [[ "$http_code" -ge 200 && "$http_code" -le 399 ]]; then
log_line "OK" "$host" "HTTP $http_code"
add_summary_line "$host" "OK" "OK"
return 0
fi
log_line "DOWN" "$host" "HTTP $http_code (erreur appli)"
add_summary_line "$host" "DOWN" "DOWN - HTTP $http_code"
return 1
fi
log_line "DOWN" "$host" "Code HTTP inattendu: $http_code"
add_summary_line "$host" "DOWN" "DOWN - code HTTP invalide"
return 1
}
@@ -174,7 +202,6 @@ check_site() {
#######################################
main() {
local failures=0
for site in "${SITES[@]}"; do
@@ -183,7 +210,10 @@ main() {
fi
done
if [ "$failures" -gt 0 ]; then
FAILURES="$failures"
send_discord_summary
if [[ "$failures" -gt 0 ]]; then
exit 2
fi