up
This commit is contained in:
@@ -3,6 +3,8 @@ package controllers
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@@ -35,9 +37,41 @@ func AgentHeartbeat(c *fiber.Ctx) error {
|
||||
now := time.Now()
|
||||
updates := map[string]interface{}{
|
||||
"agent_last_seen": &now,
|
||||
"ultimo_ping": now.Format(time.RFC3339),
|
||||
}
|
||||
if req.Metricas != "" {
|
||||
updates["metricas_json"] = req.Metricas
|
||||
// Actualizar campos planos desde las métricas del agente (la verdad viene del agente)
|
||||
type agentRAM struct {
|
||||
TotalGB float64 `json:"total_gb"`
|
||||
}
|
||||
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
|
||||
}
|
||||
if m.RAM.TotalGB > 0 {
|
||||
updates["ram"] = fmt.Sprintf("%.0f GB", m.RAM.TotalGB)
|
||||
}
|
||||
if m.CPU.Nucleos > 0 {
|
||||
updates["nucleos"] = fmt.Sprintf("%d", m.CPU.Nucleos)
|
||||
}
|
||||
if m.Disco.TotalGB > 0 {
|
||||
updates["disco"] = fmt.Sprintf("%.0f GB", m.Disco.TotalGB)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := db.Model(&servidor).Updates(updates).Error; err != nil {
|
||||
|
||||
Reference in New Issue
Block a user