95 lines
3.2 KiB
Go
95 lines
3.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/backend/internal/grpcsrv/collector"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AgentsGroup struct {
|
|
*Handlers
|
|
collector *collector.Collector
|
|
}
|
|
|
|
func NewAgentsGroup(h *Handlers, coll *collector.Collector) AgentsGroup {
|
|
return AgentsGroup{Handlers: h, collector: coll}
|
|
}
|
|
|
|
// AgentInfo represents a connected agent's current status.
|
|
type AgentInfo struct {
|
|
Token string `json:"token" example:"agent-001"` // Unique agent identifier
|
|
Label string `json:"label" example:"web-server-1"` // Human-readable label
|
|
Services []string `json:"services" example:"nginx:running,redis:up"` // List of services with status (format: "name:status")
|
|
ConnectedAt string `json:"connected_at" example:"2026-04-04 10:30:00"` // Time when agent connected (RFC3339-like)
|
|
}
|
|
|
|
// @Summary Get connected agents
|
|
// @Description Returns a list of all agents currently connected via Collector (log streaming)
|
|
// @Tags agents
|
|
// @Security Bearer
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {array} AgentInfo
|
|
// @Router /agents [get]
|
|
func (ag *AgentsGroup) List(c *gin.Context) {
|
|
agents := make([]AgentInfo, 0)
|
|
|
|
for _, agent := range ag.collector.Agents() {
|
|
services := make([]string, 0, len(agent.Services))
|
|
for _, s := range agent.Services {
|
|
services = append(services, fmt.Sprintf("%s:%s", s.Name, s.Status))
|
|
}
|
|
agents = append(agents, AgentInfo{
|
|
Token: agent.ID,
|
|
Label: agent.Label,
|
|
Services: services,
|
|
ConnectedAt: agent.ConnectedAt.Format("2006-01-02 15:04:05"),
|
|
})
|
|
}
|
|
|
|
c.JSON(http.StatusOK, agents)
|
|
}
|
|
|
|
// AgentSystemMetricsOut represents system metrics for a single agent.
|
|
type AgentSystemMetricsOut struct {
|
|
ID string `json:"id" example:"agent-001"`
|
|
Label string `json:"label" example:"web-server-1"`
|
|
ConnectedAt string `json:"connected_at" example:"2026-04-04 10:30:00"`
|
|
CPUPercent float64 `json:"cpu_percent" example:"45.2"`
|
|
MemoryPercent float64 `json:"memory_percent" example:"62.5"`
|
|
DiskPercent float64 `json:"disk_percent" example:"78.9"`
|
|
NetworkRxBytes float64 `json:"network_rx_bytes" example:"1048576.0"`
|
|
NetworkTxBytes float64 `json:"network_tx_bytes" example:"524288.0"`
|
|
}
|
|
|
|
// GetSystemMetrics returns system load metrics for all connected agents.
|
|
// @Summary Get agent system metrics
|
|
// @Description Returns CPU, RAM, disk, and network usage metrics for all connected agents
|
|
// @Tags agents
|
|
// @Security Bearer
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {array} AgentSystemMetricsOut
|
|
// @Router /agents/system-metrics [get]
|
|
func (ag *AgentsGroup) GetSystemMetrics(c *gin.Context) {
|
|
metricsMap := ag.collector.GetSystemMetrics()
|
|
|
|
metrics := make([]AgentSystemMetricsOut, 0, len(metricsMap))
|
|
for _, m := range metricsMap {
|
|
metrics = append(metrics, AgentSystemMetricsOut{
|
|
ID: m.ID,
|
|
Label: m.Label,
|
|
ConnectedAt: m.ConnectedAt.Format("2006-01-02 15:04:05"),
|
|
CPUPercent: m.CPUPercent,
|
|
MemoryPercent: m.MemoryPercent,
|
|
DiskPercent: m.DiskPercent,
|
|
NetworkRxBytes: m.NetworkRxBytes,
|
|
NetworkTxBytes: m.NetworkTxBytes,
|
|
})
|
|
}
|
|
|
|
c.JSON(http.StatusOK, metrics)
|
|
}
|