- New table servidor_metricas_history (cpu%, ram%, swap%, disco%, tcp, load1) - Each heartbeat inserts a compact row (~60 bytes) - GET /app/servidor/:id/metricas-history?horas=24 returns the data - Daily cron at 3AM purges rows older than 7 days - ~1.2 MB per server per 7 days at 30s interval Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
152 lines
5.1 KiB
Go
Executable File
152 lines
5.1 KiB
Go
Executable File
package models
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/sujit-baniya/fiber-boilerplate/app"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func generateAgentToken() string {
|
|
b := make([]byte, 16)
|
|
rand.Read(b)
|
|
return "agent_" + hex.EncodeToString(b)
|
|
}
|
|
|
|
type Servidor struct {
|
|
gorm.Model
|
|
Nombre string `json:"nombre" gorm:"column:nombre"`
|
|
IpServidor string `json:"ip_servidor" gorm:"column:ip_servidor"`
|
|
So string `json:"so" gorm:"column:so"`
|
|
Vencimiento string `json:"vencimiento" gorm:"column:vencimiento"`
|
|
Ram string `json:"ram" gorm:"column:ram"`
|
|
Nucleos string `json:"nucleos" gorm:"column:nucleos"`
|
|
Disco string `json:"disco" gorm:"column:disco"`
|
|
UltimoPing string `json:"ultimo_ping" gorm:"column:ultimo_ping"`
|
|
ProvServidorID uint `json:"prov_servidor_id" gorm:"column:prov_servidor_id;foreignKey:ProvServidorID"`
|
|
ProvServidor ProvServidor `json:"prov_servidor" gorm:"foreignKey:ProvServidorID"`
|
|
TipoServidorID uint `json:"tipo_servidor_id" gorm:"column:tipo_servidor_id;foreignKey:TipoServidorID"`
|
|
TipoServidor TipoServidor `json:"tipo_servidor" gorm:"foreignKey:TipoServidorID"`
|
|
// Campos del agente de monitoreo
|
|
AgentToken string `json:"agent_token" gorm:"column:agent_token;uniqueIndex"`
|
|
AgentLastSeen *time.Time `json:"agent_last_seen" gorm:"column:agent_last_seen"`
|
|
MetricasJson string `json:"metricas_json" gorm:"column:metricas_json;type:text"`
|
|
// Integración Hostinger
|
|
HostingerVpsID *int `json:"hostinger_vps_id" gorm:"column:hostinger_vps_id"`
|
|
HostingerState string `json:"hostinger_state" gorm:"column:hostinger_state"`
|
|
HostingerSubscriptionID string `json:"hostinger_subscription_id" gorm:"column:hostinger_subscription_id"`
|
|
}
|
|
|
|
func (Servidor) TableName() string {
|
|
return "servidor"
|
|
}
|
|
|
|
func GetAllServidores(limit, offset int, search string) ([]Servidor, int64, error) {
|
|
var items []Servidor
|
|
var total int64
|
|
db := app.Http.Database.DB.Model(&Servidor{})
|
|
|
|
if search != "" {
|
|
db = db.Where("nombre LIKE ? OR ip_servidor LIKE ?", "%"+search+"%", "%"+search+"%")
|
|
}
|
|
|
|
db = db.Preload("ProvServidor").Preload("TipoServidor")
|
|
|
|
if err := db.Count(&total).Error; err != nil {
|
|
log.Printf("Error counting servidores: %v", err)
|
|
return nil, 0, err
|
|
}
|
|
|
|
if err := db.Order("created_at DESC").Limit(limit).Offset(offset).Find(&items).Error; err != nil {
|
|
log.Printf("Error retrieving servidores: %v", err)
|
|
return nil, 0, err
|
|
}
|
|
|
|
return items, total, nil
|
|
}
|
|
|
|
func GetAllServidoresSelect() ([]Servidor, error) {
|
|
var items []Servidor
|
|
db := app.Http.Database.DB.Model(&Servidor{})
|
|
|
|
if err := db.Order("created_at DESC").Find(&items).Error; err != nil {
|
|
log.Printf("Error retrieving servidores: %v", err)
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
|
|
}
|
|
|
|
func CreateServidor(servidor Servidor) error {
|
|
if servidor.AgentToken == "" {
|
|
servidor.AgentToken = generateAgentToken()
|
|
}
|
|
if err := app.Http.Database.DB.Create(&servidor).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func UpdateServidor(servidor Servidor) error {
|
|
if err := app.Http.Database.DB.Model(&servidor).Updates(servidor).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func DeleteServidor(servidor Servidor) error {
|
|
if err := app.Http.Database.DB.Delete(&servidor).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ── Historial de métricas ─────────────────────────────────────────────────────
|
|
|
|
type ServidorMetricasHistory struct {
|
|
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
|
|
ServidorID uint `json:"servidor_id" gorm:"column:servidor_id;index"`
|
|
CreatedAt time.Time `json:"created_at" gorm:"column:created_at;index"`
|
|
CpuPct float64 `json:"cpu_pct" gorm:"column:cpu_pct"`
|
|
RamPct float64 `json:"ram_pct" gorm:"column:ram_pct"`
|
|
SwapPct float64 `json:"swap_pct" gorm:"column:swap_pct"`
|
|
DiscoPct float64 `json:"disco_pct" gorm:"column:disco_pct"`
|
|
TCPConns int `json:"tcp_conns" gorm:"column:tcp_conns"`
|
|
Load1 float64 `json:"load1" gorm:"column:load1"`
|
|
}
|
|
|
|
func (ServidorMetricasHistory) TableName() string { return "servidor_metricas_history" }
|
|
|
|
func InsertMetricasHistory(servidorID uint, cpu, ram, swap, disco float64, tcp int, load1 float64) {
|
|
app.Http.Database.DB.Create(&ServidorMetricasHistory{
|
|
ServidorID: servidorID,
|
|
CreatedAt: time.Now(),
|
|
CpuPct: cpu,
|
|
RamPct: ram,
|
|
SwapPct: swap,
|
|
DiscoPct: disco,
|
|
TCPConns: tcp,
|
|
Load1: load1,
|
|
})
|
|
}
|
|
|
|
func GetMetricasHistory(servidorID uint, horas int) ([]ServidorMetricasHistory, error) {
|
|
var items []ServidorMetricasHistory
|
|
desde := time.Now().Add(-time.Duration(horas) * time.Hour)
|
|
err := app.Http.Database.DB.
|
|
Where("servidor_id = ? AND created_at >= ?", servidorID, desde).
|
|
Order("created_at ASC").
|
|
Find(&items).Error
|
|
return items, err
|
|
}
|
|
|
|
func PurgarMetricasHistory(dias int) error {
|
|
corte := time.Now().AddDate(0, 0, -dias)
|
|
return app.Http.Database.DB.
|
|
Where("created_at < ?", corte).
|
|
Delete(&ServidorMetricasHistory{}).Error
|
|
}
|