Files
Malio-ops/check_statut_recette.sh
2026-03-05 10:22:01 +01:00

148 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -u
#######################################
# Sites à vérifier
#######################################
SITES=(
"ferme.malio-dev.fr"
"sirh.malio-dev.fr"
"inventory.malio-dev.fr"
)
SCHEME="http"
CONNECT_TIMEOUT=3
MAX_TIME=8
#######################################
# Logs
#######################################
LOG_DIR="/var/log/app_health"
mkdir -p "$LOG_DIR"
LOG_FILE="${LOG_DIR}/app_health_$(date +'%Y-%m-%d').log"
#######################################
# Discord
#######################################
DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/1478379245842600007/tSxi3G6PbCn89pOdeqK34LR7c-GhXfT-lSCPolwBywJXcpa3ihL8rN4QRwsTjF6SS3w0"
discord_ping() {
local site="$1"
local status="$2"
local detail="$3"
[[ -z "${DISCORD_WEBHOOK_URL:-}" ]] && return 0
local color icon
if [[ "$status" == "OK" ]]; then
color="🟢"
icon="✅"
else
color="🔴"
icon="❌"
fi
local msg="**CHECK APP RECETTE $color**\n"
msg+="Application: ${site}\n"
msg+="Status: ${icon}\n"
msg+="Details: ${detail}"
curl -fsS -H "Content-Type: application/json" \
-d "{\"content\":\"$msg\"}" \
"$DISCORD_WEBHOOK_URL" >/dev/null || true
}
#######################################
# Logging
#######################################
log_line() {
# 2026-03-04 14:12:33 | LEVEL | site | message
printf "%s | %s | %s | %s\n" \
"$(date +'%Y-%m-%d %H:%M:%S')" "$1" "$2" "$3" | tee -a "$LOG_FILE"
# Envoi Discord par application
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 "$@"