4
0

netpulse-agent.init 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "${NETPULSE_SERVER%%:*}" in
  30. https|HTTPS) ;;
  31. *)
  32. [ "${NETPULSE_ALLOW_INSECURE_COMMANDS:-}" = "1" ] || {
  33. logger -t netpulse-agent "self-heal: HTTP without NETPULSE_ALLOW_INSECURE_COMMANDS=1, binary not downloaded"
  34. return 1
  35. }
  36. ;;
  37. esac
  38. case "$(uname -m)" in
  39. aarch64|arm64) local ARCH=arm64 ;;
  40. armv7l|armv7|armhf|arm) local ARCH=arm ;;
  41. x86_64|amd64) local ARCH=amd64 ;;
  42. mips)
  43. case "$(head -c 6 /bin/sh | tail -c 1 | tr '\001\002' '12')" in
  44. 1) local ARCH=mipsle ;;
  45. 2) local ARCH=mips ;;
  46. *) return 1 ;;
  47. esac ;;
  48. *) return 1 ;;
  49. esac
  50. local url tmp hdr digest sum
  51. url="${NETPULSE_SERVER%/}/api/agents/${NETPULSE_SLUG}/binary?arch=${ARCH}"
  52. logger -t netpulse-agent "self-heal: binary missing, downloading from $NETPULSE_SERVER"
  53. tmp=/tmp/netpulse-agent.$$
  54. hdr=/tmp/netpulse-agent.hdr.$$
  55. if command -v curl >/dev/null 2>&1; then
  56. curl -fsSL -m 120 -D "$hdr" -H "Authorization: Bearer $NETPULSE_TOKEN" -o "$tmp" "$url" || { rm -f "$tmp" "$hdr"; return 1; }
  57. else
  58. wget -S -T 60 -O "$tmp" --header="Authorization: Bearer $NETPULSE_TOKEN" "$url" 2>"$hdr" || { rm -f "$tmp" "$hdr"; return 1; }
  59. fi
  60. digest=$(grep -i '^[[:space:]]*x-checksum-sha256:' "$hdr" 2>/dev/null | tail -n 1 | tr -d '\r' | awk '{print $2}')
  61. rm -f "$hdr"
  62. case "$digest" in
  63. ""|*[!0123456789abcdefABCDEF]*)
  64. logger -t netpulse-agent "self-heal: no valid X-Checksum-Sha256, binary discarded"
  65. rm -f "$tmp"
  66. return 1 ;;
  67. esac
  68. [ "${#digest}" -eq 64 ] || { logger -t netpulse-agent "self-heal: digest with invalid length, binary discarded"; rm -f "$tmp"; return 1; }
  69. sum=$(sha256sum "$tmp" 2>/dev/null | awk '{print $1}')
  70. if [ -z "$sum" ] || [ "$(printf '%s' "$sum" | tr 'A-F' 'a-f')" != "$(printf '%s' "$digest" | tr 'A-F' 'a-f')" ]; then
  71. logger -t netpulse-agent "self-heal: SHA-256 mismatch, binary discarded"
  72. rm -f "$tmp"
  73. return 1
  74. fi
  75. chmod 0755 "$tmp" && mv "$tmp" /usr/sbin/netpulse-agent || { rm -f "$tmp"; return 1; }
  76. logger -t netpulse-agent "self-heal: binary restored"
  77. BIN=/usr/sbin/netpulse-agent
  78. return 0
  79. }
  80. # (#879) self-heal: tras un sysupgrade /etc sobrevive pero el binario del
  81. # watchdog no: si quedó la entrada de cron del antiguo watchdog, retirarla
  82. # (cron loguearía un comando inexistente cada 2 min).
  83. cleanup_stale_watchdog_cron() {
  84. [ -f /usr/sbin/netpulse-watchdog ] && return 0
  85. crontab -l 2>/dev/null | grep -q netpulse-watchdog || return 0
  86. ( crontab -l 2>/dev/null | grep -v netpulse-watchdog ) | crontab - 2>/dev/null || true
  87. logger -t netpulse-agent "orphan watchdog cron entry removed (#879)"
  88. }
  89. start_service() {
  90. cleanup_stale_watchdog_cron
  91. selfheal_binary || logger -t netpulse-agent "self-heal: could not restore the binary"
  92. procd_open_instance netpulse-agent
  93. procd_set_param command "$BIN"
  94. # respawn: si muere, procd lo levanta (5 s entre intentos, 5 intentos/hora)
  95. procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
  96. procd_set_param stdout 1
  97. procd_set_param stderr 1
  98. procd_set_param file /etc/netpulse-agent.env
  99. procd_close_instance
  100. }