diff --git a/dev-scripts/dashboard.sh b/dev-scripts/dashboard.sh new file mode 100755 index 0000000..61421c4 --- /dev/null +++ b/dev-scripts/dashboard.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +# dashboard.sh — lanza el TUI interactivo de gestión de bots +# Uso: ./dev-scripts/dashboard.sh + +source "$(dirname "$0")/_common.sh" + +exec "$GO" run ./cmd/dashboard "$@" diff --git a/dev-scripts/ps.sh b/dev-scripts/ps.sh new file mode 100755 index 0000000..af3423a --- /dev/null +++ b/dev-scripts/ps.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# ps.sh — muestra procesos de agentes con detalles (PID, uptime, memoria, CPU) +# +# Uso: +# ./dev-scripts/ps.sh # todos los agentes corriendo +# ./dev-scripts/ps.sh assistant-bot # uno específico + +source "$(dirname "$0")/_common.sh" + +TARGET="${1:-}" +found=0 + +# Cabecera +printf "%-22s %-8s %-12s %-10s %-8s %s\n" \ + "AGENT" "PID" "UPTIME" "MEM (RSS)" "CPU %" "LOG SIZE" +printf '%s\n' "$(printf '─%.0s' {1..78})" + +while IFS='|' read -r id _version _enabled _desc _cfg; do + [[ -n "$TARGET" && "$id" != "$TARGET" ]] && continue + + if ! is_running "$id"; then + if [[ -n "$TARGET" ]]; then + printf "%-22s ${DIM}%-8s${RST}\n" "$id" "stopped" + fi + continue + fi + + pid="$(read_pid "$id")" + ((found++)) || true + + # Uptime: calcular desde el inicio del proceso + if [[ -f /proc/$pid/stat ]]; then + start_ticks=$(awk '{print $22}' /proc/$pid/stat 2>/dev/null || echo 0) + clk_tck=$(getconf CLK_TCK) + boot_time=$(awk '/btime/{print $2}' /proc/stat) + proc_start=$((boot_time + start_ticks / clk_tck)) + now=$(date +%s) + elapsed=$((now - proc_start)) + days=$((elapsed / 86400)) + hours=$(( (elapsed % 86400) / 3600 )) + mins=$(( (elapsed % 3600) / 60 )) + if [[ $days -gt 0 ]]; then + uptime="${days}d ${hours}h" + elif [[ $hours -gt 0 ]]; then + uptime="${hours}h ${mins}m" + else + uptime="${mins}m" + fi + else + uptime="n/a" + fi + + # Memoria RSS y CPU desde ps + read -r mem_kb cpu_pct < <(ps -p "$pid" -o rss=,pcpu= 2>/dev/null || echo "0 0") + if [[ "$mem_kb" -gt 1048576 ]]; then + mem="$(( mem_kb / 1048576 )) GB" + elif [[ "$mem_kb" -gt 1024 ]]; then + mem="$(( mem_kb / 1024 )) MB" + else + mem="${mem_kb} KB" + fi + + # Tamaño del log + log="$(log_file "$id")" + if [[ -f "$log" ]]; then + log_bytes=$(stat -c%s "$log" 2>/dev/null || echo 0) + if [[ "$log_bytes" -gt 1048576 ]]; then + log_size="$(( log_bytes / 1048576 )) MB" + elif [[ "$log_bytes" -gt 1024 ]]; then + log_size="$(( log_bytes / 1024 )) KB" + else + log_size="${log_bytes} B" + fi + else + log_size="-" + fi + + printf "%-22s ${GRN}%-8s${RST} %-12s %-10s %-8s %s\n" \ + "$id" "$pid" "$uptime" "$mem" "${cpu_pct}%" "$log_size" + +done < <(list_agents_raw) + +if [[ "$found" -eq 0 ]]; then + if [[ -n "$TARGET" ]]; then + fail "Agente '$TARGET' no está corriendo." + else + dim " No hay agentes corriendo." + fi +fi diff --git a/dev-scripts/restart.sh b/dev-scripts/restart.sh new file mode 100755 index 0000000..c6f6db4 --- /dev/null +++ b/dev-scripts/restart.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# restart.sh — reinicia uno o todos los agentes +# +# Uso: +# ./dev-scripts/restart.sh # reinicia todos los habilitados +# ./dev-scripts/restart.sh assistant-bot # reinicia uno específico + +source "$(dirname "$0")/_common.sh" + +TARGET="${1:-}" + +info "Deteniendo agentes..." +"$REPO_ROOT/dev-scripts/stop.sh" ${TARGET:+"$TARGET"} + +echo "" +info "Iniciando agentes..." +"$REPO_ROOT/dev-scripts/start.sh" ${TARGET:+"$TARGET"} diff --git a/dev-scripts/server.sh b/dev-scripts/server.sh new file mode 100755 index 0000000..bbb6c41 --- /dev/null +++ b/dev-scripts/server.sh @@ -0,0 +1,121 @@ +#!/usr/bin/env bash +# server.sh — gestión unificada del servidor de bots +# +# Uso: +# ./dev-scripts/server.sh start [agent-id] # iniciar uno o todos +# ./dev-scripts/server.sh stop [agent-id] # detener uno o todos +# ./dev-scripts/server.sh restart [agent-id] # reiniciar uno o todos +# ./dev-scripts/server.sh status # resumen general del servidor +# ./dev-scripts/server.sh ps [agent-id] # procesos con detalle +# ./dev-scripts/server.sh logs [agent-id] # tail -f de logs +# ./dev-scripts/server.sh kill [agent-id] # SIGKILL forzado (emergencia) +# ./dev-scripts/server.sh dashboard # TUI interactivo + +source "$(dirname "$0")/_common.sh" + +CMD="${1:-status}" +shift || true +AGENT="${1:-}" + +case "$CMD" in + start) + exec "$REPO_ROOT/dev-scripts/start.sh" ${AGENT:+"$AGENT"} + ;; + + stop) + exec "$REPO_ROOT/dev-scripts/stop.sh" ${AGENT:+"$AGENT"} + ;; + + restart) + exec "$REPO_ROOT/dev-scripts/restart.sh" ${AGENT:+"$AGENT"} + ;; + + ps) + exec "$REPO_ROOT/dev-scripts/ps.sh" ${AGENT:+"$AGENT"} + ;; + + logs) + exec "$REPO_ROOT/dev-scripts/logs.sh" ${AGENT:+"$AGENT"} + ;; + + dashboard|tui) + exec "$REPO_ROOT/dev-scripts/dashboard.sh" + ;; + + kill) + # SIGKILL forzado para emergencias + if [[ -n "$AGENT" ]]; then + agents=("$AGENT") + else + agents=() + while IFS='|' read -r id _v _e _d _c; do + agents+=("$id") + done < <(list_agents_raw) + fi + + killed=0 + for id in "${agents[@]}"; do + pid="$(read_pid "$id")" + if [[ "$pid" -gt 0 ]] && kill -0 "$pid" 2>/dev/null; then + kill -9 "$pid" 2>/dev/null || true + rm -f "$(pid_file "$id")" + ok "$id killed (PID $pid)" + ((killed++)) || true + else + dim " $id (no estaba corriendo)" + fi + done + [[ "$killed" -eq 0 ]] && dim "Ningún proceso eliminado." + ;; + + status) + # Resumen general del servidor + total=0 + running=0 + stopped=0 + disabled=0 + + while IFS='|' read -r id _version enabled _desc _cfg; do + ((total++)) || true + st=$(agent_status "$id" "$enabled") + case "$st" in + running) ((running++)) || true ;; + stopped) ((stopped++)) || true ;; + disabled) ((disabled++)) || true ;; + esac + done < <(list_agents_raw) + + echo "" + echo -e " ${BLU}Bot Server Status${RST}" + printf '%s\n' " $(printf '─%.0s' {1..40})" + echo -e " Agentes totales: $total" + echo -e " ${GRN}● Running:${RST} $running" + echo -e " ${DIM}○ Stopped:${RST} $stopped" + echo -e " ${YLW} Disabled:${RST} $disabled" + echo "" + + # Mostrar tabla de agentes + "$REPO_ROOT/dev-scripts/list.sh" + + # Si hay agentes corriendo, mostrar uso de recursos + if [[ "$running" -gt 0 ]]; then + echo "" + "$REPO_ROOT/dev-scripts/ps.sh" + fi + ;; + + *) + echo "Uso: $0 {start|stop|restart|status|ps|logs|kill|dashboard} [agent-id]" + echo "" + echo "Comandos:" + echo " start [id] Iniciar uno o todos los agentes habilitados" + echo " stop [id] Detener uno o todos los agentes" + echo " restart [id] Reiniciar uno o todos los agentes" + echo " status Resumen general del servidor" + echo " ps [id] Procesos corriendo con detalle (PID, mem, CPU)" + echo " logs [id] Tail -f de logs" + echo " kill [id] SIGKILL forzado (solo emergencias)" + echo " dashboard TUI interactivo de gestión" + exit 1 + ;; +esac