From 41a1075b913886aca374cf8363fd46e7b9f01ce0 Mon Sep 17 00:00:00 2001 From: Egutierrez Date: Sun, 7 Jun 2026 14:25:45 +0200 Subject: [PATCH] =?UTF-8?q?feat(hub):=20deploy=5Fagent=5Ftermux.sh=20(runi?= =?UTF-8?q?t+Termux:Boot+wake-lock,=20helpers=20shell=20battery/logcat),?= =?UTF-8?q?=20panels=20de=20bater=C3=ADa=20+=20logs=20unificados=20en=20no?= =?UTF-8?q?de=20detail?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hub/dashboards/fleet-node.json | 29 ++++++++- hub/deploy_agent_termux.sh | 105 +++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 3 deletions(-) create mode 100755 hub/deploy_agent_termux.sh diff --git a/hub/dashboards/fleet-node.json b/hub/dashboards/fleet-node.json index 16f69c2..d8d664c 100644 --- a/hub/dashboards/fleet-node.json +++ b/hub/dashboards/fleet-node.json @@ -161,13 +161,36 @@ "fieldConfig": { "defaults": {}, "overrides": [ { "matcher": { "id": "byName", "options": "RAM %" }, "properties": [ { "id": "unit", "value": "percent" }, { "id": "custom.cellOptions", "value": { "type": "gauge" } }, { "id": "max", "value": 100 } ] } ] }, "options": { "sortBy": [ { "displayName": "RAM %", "desc": true } ] } }, + { + "id": 14, + "type": "gauge", + "title": "Batería % (móviles)", + "gridPos": { "h": 6, "w": 8, "x": 0, "y": 41 }, + "datasource": { "type": "prometheus", "uid": "victoriametrics" }, + "targets": [ { "refId": "A", "expr": "node_battery_percent{instance=\"$node\"}", "datasource": { "type": "prometheus", "uid": "victoriametrics" } } ], + "fieldConfig": { "defaults": { "unit": "percent", "min": 0, "max": 100, "thresholds": { "mode": "absolute", "steps": [ { "color": "red", "value": null }, { "color": "yellow", "value": 20 }, { "color": "green", "value": 50 } ] } }, "overrides": [] }, + "options": { "reduceOptions": { "calcs": ["lastNotNull"], "fields": "", "values": false } } + }, + { + "id": 15, + "type": "timeseries", + "title": "Batería: nivel y temperatura", + "gridPos": { "h": 6, "w": 16, "x": 8, "y": 41 }, + "datasource": { "type": "prometheus", "uid": "victoriametrics" }, + "targets": [ + { "refId": "A", "expr": "node_battery_percent{instance=\"$node\"}", "legendFormat": "nivel %", "datasource": { "type": "prometheus", "uid": "victoriametrics" } }, + { "refId": "B", "expr": "node_battery_temp_celsius{instance=\"$node\"}", "legendFormat": "temp °C", "datasource": { "type": "prometheus", "uid": "victoriametrics" } } + ], + "fieldConfig": { "defaults": { "custom": { "drawStyle": "line", "fillOpacity": 8, "showPoints": "never", "lineWidth": 2 } }, "overrides": [ { "matcher": { "id": "byName", "options": "temp °C" }, "properties": [ { "id": "unit", "value": "celsius" } ] }, { "matcher": { "id": "byName", "options": "nivel %" }, "properties": [ { "id": "unit", "value": "percent" }, { "id": "max", "value": 100 } ] } ] }, + "options": { "legend": { "displayMode": "list", "placement": "bottom" }, "tooltip": { "mode": "multi" } } + }, { "id": 13, "type": "logs", - "title": "Logs (journald)", - "gridPos": { "h": 11, "w": 24, "x": 0, "y": 41 }, + "title": "Logs (journald / logcat)", + "gridPos": { "h": 11, "w": 24, "x": 0, "y": 47 }, "datasource": { "type": "loki", "uid": "loki" }, - "targets": [ { "refId": "A", "expr": "{instance=\"$node\", job=\"journald\"}", "datasource": { "type": "loki", "uid": "loki" } } ], + "targets": [ { "refId": "A", "expr": "{instance=\"$node\"}", "datasource": { "type": "loki", "uid": "loki" } } ], "options": { "showTime": true, "wrapLogMessage": true, "prettifyLogMessage": false, "enableLogDetails": true, "dedupStrategy": "none", "sortOrder": "Descending" } } ] diff --git a/hub/deploy_agent_termux.sh b/hub/deploy_agent_termux.sh new file mode 100755 index 0000000..b23ce5d --- /dev/null +++ b/hub/deploy_agent_termux.sh @@ -0,0 +1,105 @@ +#!/usr/bin/env bash +# Despliega metrics_agent en un móvil Android/Termux como servicio supervisado +# (termux-services / runit) con arranque en boot (Termux:Boot) y wake-lock. +# +# Uso: ./deploy_agent_termux.sh +# +# Requisitos: +# - Binario en apps/metrics_agent/dist/metrics_agent_arm64 +# (CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o dist/metrics_agent_arm64 .) +# - `pass fleet/ingest-pass`. +# - En el móvil: Termux + Termux:Boot + Termux:API instalados, paquetes +# `termux-services` y `termux-api`, y un alias SSH al sshd de Termux. +set -euo pipefail + +NODE="${1:?uso: deploy_agent_termux.sh }" +HOST="${2:?uso: deploy_agent_termux.sh }" + +HUB="https://metrics-dxaqj3ina6eqd5pjt85wkrrj.organic-machine.com/api/v1/import/prometheus" +LOKI="https://logs-wmaxecsjcfnocz81d5luca92.organic-machine.com/loki/api/v1/push" +PW="$(pass show fleet/ingest-pass | head -1)" +BIN="$(cd "$(dirname "$0")/.." && pwd)/apps/metrics_agent/dist/metrics_agent_arm64" + +[ -f "$BIN" ] || { echo "ERROR: falta el binario $BIN (compílalo primero)"; exit 1; } + +echo ">> copiando binario a $HOST" +ssh -o BatchMode=yes "$HOST" 'mkdir -p ~/fleet-agent' +scp -q -o BatchMode=yes "$BIN" "$HOST:fleet-agent/metrics_agent" + +echo ">> instalando servicio Termux en $NODE ($HOST)" +ssh -o BatchMode=yes "$HOST" "NODE='$NODE' PW='$PW' HUB='$HUB' LOKI='$LOKI' bash -s" <<'OUTER' +set -e +PREFIX=/data/data/com.termux/files/usr +HM=/data/data/com.termux/files/home +chmod +x "$HM/fleet-agent/metrics_agent" + +cat > "$HM/fleet-agent/agent.json" < "$PREFIX/var/service/fleet-agent/run" <&1 +termux-wake-lock +exec $HM/fleet-agent/metrics_agent -config $HM/fleet-agent/agent.json +RUN +chmod +x "$PREFIX/var/service/fleet-agent/run" + +# Helper shell de batería: el exec de sh SÍ funciona en Termux (el bloqueado es +# el pidfd_open de Go). Escribe el JSON de termux-battery-status cada 20s. +mkdir -p "$PREFIX/var/service/fleet-battery" +cat > "$PREFIX/var/service/fleet-battery/run" <&1 +termux-wake-lock +while true; do + termux-battery-status > $HM/fleet-agent/battery.json.tmp 2>/dev/null && mv $HM/fleet-agent/battery.json.tmp $HM/fleet-agent/battery.json + sleep 20 +done +RUN +chmod +x "$PREFIX/var/service/fleet-battery/run" + +# Helper shell de logcat: vuelca el log de Android a fichero rotado (cap ~4MB). +# El agente Go lo tail-ea sin exec. +mkdir -p "$PREFIX/var/service/fleet-logcat" +cat > "$PREFIX/var/service/fleet-logcat/run" <&1 +termux-wake-lock +exec logcat -v epoch -f $HM/fleet-agent/logcat.log -r 2048 -n 2 +RUN +chmod +x "$PREFIX/var/service/fleet-logcat/run" + +# Arranque en boot (Termux:Boot): wake-lock + runsvdir vía profile. +mkdir -p "$HM/.termux/boot" +cat > "$HM/.termux/boot/start-fleet.sh" </dev/null 2>&1; then + nohup runsvdir "$PREFIX/var/service" >/dev/null 2>&1 & + sleep 6 +fi +export SVDIR="$PREFIX/var/service" +for s in fleet-logcat fleet-battery fleet-agent; do sv up "$s" 2>/dev/null || true; done +sleep 5 +for s in fleet-logcat fleet-battery fleet-agent; do echo -n "$s: "; sv status "$s" 2>/dev/null || echo "N/A"; done +OUTER + +echo ">> $NODE desplegado"