#!/bin/bash # ================================================ # SENTINELLA DI BACKUP (Gira su Pi-1 Master) # Controlla Pi-2 e attiva failover bundle IoT su Pi-1 se down. # ================================================ LOOGLE_NOTIFY="/home/daniely/docker/loogle-casa/scripts/loogle-notify.sh" HA_FAILOVER="/home/daniely/rete/scripts/ha-failover.sh" if [[ ! -x "$LOOGLE_NOTIFY" ]]; then echo "Errore: loogle-notify non disponibile ($LOOGLE_NOTIFY)" >&2 exit 1 fi TARGET_IP="192.168.128.81" TARGET_NAME="🍓 Pi-2 (Backup & Monitor)" STATE_FILE="/mnt/ha-apps/.metadata/pi2-watchdog.state" SIM_STATE="/mnt/ha-apps/.metadata/failover-sim.state" LEGACY_STATE_FILE="/tmp/pi2_watchdog.state" SSH_OPTS=(-o ConnectTimeout=5 -o BatchMode=yes -o StrictHostKeyChecking=accept-new) if [[ -f "$SIM_STATE" ]] && [[ "$(cat "$SIM_STATE" 2>/dev/null)" != "none" ]]; then exit 0 fi send_alert() { local title="$1" local body="$2" local severity="$3" "$LOOGLE_NOTIFY" --title "$title" --body "$body" --category watchdog_pi2 \ --severity "$severity" & } pi2_reachable() { ping -c 3 -W 2 "$TARGET_IP" > /dev/null 2>&1 && return 0 ssh "${SSH_OPTS[@]}" pi2 "exit 0" 2>/dev/null } if [ ! -f "$STATE_FILE" ]; then if [ -f "$LEGACY_STATE_FILE" ]; then cp "$LEGACY_STATE_FILE" "$STATE_FILE" else echo "UP" > "$STATE_FILE" fi fi LAST_STATE=$(cat "$STATE_FILE") if pi2_reachable; then if [ "$LAST_STATE" == "DOWN" ]; then send_alert \ "Pi-2 online" \ "RISOLTO: $TARGET_NAME è tornato ONLINE! Failback bundle IoT in corso." \ "info" echo "UP" > "$STATE_FILE" if [[ -x "$HA_FAILOVER" ]]; then sudo bash -c "\"$HA_FAILOVER\" stop-iot-bundle >> /var/log/ha-failover.log 2>&1 &" fi fi else if [ "$LAST_STATE" == "UP" ]; then send_alert \ "Pi-2 offline" \ "ALLARME: $TARGET_NAME è OFFLINE! Avvio failover bundle IoT su Pi-1." \ "warning" echo "DOWN" > "$STATE_FILE" if [[ -x "$HA_FAILOVER" ]]; then sudo bash -c "\"$HA_FAILOVER\" start-iot-bundle >> /var/log/ha-failover.log 2>&1 &" fi fi fi