| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #!/bin/sh
- # =============================================================================
- # netpulse-watchdog — Fase 5 (Plan A): auto-supervisión del agente desde el
- # propio router. Se ejecuta desde cron cada 2 minutos:
- #
- # 1. Proceso muerto → /etc/init.d/netpulse-agent restart
- # (reinicia la instancia procd y RESETA su contador de reintentos, que
- # es el hueco que deja al agente caído para siempre tras 5 crashes).
- # 2. Proceso vivo PERO heartbeat viejo (> MAX_AGE s) → restart
- # ("vivo pero roto": el proceso anda pero no consigue empujar datos
- # confirmados por el servidor).
- # 3. Proceso vivo sin fichero de heartbeat → NO hacer nada (arranque
- # reciente o backoff inicial con el servidor caído; reiniciar solo
- # resetearía el backoff sin aportar nada).
- #
- # El heartbeat lo escribe el agente (agent/internal/heartbeat) tras cada push
- # CONFIRMADO: /tmp/netpulse-agent.heartbeat (tmpfs, nada en NAND).
- #
- # Si el binario no existe (variante /tmp tras un reboot) no hace nada: la
- # reinstalación es cosa del servidor (POST /api/agents/{slug}/rearm en el
- # futuro, hoy install-agent.sh).
- # =============================================================================
- INIT=/etc/init.d/netpulse-agent
- HB=/tmp/netpulse-agent.heartbeat
- MAX_AGE=300 # 5 min sin latido = "vivo pero roto"
- # El env puede mover el heartbeat (NETPULSE_HEARTBEAT_FILE)
- [ -f /etc/netpulse-agent.env ] && . /etc/netpulse-agent.env 2>/dev/null
- [ -n "${NETPULSE_HEARTBEAT_FILE:-}" ] && HB="$NETPULSE_HEARTBEAT_FILE"
- log() { logger -t netpulse-watchdog "$*"; }
- # ¿Hay binario? Sin él no hay nada que vigilar.
- if [ ! -x /usr/sbin/netpulse-agent ] && [ ! -x /tmp/netpulse-agent ]; then
- exit 0
- fi
- # Nota: BusyBox pgrep -x compara contra la línea de comando completa,
- # no solo el nombre base; /usr/sbin/netpulse-agent no coincide con
- # netpulse-agent. Usamos pidof, que busca por comm/pidof y funciona
- # tanto en BusyBox como en procps-ng.
- if ! pidof netpulse-agent >/dev/null 2>&1; then
- log "agente no está en marcha — reiniciando servicio"
- $INIT restart >/dev/null 2>&1
- exit 0
- fi
- if [ -f "$HB" ]; then
- now=$(date +%s)
- hb=$(cat "$HB" 2>/dev/null)
- case "$hb" in
- '' | *[!0-9]*) exit 0 ;; # fichero corrupto: no actuar a ciegas
- esac
- age=$((now - hb))
- if [ "$age" -gt "$MAX_AGE" ]; then
- log "proceso vivo pero sin latido en ${age}s — reiniciando servicio"
- $INIT restart >/dev/null 2>&1
- fi
- fi
- exit 0
|