From f087fd46f1437c1149e0e7d25205e2614b193387 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Wed, 3 Jun 2026 20:10:22 -0500 Subject: [PATCH] up --- agent/main.go | 76 +++++++++++++++++-------- resources/views/servidor_dashboard.html | 35 ++++++++---- rest/controllers/agent_controller.go | 49 +++++++++------- 3 files changed, 105 insertions(+), 55 deletions(-) diff --git a/agent/main.go b/agent/main.go index cea6c08..6d8d55d 100644 --- a/agent/main.go +++ b/agent/main.go @@ -46,16 +46,16 @@ func loadConfig(path string) (*Config, error) { // ── Métricas ────────────────────────────────────────────────────────────────── type Metricas struct { - Hostname string `json:"hostname"` - OS string `json:"os"` - Arch string `json:"arch"` - Uptime uint64 `json:"uptime_seg"` - UptimeStr string `json:"uptime"` - RAM RAMInfo `json:"ram"` - CPU CPUInfo `json:"cpu"` - Disco DiscoInfo `json:"disco"` - LoadAvg LoadInfo `json:"load_avg"` - ReportadoEn string `json:"reportado_en"` + Hostname string `json:"hostname"` + OS string `json:"os"` + Arch string `json:"arch"` + Uptime uint64 `json:"uptime_seg"` + UptimeStr string `json:"uptime"` + RAM RAMInfo `json:"ram"` + CPU CPUInfo `json:"cpu"` + Discos []DiscoInfo `json:"discos"` + LoadAvg LoadInfo `json:"load_avg"` + ReportadoEn string `json:"reportado_en"` } type RAMInfo struct { @@ -136,19 +136,47 @@ func collectMetrics() (*Metricas, error) { m.CPU.Porcentaje = round2(pcts[0]) } - // Disco (raíz /) - root := "/" - if runtime.GOOS == "windows" { - root = "C:\\" - } - if d, err := disk.Usage(root); err == nil { + // Discos: todas las particiones físicas/reales + if parts, err := disk.Partitions(false); err == nil { gb := 1024.0 * 1024 * 1024 - m.Disco = DiscoInfo{ - TotalGB: round2(float64(d.Total) / gb), - UsadoGB: round2(float64(d.Used) / gb), - LibreGB: round2(float64(d.Free) / gb), - Porcentaje: round2(d.UsedPercent), - Ruta: root, + skipFSTypes := map[string]bool{ + "proc": true, "sysfs": true, "tmpfs": true, "devtmpfs": true, + "devpts": true, "cgroup": true, "cgroup2": true, "overlay": true, + "hugetlbfs": true, "mqueue": true, "pstore": true, "securityfs": true, + "debugfs": true, "tracefs": true, "autofs": true, "fusectl": true, + "configfs": true, "efivarfs": true, "bpf": true, "rpc_pipefs": true, + "squashfs": true, "ramfs": true, "nsfs": true, "none": true, + } + for _, p := range parts { + if skipFSTypes[p.Fstype] { + continue + } + if d, err := disk.Usage(p.Mountpoint); err == nil { + m.Discos = append(m.Discos, DiscoInfo{ + TotalGB: round2(float64(d.Total) / gb), + UsadoGB: round2(float64(d.Used) / gb), + LibreGB: round2(float64(d.Free) / gb), + Porcentaje: round2(d.UsedPercent), + Ruta: p.Mountpoint, + }) + } + } + } + // Fallback: si no se encontraron particiones, usar raíz + if len(m.Discos) == 0 { + root := "/" + if runtime.GOOS == "windows" { + root = "C:\\" + } + if d, err := disk.Usage(root); err == nil { + gb := 1024.0 * 1024 * 1024 + m.Discos = []DiscoInfo{{ + TotalGB: round2(float64(d.Total) / gb), + UsadoGB: round2(float64(d.Used) / gb), + LibreGB: round2(float64(d.Free) / gb), + Porcentaje: round2(d.UsedPercent), + Ruta: root, + }} } } @@ -248,8 +276,8 @@ func main() { if err := sendHeartbeat(cfg, m); err != nil { log.Printf("⚠️ Error enviando heartbeat: %v", err) } else { - log.Printf("✅ Heartbeat enviado | RAM: %.1f%% | CPU: %.1f%% | Disco: %.1f%%", - m.RAM.Porcentaje, m.CPU.Porcentaje, m.Disco.Porcentaje) + log.Printf("✅ Heartbeat enviado | RAM: %.1f%% | CPU: %.1f%% | Discos: %d", + m.RAM.Porcentaje, m.CPU.Porcentaje, len(m.Discos)) } } diff --git a/resources/views/servidor_dashboard.html b/resources/views/servidor_dashboard.html index dc2e0c0..aecc064 100644 --- a/resources/views/servidor_dashboard.html +++ b/resources/views/servidor_dashboard.html @@ -85,18 +85,29 @@ :style="'width:' + Math.min(m.cpu?.porcentaje, 100) + '%'"> - +
- 💾 Disco - -
-
-
+ 💾 Discos +
+ +
@@ -428,9 +439,9 @@

-

Disco

-

-

+

Discos

+

+

diff --git a/rest/controllers/agent_controller.go b/rest/controllers/agent_controller.go index 16d1918..26be45e 100644 --- a/rest/controllers/agent_controller.go +++ b/rest/controllers/agent_controller.go @@ -48,28 +48,39 @@ func AgentHeartbeat(c *fiber.Ctx) error { type agentCPU struct { Nucleos int `json:"nucleos"` } - type agentDisco struct { - TotalGB float64 `json:"total_gb"` - } - type agentPayload struct { - OS string `json:"os"` - RAM agentRAM `json:"ram"` - CPU agentCPU `json:"cpu"` - Disco agentDisco `json:"disco"` - } - var m agentPayload - if jsonErr := json.Unmarshal([]byte(req.Metricas), &m); jsonErr == nil { - if m.OS != "" { - updates["so"] = m.OS + var rawMap map[string]any + if jsonErr := json.Unmarshal([]byte(req.Metricas), &rawMap); jsonErr == nil { + if os, _ := rawMap["os"].(string); os != "" { + updates["so"] = os } - if m.RAM.TotalGB > 0 { - updates["ram"] = fmt.Sprintf("%.0f GB", m.RAM.TotalGB) + if ram, _ := rawMap["ram"].(map[string]any); ram != nil { + if total, _ := ram["total_gb"].(float64); total > 0 { + updates["ram"] = fmt.Sprintf("%.0f GB", total) + } } - if m.CPU.Nucleos > 0 { - updates["nucleos"] = fmt.Sprintf("%d", m.CPU.Nucleos) + if cpu, _ := rawMap["cpu"].(map[string]any); cpu != nil { + if n, _ := cpu["nucleos"].(float64); n > 0 { + updates["nucleos"] = fmt.Sprintf("%d", int(n)) + } } - if m.Disco.TotalGB > 0 { - updates["disco"] = fmt.Sprintf("%.0f GB", m.Disco.TotalGB) + // Compatibilidad: nuevo formato (discos array) y viejo (disco objeto) + discos, hasArray := rawMap["discos"].([]any) + if hasArray { + var maxTotal float64 + for _, item := range discos { + if d, _ := item.(map[string]any); d != nil { + if t, _ := d["total_gb"].(float64); t > maxTotal { + maxTotal = t + } + } + } + if maxTotal > 0 { + updates["disco"] = fmt.Sprintf("%.0f GB", maxTotal) + } + } else if disco, _ := rawMap["disco"].(map[string]any); disco != nil { + if total, _ := disco["total_gb"].(float64); total > 0 { + updates["disco"] = fmt.Sprintf("%.0f GB", total) + } } } }