netpulse-agent.init 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/sh /etc/rc.common
  2. # netpulse-agent — procd init script (OpenWrt)
  3. # Instalado por el paquete .ipk; config en /etc/config/netpulse-agent
  4. START=90
  5. USE_PROCD=1
  6. PROG=/usr/sbin/netpulse-agent
  7. HEARTBEAT_FILE=/tmp/netpulse-agent.heartbeat
  8. # Leer config UCI, con fallback a variables de entorno
  9. load_config() {
  10. local server token slug interval
  11. config_load netpulse-agent
  12. config_get server main server ""
  13. config_get token main token ""
  14. config_get slug main slug ""
  15. # Si no hay token/slug en UCI, heredar de /etc/netpulse-agent.env
  16. # (instalación manual previa a .ipk)
  17. if [ -z "$token" ] && [ -f /etc/netpulse-agent.env ]; then
  18. . /etc/netpulse-agent.env
  19. fi
  20. if [ -z "$server" ]; then
  21. server="$NETPULSE_SERVER"
  22. fi
  23. if [ -z "$token" ]; then
  24. token="$NETPULSE_TOKEN"
  25. fi
  26. if [ -z "$slug" ]; then
  27. slug="$NETPULSE_SLUG"
  28. fi
  29. config_get interval main interval "30s"
  30. export NETPULSE_SERVER="$server"
  31. export NETPULSE_TOKEN="$token"
  32. export NETPULSE_SLUG="$slug"
  33. export NETPULSE_INTERVAL="$interval"
  34. export NETPULSE_HEARTBEAT_FILE="$HEARTBEAT_FILE"
  35. # Intervalo entre scans de vecinos (#699): "0" los desactiva (solo
  36. # on-demand). Vacío = default del agente (30m).
  37. local scan_interval
  38. config_get scan_interval main scan_interval ""
  39. [ -n "$scan_interval" ] && export NETPULSE_SCAN_INTERVAL="$scan_interval"
  40. # Targets de ping (solo se usan si están definidos)
  41. local wan_target gw_target
  42. config_get wan_target main wan_target ""
  43. config_get gw_target main gw_target ""
  44. [ -n "$wan_target" ] && export NETPULSE_WAN_TARGET="$wan_target"
  45. [ -n "$gw_target" ] && export NETPULSE_GW_TARGET="$gw_target"
  46. # Fingerprint SPKI del servidor (obligatorio si server es https://)
  47. local server_fp
  48. config_get server_fp main server_fp ""
  49. [ -n "$server_fp" ] && export NETPULSE_SERVER_FP="$server_fp"
  50. }
  51. # (#879) self-heal: tras un sysupgrade /etc sobrevive pero el binario del
  52. # watchdog no: si quedó la entrada de cron del antiguo watchdog, retirarla
  53. # (cron loguearía un comando inexistente cada 2 min).
  54. cleanup_stale_watchdog_cron() {
  55. [ -f /usr/sbin/netpulse-watchdog ] && return 0
  56. crontab -l 2>/dev/null | grep -q netpulse-watchdog || return 0
  57. ( crontab -l 2>/dev/null | grep -v netpulse-watchdog ) | crontab - 2>/dev/null || true
  58. logger -t netpulse-agent "watchdog cron huérfano retirado (#879)"
  59. }
  60. start_service() {
  61. cleanup_stale_watchdog_cron
  62. load_config
  63. # Validación mínima
  64. if [ -z "$NETPULSE_SERVER" ] || [ -z "$NETPULSE_TOKEN" ] || [ -z "$NETPULSE_SLUG" ]; then
  65. echo "netpulse-agent: falta server, token o slug en /etc/config/netpulse-agent"
  66. return 1
  67. fi
  68. procd_open_instance
  69. procd_set_param command "$PROG"
  70. # #1772: procd lanza el binario desde su propio entorno, asi que los
  71. # export de load_config no llegan al proceso. Una sola llamada env con
  72. # todas las variables (varias llamadas se pisan entre si en 21.02).
  73. procd_set_param env \
  74. NETPULSE_SERVER="$NETPULSE_SERVER" \
  75. NETPULSE_TOKEN="$NETPULSE_TOKEN" \
  76. NETPULSE_SLUG="$NETPULSE_SLUG" \
  77. NETPULSE_INTERVAL="$NETPULSE_INTERVAL" \
  78. NETPULSE_HEARTBEAT_FILE="$NETPULSE_HEARTBEAT_FILE" \
  79. NETPULSE_SCAN_INTERVAL="$NETPULSE_SCAN_INTERVAL" \
  80. NETPULSE_WAN_TARGET="$NETPULSE_WAN_TARGET" \
  81. NETPULSE_GW_TARGET="$NETPULSE_GW_TARGET" \
  82. NETPULSE_SERVER_FP="$NETPULSE_SERVER_FP"
  83. # Booleanos explícitos: "procd_set_param stdout stderr" no es válido y en
  84. # OpenWrt 21.02 deja los logs sin redirigir a syslog (#1701).
  85. procd_set_param stdout 1
  86. procd_set_param stderr 1
  87. procd_set_param respawn 3600 3600 0 # respawn con backoff de 1h (#851: ya sin watchdog cron)
  88. procd_close_instance
  89. }
  90. service_triggers() {
  91. procd_add_reload_trigger netpulse-agent
  92. }
  93. # stop_service lo gestiona procd automáticamente (SIGTERM al comando)