| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- #!/usr/bin/ucode
- // luci.netpulse — plugin rpcd (protocolo exec) para luci-app-netpulse.
- //
- // Expone el estado LOCAL del agente sin abrir ningún puerto ni tocar el
- // binario del agente (criterio Fase 11: cero superficie/RAM extra):
- //
- // status → procd/pid, RSS, uptime del proceso, versión del binario,
- // heartbeat (/tmp, epoch s) y si el servicio está habilitado.
- // logs → últimas N líneas de `logread -e netpulse` (10..300).
- // restart → /etc/init.d/netpulse-agent restart (el "rearme" local).
- // test → curl al /api/health del servidor desde este router (valida
- // conectividad real: DNS, ruta, puerto, server vivo).
- //
- // rpcd lo invoca como ejecutable: `list` imprime la firma de métodos;
- // `call <método>` recibe los argumentos JSON por stdin e imprime el
- // resultado JSON. El acceso por sesión lo limita la ACL
- // /usr/share/rpcd/acl.d/luci-app-netpulse.json.
- //
- // ubus se carga con require() dentro de la función (no import top-level):
- // si el módulo no está, se degrada a `pidof` en vez de romper el plugin.
- 'use strict';
- const fs = require('fs');
- const INIT = '/etc/init.d/netpulse-agent';
- const BIN = '/usr/sbin/netpulse-agent';
- const HEARTBEAT = '/tmp/netpulse-agent.heartbeat';
- // popen_all ejecuta cmd por shell y devuelve stdout completo (o null).
- function popen_all(cmd) {
- let p = fs.popen(cmd, 'r');
- if (!p)
- return null;
- let out = p.read('all');
- p.close();
- return out;
- }
- // agent_pid: PID vía `ubus service list` (procd, fuente canónica) con
- // fallback a pidof (BusyBox) si ubus/el módulo no están disponibles.
- function agent_pid() {
- try {
- let ubus = require('ubus');
- let conn = ubus.connect();
- if (conn) {
- let res = conn.call('service', 'list', { name: 'netpulse-agent' });
- conn.disconnect();
- let inst = res?.['netpulse-agent']?.instances;
- for (let name in inst) {
- let i = inst[name];
- if (i?.running && int(i.pid) > 0)
- return int(i.pid);
- }
- }
- }
- catch (e) {
- // sin ubus: caemos a pidof
- }
- let out = popen_all('pidof netpulse-agent 2>/dev/null');
- if (out) {
- let pid = int(split(trim(out), ' ')[0]);
- if (pid > 0)
- return pid;
- }
- return 0;
- }
- // proc_rss_kb: VmRSS del proceso en KiB (null si no legible).
- function proc_rss_kb(pid) {
- let st = fs.readfile(sprintf('/proc/%d/status', pid), 8192);
- if (!st)
- return null;
- let m = match(st, /VmRSS:[ \t]+([0-9]+)[ \t]+kB/);
- return m ? int(m[1]) : null;
- }
- // proc_uptime_s: segundos de vida del proceso — starttime (campo 22 de
- // /proc/pid/stat, en ticks de 100 Hz) contra /proc/uptime. El comm puede
- // llevar espacios/paréntesis: se parte por el ÚLTIMO ')'.
- function proc_uptime_s(pid) {
- let stat = fs.readfile(sprintf('/proc/%d/stat', pid), 2048);
- let up = fs.readfile('/proc/uptime', 128);
- if (!stat || !up)
- return null;
- let rp = rindex(stat, ')');
- if (rp < 0)
- return null;
- let f = split(trim(substr(stat, rp + 2)), ' ');
- // f[0] = state (campo 3) → starttime (campo 22) = f[19]
- let start_ticks = int(f[19]);
- let boot_s = int(split(trim(up), '.')[0]);
- if (start_ticks <= 0 || boot_s <= 0)
- return null;
- let secs = int(boot_s - start_ticks / 100);
- return secs >= 0 ? secs : 0;
- }
- function do_status() {
- let pid = agent_pid();
- let running = pid > 0;
- let res = {
- installed: !!fs.access(BIN),
- enabled: system([ INIT, 'enabled' ], 5000) == 0,
- running: running,
- pid: running ? pid : null,
- rss_kb: running ? proc_rss_kb(pid) : null,
- uptime_s: running ? proc_uptime_s(pid) : null,
- version: null,
- heartbeat_ts: null,
- heartbeat_age_s: null
- };
- if (res.installed) {
- let v = popen_all(BIN + ' --version 2>/dev/null');
- if (v && trim(v) != '')
- res.version = split(trim(v), '\n')[0];
- }
- // Heartbeat: epoch en segundos escrito por el agente tras cada push
- // (agent/internal/heartbeat). Sin fichero (reboot reciente) → null.
- let hb = fs.readfile(HEARTBEAT, 64);
- if (hb) {
- let ts = int(trim(hb));
- if (ts > 0) {
- res.heartbeat_ts = ts;
- let age = time() - ts;
- res.heartbeat_age_s = age >= 0 ? age : 0;
- }
- }
- return res;
- }
- function do_logs(args) {
- let n = int(args?.lines ?? 50);
- if (n < 10)
- n = 10;
- if (n > 300)
- n = 300;
- // Leemos logread completo y procesamos en memoria (ucode nativo,
- // cero shell string y cero pipes). En un router doméstico los logs
- // de netpulse son de pocos KB: split/slice/join es más limpio.
- let out = popen_all('logread -e netpulse 2>/dev/null');
- if (!out)
- return { lines: '' };
- let lines = split(out, '\n');
- if (length(lines) > 0 && lines[length(lines) - 1] === '')
- lines = slice(lines, 0, -1);
- let start = length(lines) - n;
- if (start < 0)
- start = 0;
- return { lines: join('\n', slice(lines, start)) };
- }
- function do_restart() {
- let rc = system([ INIT, 'restart' ], 20000);
- return { ok: rc == 0, code: rc };
- }
- // do_test: valida conectividad real desde este router al servidor NetPulse.
- // Lee la URL de UCI y hace curl al /api/health (sin auth, solo reachability).
- function do_test() {
- let server = trim(popen_all('uci -q get netpulse-agent.main.server 2>/dev/null') || '');
- if (!server)
- return { ok: false, error: 'no_server' };
- // curl: -sf silencia progreso y errores HTTP; -m 5 timeout 5s;
- // -o /dev/null descarta body; -w "%{http_code}" captura el código.
- let url = server + '/api/health';
- let cmd = sprintf('curl -sf -m 5 -o /dev/null -w "%%{http_code}" %s 2>/dev/null', url);
- let code = trim(popen_all(cmd) || '');
- if (code == '200')
- return { ok: true, code: 200 };
- return { ok: false, code: code || 'unreachable' };
- }
- // ── Dispatcher del protocolo exec de rpcd ────────────────────────────────
- if (ARGV[0] == 'list') {
- printf('%J\n', { status: {}, logs: { lines: 50 }, restart: {}, test: {} });
- exit(0);
- }
- if (ARGV[0] == 'call') {
- let args = {};
- try {
- let raw = fs.stdin.read('all');
- if (raw && trim(raw) != '')
- args = json(raw);
- }
- catch (e) {
- args = {};
- }
- if (ARGV[1] == 'status') {
- printf('%J\n', do_status());
- exit(0);
- }
- if (ARGV[1] == 'logs') {
- printf('%J\n', do_logs(args));
- exit(0);
- }
- if (ARGV[1] == 'restart') {
- printf('%J\n', do_restart());
- exit(0);
- }
- if (ARGV[1] == 'test') {
- printf('%J\n', do_test());
- exit(0);
- }
- }
- exit(1);
|