| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #!/bin/sh /etc/rc.common
- # =============================================================================
- # netpulse-agent — agente nativo NetPulse para OpenWrt (SPEC-AGENTE-PILOTO §2)
- #
- # procd con respawn; logs del agente (stderr) van a syslog (logread -e
- # netpulse-agent). La config vive en /etc/netpulse-agent.env (chmod 600) —
- # procd reinicia el servicio si ese fichero cambia (procd_set_param file).
- #
- # Variante /tmp (equipos con flash justa): si el binario no está en
- # /usr/sbin se usa /tmp/netpulse-agent (RAM; hay que reinstalar tras reboot —
- # ver install-agent.sh --tmp).
- # =============================================================================
- START=99
- STOP=10
- USE_PROCD=1
- ENV_FILE=/etc/netpulse-agent.env
- BIN=/usr/sbin/netpulse-agent
- [ -x "$BIN" ] || BIN=/tmp/netpulse-agent
- # Self-heal: un sysupgrade solo conserva /etc, así que si el binario falta
- # al arrancar se descarga del server con el env (mismo criterio que el
- # reinstall del server; #851 retiró el watchdog cron, esto cubre el boot).
- selfheal_binary() {
- [ -x "$BIN" ] && return 0
- [ -f "$ENV_FILE" ] || return 1
- . "$ENV_FILE" 2>/dev/null
- [ -n "${NETPULSE_SERVER:-}" ] && [ -n "${NETPULSE_TOKEN:-}" ] && [ -n "${NETPULSE_SLUG:-}" ] || return 1
- case "$(uname -m)" in
- aarch64|arm64) local ARCH=arm64 ;;
- armv7l|armv7|armhf|arm) local ARCH=arm ;;
- x86_64|amd64) local ARCH=amd64 ;;
- mips)
- case "$(head -c 6 /bin/sh | tail -c 1 | tr '\001\002' '12')" in
- 1) local ARCH=mipsle ;;
- 2) local ARCH=mips ;;
- *) return 1 ;;
- esac ;;
- *) return 1 ;;
- esac
- local url tmp
- url="${NETPULSE_SERVER%/}/api/agents/${NETPULSE_SLUG}/binary?arch=${ARCH}"
- logger -t netpulse-agent "self-heal: binario ausente, descargando de $NETPULSE_SERVER"
- tmp=/tmp/netpulse-agent.$$
- if command -v curl >/dev/null 2>&1; then
- curl -fsSL -m 120 -H "Authorization: Bearer $NETPULSE_TOKEN" -o "$tmp" "$url" || return 1
- else
- wget -q -T 60 -O "$tmp" --header="Authorization: Bearer $NETPULSE_TOKEN" "$url" || return 1
- fi
- chmod 0755 "$tmp" && mv "$tmp" /usr/sbin/netpulse-agent || { rm -f "$tmp"; return 1; }
- logger -t netpulse-agent "self-heal: binario restaurado"
- BIN=/usr/sbin/netpulse-agent
- return 0
- }
- # (#879) self-heal: tras un sysupgrade /etc sobrevive pero el binario del
- # watchdog no: si quedó la entrada de cron del antiguo watchdog, retirarla
- # (cron loguearía un comando inexistente cada 2 min).
- cleanup_stale_watchdog_cron() {
- [ -f /usr/sbin/netpulse-watchdog ] && return 0
- crontab -l 2>/dev/null | grep -q netpulse-watchdog || return 0
- ( crontab -l 2>/dev/null | grep -v netpulse-watchdog ) | crontab - 2>/dev/null || true
- logger -t netpulse-agent "watchdog cron huérfano retirado (#879)"
- }
- start_service() {
- cleanup_stale_watchdog_cron
- selfheal_binary || logger -t netpulse-agent "self-heal: no se pudo restaurar el binario"
- procd_open_instance netpulse-agent
- procd_set_param command "$BIN"
- # respawn: si muere, procd lo levanta (5 s entre intentos, 5 intentos/hora)
- procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
- procd_set_param stdout 1
- procd_set_param stderr 1
- procd_set_param file /etc/netpulse-agent.env
- procd_close_instance
- }
|