Files
admin 209f81aa3d
🚀 Deploy — JH Photomaton / 🔍 Vérification (push) Has been cancelled
🚀 Deploy — JH Photomaton / 🍓 Deploy sur le Pi (push) Has been cancelled
first commit
2026-07-16 00:55:29 +02:00

243 lines
7.9 KiB
Bash

#!/bin/bash
# =============================================================================
# health-check.sh - Surveillance de sante du photomaton
# =============================================================================
# Usage: bash health-check.sh
# Appele regulierement par le timer systemd photobooth-watchdog.timer
# =============================================================================
set -uo pipefail
LOG_TAG="photomaton-watchdog"
PHOTOBOOTH_URL="http://localhost:8083"
MAX_RESTART_ATTEMPTS=3
RESTART_COUNT_FILE="/tmp/photomaton-restart-count"
log_info() { logger -t "$LOG_TAG" "[INFO] $1"; echo "[INFO] $1"; }
log_warn() { logger -t "$LOG_TAG" "[WARN] $1"; echo "[WARN] $1"; }
log_error() { logger -t "$LOG_TAG" "[ERROR] $1"; echo "[ERROR] $1"; }
# Initialiser le compteur de redemarrage
if [ ! -f "$RESTART_COUNT_FILE" ]; then
echo "0" > "$RESTART_COUNT_FILE"
fi
restart_service() {
local service="$1"
local count
count=$(cat "$RESTART_COUNT_FILE" 2>/dev/null || echo 0)
if [ "$count" -ge "$MAX_RESTART_ATTEMPTS" ]; then
log_error "Service $service: trop de redemarrages ($count). Intervention manuelle requise."
return 1
fi
log_warn "Redemarrage de $service (tentative $((count + 1))/$MAX_RESTART_ATTEMPTS)..."
echo "$((count + 1))" > "$RESTART_COUNT_FILE"
systemctl --user restart "$service" 2>/dev/null || \
systemctl restart "$service" 2>/dev/null || true
sleep 5
}
# Reset le compteur si l'app tourne bien depuis plus de 10 minutes
reset_restart_counter() {
local uptime_check
uptime_check=$(systemctl --user show photobooth-app --property=ActiveEnterTimestamp 2>/dev/null | cut -d= -f2 || true)
if [ -n "$uptime_check" ]; then
local now
now=$(date +%s)
local started
started=$(date -d "$uptime_check" +%s 2>/dev/null || echo "$now")
local diff=$((now - started))
if [ "$diff" -gt 600 ]; then
echo "0" > "$RESTART_COUNT_FILE"
fi
fi
}
# -------------------------------------------------------------------
# 1. VERIFIER LA MEMOIRE
# -------------------------------------------------------------------
check_memory() {
local mem_available
mem_available=$(awk '/MemAvailable/ {print $2}' /proc/meminfo)
local mem_total
mem_total=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
# Alerte si moins de 100 Mo disponibles
if [ "$mem_available" -lt 102400 ]; then
log_warn "Memoire faible: ${mem_available}kB disponible sur ${mem_total}kB total"
# Liberer les caches
sync
echo 3 > /proc/sys/vm/drop_caches 2>/dev/null || true
log_info "Caches memoire vides"
# Re-verifier
mem_available=$(awk '/MemAvailable/ {print $2}' /proc/meminfo)
if [ "$mem_available" -lt 51200 ]; then
log_error "Memoire critique: ${mem_available}kB - intervention necessaire"
return 1
fi
fi
log_info "Memoire OK: ${mem_available}kB disponible"
return 0
}
# -------------------------------------------------------------------
# 2. VERIFIER LA TEMPERATURE CPU
# -------------------------------------------------------------------
check_temperature() {
local temp_file="/sys/class/thermal/thermal_zone0/temp"
if [ -f "$temp_file" ]; then
local temp
temp=$(cat "$temp_file")
local temp_c=$((temp / 1000))
if [ "$temp_c" -ge 80 ]; then
log_error "Temperature CPU critique: ${temp_c}C - throttling probable!"
return 1
elif [ "$temp_c" -ge 70 ]; then
log_warn "Temperature CPU elevee: ${temp_c}C"
else
log_info "Temperature CPU OK: ${temp_c}C"
fi
fi
return 0
}
# -------------------------------------------------------------------
# 3. VERIFIER PHOTOBOOTH-APP
# -------------------------------------------------------------------
check_photobooth() {
# Verifier si le service tourne
if ! systemctl --user is-active photobooth-app &>/dev/null; then
log_error "photobooth-app n'est pas actif!"
restart_service "photobooth-app"
return 1
fi
# Verifier si l'interface web repond
local http_code
http_code=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$PHOTOBOOTH_URL" 2>/dev/null || echo "000")
if [ "$http_code" = "000" ] || [ "$http_code" = "502" ] || [ "$http_code" = "503" ]; then
log_error "photobooth-app ne repond pas (HTTP $http_code)"
restart_service "photobooth-app"
return 1
fi
log_info "photobooth-app OK (HTTP $http_code)"
reset_restart_counter
return 0
}
# -------------------------------------------------------------------
# 4. VERIFIER CUPS / IMPRIMANTES
# -------------------------------------------------------------------
check_printers() {
if ! systemctl is-active cups &>/dev/null; then
log_error "CUPS n'est pas actif!"
systemctl restart cups 2>/dev/null || true
return 1
fi
# Verifier les imprimantes
local disabled_printers
disabled_printers=$(lpstat -p 2>/dev/null | grep -c "disabled" || echo "0")
if [ "$disabled_printers" -gt 0 ]; then
log_warn "$disabled_printers imprimante(s) desactivee(s), reactivation..."
# Reactiver les imprimantes
for printer in $(lpstat -p 2>/dev/null | grep "disabled" | awk '{print $2}'); do
cupsenable "$printer" 2>/dev/null || true
log_info "Imprimante reactivee: $printer"
done
fi
# Nettoyer les jobs bloques (plus de 10 minutes)
local stuck_jobs
stuck_jobs=$(lpstat -o 2>/dev/null | wc -l || echo "0")
if [ "$stuck_jobs" -gt 5 ]; then
log_warn "$stuck_jobs jobs d'impression en attente, nettoyage..."
cancel -a 2>/dev/null || true
fi
log_info "Imprimantes OK"
return 0
}
# -------------------------------------------------------------------
# 5. VERIFIER L'ESPACE DISQUE
# -------------------------------------------------------------------
check_disk() {
local usage
usage=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$usage" -ge 90 ]; then
log_error "Espace disque critique: ${usage}% utilise"
# Nettoyage d'urgence
# Supprimer les anciens logs
find /var/log -name "*.gz" -mtime +1 -delete 2>/dev/null || true
find /var/log -name "*.[0-9]" -mtime +1 -delete 2>/dev/null || true
# Vider le cache apt
apt-get clean 2>/dev/null || true
log_info "Nettoyage d'urgence effectue"
return 1
elif [ "$usage" -ge 80 ]; then
log_warn "Espace disque: ${usage}% utilise"
else
log_info "Espace disque OK: ${usage}% utilise"
fi
return 0
}
# -------------------------------------------------------------------
# 6. VERIFIER LA CAMERA
# -------------------------------------------------------------------
check_camera() {
# Verifier que la camera est detectee
if command -v libcamera-hello &>/dev/null; then
# Test rapide sans afficher d'image
if ! libcamera-hello --list-cameras 2>/dev/null | grep -q "Available cameras"; then
log_warn "Camera non detectee par libcamera"
return 1
fi
elif [ -e /dev/video0 ]; then
log_info "Camera detectee: /dev/video0"
else
log_warn "Aucun peripherique camera detecte"
return 1
fi
log_info "Camera OK"
return 0
}
# -------------------------------------------------------------------
# EXECUTION
# -------------------------------------------------------------------
echo "=== Health Check Photomaton - $(date) ==="
ERRORS=0
check_memory || ((ERRORS++))
check_temperature || ((ERRORS++))
check_photobooth || ((ERRORS++))
check_printers || ((ERRORS++))
check_disk || ((ERRORS++))
check_camera || ((ERRORS++))
echo ""
if [ "$ERRORS" -eq 0 ]; then
log_info "Tous les checks OK"
else
log_warn "$ERRORS probleme(s) detecte(s)"
fi
exit "$ERRORS"