#!/bin/bash # Vérifie l'état de tous les containers Docker et alerte sur Discord (salon homelab) # si un container censé tourner (restart policy unless-stopped/always) est arrêté # de façon anormale (code de sortie non-nul) ou en boucle de redémarrage. set -uo pipefail WEBHOOK_URL="https://discord.com/api/webhooks/1466011674371821599/KPvSSS53ot4cjICfDjJcn8s0So-oQaxBui4vpciyIz4e_yL3Fm6-7GQqeTaT3G6TLQmc" problems=() while IFS='|' read -r name state status; do if [ "$state" = "restarting" ]; then problems+=("🔴 **$name** en boucle de redémarrage ($status)") continue fi if [ "$state" = "exited" ] || [ "$state" = "dead" ]; then exit_code=$(docker inspect --format '{{.State.ExitCode}}' "$name" 2>/dev/null) restart_policy=$(docker inspect --format '{{.HostConfig.RestartPolicy.Name}}' "$name" 2>/dev/null) if [ "$exit_code" != "0" ] && { [ "$restart_policy" = "unless-stopped" ] || [ "$restart_policy" = "always" ]; }; then problems+=("🔴 **$name** arrêté anormalement (code $exit_code, $status)") fi fi done < <(docker ps -a --format '{{.Names}}|{{.State}}|{{.Status}}') if [ ${#problems[@]} -eq 0 ]; then echo "OK - tous les containers vont bien" exit 0 fi message="⚠️ **Alerte containers Docker** (${#problems[@]})\n" for p in "${problems[@]}"; do message+="$p\n" done json_payload=$(python3 -c "import json,sys; print(json.dumps({'content': sys.argv[1]}))" "$message") curl -s -X POST "$WEBHOOK_URL" -H "Content-Type: application/json" -d "$json_payload" > /dev/null echo "Alerte envoyée : ${#problems[@]} problème(s)"