netpulse-agent.init 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 NO conserva los ficheros sueltos (solo conffiles
  20. # de paquetes, /etc/sysupgrade.conf y keep.d), por eso el instalador añade
  21. # binario, init y env a /etc/sysupgrade.conf (#1476). Si aun así falta el
  22. # binario al arrancar se descarga del server con el env (mismo criterio que el
  23. # reinstall del server; #851 retiró el watchdog cron, esto cubre el boot).
  24. selfheal_binary() {
  25. [ -x "$BIN" ] && return 0
  26. [ -f "$ENV_FILE" ] || return 1
  27. . "$ENV_FILE" 2>/dev/null
  28. [ -n "${NETPULSE_SERVER:-}" ] && [ -n "${NETPULSE_TOKEN:-}" ] && [ -n "${NETPULSE_SLUG:-}" ] || return 1
  29. case "$(uname -m)" in
  30. aarch64|arm64) local ARCH=arm64 ;;
  31. armv7l|armv7|armhf|arm) local ARCH=arm ;;
  32. x86_64|amd64) local ARCH=amd64 ;;
  33. mips)
  34. case "$(head -c 6 /bin/sh | tail -c 1 | tr '\001\002' '12')" in
  35. 1) local ARCH=mipsle ;;
  36. 2) local ARCH=mips ;;
  37. *) return 1 ;;
  38. esac ;;
  39. *) return 1 ;;
  40. esac
  41. local url tmp
  42. url="${NETPULSE_SERVER%/}/api/agents/${NETPULSE_SLUG}/binary?arch=${ARCH}"
  43. logger -t netpulse-agent "self-heal: binario ausente, descargando de $NETPULSE_SERVER"
  44. tmp=/tmp/netpulse-agent.$$
  45. if command -v curl >/dev/null 2>&1; then
  46. curl -fsSL -m 120 -H "Authorization: Bearer $NETPULSE_TOKEN" -o "$tmp" "$url" || return 1
  47. else
  48. wget -q -T 60 -O "$tmp" --header="Authorization: Bearer $NETPULSE_TOKEN" "$url" || return 1
  49. fi
  50. chmod 0755 "$tmp" && mv "$tmp" /usr/sbin/netpulse-agent || { rm -f "$tmp"; return 1; }
  51. logger -t netpulse-agent "self-heal: binario restaurado"
  52. BIN=/usr/sbin/netpulse-agent
  53. return 0
  54. }
  55. # (#879) self-heal: tras un sysupgrade /etc sobrevive pero el binario del
  56. # watchdog no: si quedó la entrada de cron del antiguo watchdog, retirarla
  57. # (cron loguearía un comando inexistente cada 2 min).
  58. cleanup_stale_watchdog_cron() {
  59. [ -f /usr/sbin/netpulse-watchdog ] && return 0
  60. crontab -l 2>/dev/null | grep -q netpulse-watchdog || return 0
  61. ( crontab -l 2>/dev/null | grep -v netpulse-watchdog ) | crontab - 2>/dev/null || true
  62. logger -t netpulse-agent "watchdog cron huérfano retirado (#879)"
  63. }
  64. start_service() {
  65. cleanup_stale_watchdog_cron
  66. selfheal_binary || logger -t netpulse-agent "self-heal: no se pudo restaurar el binario"
  67. procd_open_instance netpulse-agent
  68. procd_set_param command "$BIN"
  69. # respawn: si muere, procd lo levanta (5 s entre intentos, 5 intentos/hora)
  70. procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
  71. procd_set_param stdout 1
  72. procd_set_param stderr 1
  73. procd_set_param file /etc/netpulse-agent.env
  74. procd_close_instance
  75. }