| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #!/bin/sh /etc/rc.common
- # netpulse-agent — procd init script (OpenWrt)
- # Instalado por el paquete .ipk; config en /etc/config/netpulse-agent
- START=90
- USE_PROCD=1
- PROG=/usr/sbin/netpulse-agent
- HEARTBEAT_FILE=/tmp/netpulse-agent.heartbeat
- # Leer config UCI, con fallback a variables de entorno
- load_config() {
- local server token slug interval
- config_load netpulse-agent
- config_get server main server ""
- config_get token main token ""
- config_get slug main slug ""
- # Si no hay token/slug en UCI, heredar de /etc/netpulse-agent.env
- # (instalación manual previa a .ipk)
- if [ -z "$token" ] && [ -f /etc/netpulse-agent.env ]; then
- . /etc/netpulse-agent.env
- fi
- if [ -z "$server" ]; then
- server="$NETPULSE_SERVER"
- fi
- if [ -z "$token" ]; then
- token="$NETPULSE_TOKEN"
- fi
- if [ -z "$slug" ]; then
- slug="$NETPULSE_SLUG"
- fi
- config_get interval main interval "30s"
- export NETPULSE_SERVER="$server"
- export NETPULSE_TOKEN="$token"
- export NETPULSE_SLUG="$slug"
- export NETPULSE_INTERVAL="$interval"
- export NETPULSE_HEARTBEAT_FILE="$HEARTBEAT_FILE"
- # Intervalo entre scans de vecinos (#699): "0" los desactiva (solo
- # on-demand). Vacío = default del agente (30m).
- local scan_interval
- config_get scan_interval main scan_interval ""
- [ -n "$scan_interval" ] && export NETPULSE_SCAN_INTERVAL="$scan_interval"
- # Targets de ping (solo se usan si están definidos)
- local wan_target gw_target
- config_get wan_target main wan_target ""
- config_get gw_target main gw_target ""
- [ -n "$wan_target" ] && export NETPULSE_WAN_TARGET="$wan_target"
- [ -n "$gw_target" ] && export NETPULSE_GW_TARGET="$gw_target"
- # Fingerprint SPKI del servidor (obligatorio si server es https://)
- local server_fp
- config_get server_fp main server_fp ""
- [ -n "$server_fp" ] && export NETPULSE_SERVER_FP="$server_fp"
- }
- # (#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
- load_config
- # Validación mínima
- if [ -z "$NETPULSE_SERVER" ] || [ -z "$NETPULSE_TOKEN" ] || [ -z "$NETPULSE_SLUG" ]; then
- echo "netpulse-agent: falta server, token o slug en /etc/config/netpulse-agent"
- return 1
- fi
- procd_open_instance
- procd_set_param command "$PROG"
- # #1772: procd lanza el binario desde su propio entorno, asi que los
- # export de load_config no llegan al proceso. Una sola llamada env con
- # todas las variables (varias llamadas se pisan entre si en 21.02).
- procd_set_param env \
- NETPULSE_SERVER="$NETPULSE_SERVER" \
- NETPULSE_TOKEN="$NETPULSE_TOKEN" \
- NETPULSE_SLUG="$NETPULSE_SLUG" \
- NETPULSE_INTERVAL="$NETPULSE_INTERVAL" \
- NETPULSE_HEARTBEAT_FILE="$NETPULSE_HEARTBEAT_FILE" \
- NETPULSE_SCAN_INTERVAL="$NETPULSE_SCAN_INTERVAL" \
- NETPULSE_WAN_TARGET="$NETPULSE_WAN_TARGET" \
- NETPULSE_GW_TARGET="$NETPULSE_GW_TARGET" \
- NETPULSE_SERVER_FP="$NETPULSE_SERVER_FP"
- # Booleanos explícitos: "procd_set_param stdout stderr" no es válido y en
- # OpenWrt 21.02 deja los logs sin redirigir a syslog (#1701).
- procd_set_param stdout 1
- procd_set_param stderr 1
- procd_set_param respawn 3600 3600 0 # respawn con backoff de 1h (#851: ya sin watchdog cron)
- procd_close_instance
- }
- service_triggers() {
- procd_add_reload_trigger netpulse-agent
- }
- # stop_service lo gestiona procd automáticamente (SIGTERM al comando)
|