netpulse-agent.init 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/sh /etc/rc.common
  2. # =============================================================================
  3. # netpulse-agent — agente nativo NetPulse para OpenWrt (SPEC-AGENTE-PILOTO §2)
  4. #
  5. # procd con respawn; logs del agente (stderr) van a syslog (logread -e
  6. # netpulse-agent). La config vive en /etc/netpulse-agent.env (chmod 600) —
  7. # procd reinicia el servicio si ese fichero cambia (procd_set_param file).
  8. #
  9. # Variante /tmp (equipos con flash justa): si el binario no está en
  10. # /usr/sbin se usa /tmp/netpulse-agent (RAM; hay que reinstalar tras reboot —
  11. # ver install-agent.sh --tmp).
  12. # =============================================================================
  13. START=99
  14. STOP=10
  15. USE_PROCD=1
  16. ENV_FILE=/etc/netpulse-agent.env
  17. BIN=/usr/sbin/netpulse-agent
  18. [ -x "$BIN" ] || BIN=/tmp/netpulse-agent
  19. # Self-heal: un sysupgrade solo conserva /etc, así que si el binario falta
  20. # al arrancar se descarga del server con el env (mismo criterio que el
  21. # reinstall del server; #851 retiró el watchdog cron, esto cubre el boot).
  22. selfheal_binary() {
  23. [ -x "$BIN" ] && return 0
  24. [ -f "$ENV_FILE" ] || return 1
  25. . "$ENV_FILE" 2>/dev/null
  26. [ -n "${NETPULSE_SERVER:-}" ] && [ -n "${NETPULSE_TOKEN:-}" ] && [ -n "${NETPULSE_SLUG:-}" ] || return 1
  27. case "$(uname -m)" in
  28. aarch64|arm64) local ARCH=arm64 ;;
  29. armv7l|armv7|armhf|arm) local ARCH=arm ;;
  30. x86_64|amd64) local ARCH=amd64 ;;
  31. mips)
  32. case "$(head -c 6 /bin/sh | tail -c 1 | tr '\001\002' '12')" in
  33. 1) local ARCH=mipsle ;;
  34. 2) local ARCH=mips ;;
  35. *) return 1 ;;
  36. esac ;;
  37. *) return 1 ;;
  38. esac
  39. local url tmp
  40. url="${NETPULSE_SERVER%/}/api/agents/${NETPULSE_SLUG}/binary?arch=${ARCH}"
  41. logger -t netpulse-agent "self-heal: binario ausente, descargando de $NETPULSE_SERVER"
  42. tmp=/tmp/netpulse-agent.$$
  43. if command -v curl >/dev/null 2>&1; then
  44. curl -fsSL -m 120 -H "Authorization: Bearer $NETPULSE_TOKEN" -o "$tmp" "$url" || return 1
  45. else
  46. wget -q -T 60 -O "$tmp" --header="Authorization: Bearer $NETPULSE_TOKEN" "$url" || return 1
  47. fi
  48. chmod 0755 "$tmp" && mv "$tmp" /usr/sbin/netpulse-agent || { rm -f "$tmp"; return 1; }
  49. logger -t netpulse-agent "self-heal: binario restaurado"
  50. BIN=/usr/sbin/netpulse-agent
  51. return 0
  52. }
  53. # (#879) self-heal: tras un sysupgrade /etc sobrevive pero el binario del
  54. # watchdog no: si quedó la entrada de cron del antiguo watchdog, retirarla
  55. # (cron loguearía un comando inexistente cada 2 min).
  56. cleanup_stale_watchdog_cron() {
  57. [ -f /usr/sbin/netpulse-watchdog ] && return 0
  58. crontab -l 2>/dev/null | grep -q netpulse-watchdog || return 0
  59. ( crontab -l 2>/dev/null | grep -v netpulse-watchdog ) | crontab - 2>/dev/null || true
  60. logger -t netpulse-agent "watchdog cron huérfano retirado (#879)"
  61. }
  62. start_service() {
  63. cleanup_stale_watchdog_cron
  64. selfheal_binary || logger -t netpulse-agent "self-heal: no se pudo restaurar el binario"
  65. procd_open_instance netpulse-agent
  66. procd_set_param command "$BIN"
  67. # respawn: si muere, procd lo levanta (5 s entre intentos, 5 intentos/hora)
  68. procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
  69. procd_set_param stdout 1
  70. procd_set_param stderr 1
  71. procd_set_param file /etc/netpulse-agent.env
  72. procd_close_instance
  73. }