7be99f8e91
- agent+proto+backend: transfer service status - agent: fix returning empty message on nonzero exit status - backend: refactor collector+commander and handlers dependent on them: implement agent accounting via grpc stats handler
54 lines
1.6 KiB
Go
54 lines
1.6 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)
|
|
}
|