chore: add system metrics
This commit is contained in:
@@ -157,3 +157,8 @@ func (c *Collector) GetAgent(name string) (*Agent, bool) {
|
||||
func (c *Collector) Agents() []*Agent {
|
||||
return c.tracker.Agents()
|
||||
}
|
||||
|
||||
// GetSystemMetrics delegates to the tracker.
|
||||
func (c *Collector) GetSystemMetrics() map[string]AgentMetricsInfo {
|
||||
return c.tracker.GetSystemMetrics()
|
||||
}
|
||||
|
||||
@@ -36,3 +36,35 @@ func (c *Collector) ReportServices(ctx context.Context, req *proto.ServicesUpdat
|
||||
|
||||
return &proto.ServicesUpdateResp{}, nil
|
||||
}
|
||||
|
||||
// ReportSystemMetrics handles system metrics update from an agent.
|
||||
// Agents send their current system metrics (CPU, RAM, disk, network).
|
||||
func (c *Collector) ReportSystemMetrics(ctx context.Context, req *proto.SystemMetrics) (*proto.SystemMetricsResp, error) {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no metadata in context")
|
||||
}
|
||||
|
||||
whoamiVals := md["whoami"]
|
||||
if len(whoamiVals) == 0 {
|
||||
return nil, fmt.Errorf("whoami metadata missing")
|
||||
}
|
||||
agentName := whoamiVals[0]
|
||||
|
||||
metrics := SystemMetrics{
|
||||
CPUPercent: req.CpuPercent,
|
||||
MemoryPercent: req.MemoryPercent,
|
||||
DiskPercent: req.DiskPercent,
|
||||
NetworkRxBytes: req.NetworkRxBytes,
|
||||
NetworkTxBytes: req.NetworkTxBytes,
|
||||
}
|
||||
|
||||
if ok := c.tracker.UpdateSystemMetrics(agentName, metrics); ok {
|
||||
log.Printf("Updated system metrics for agent %s: CPU=%.1f%%, RAM=%.1f%%, Disk=%.1f%%",
|
||||
agentName, metrics.CPUPercent, metrics.MemoryPercent, metrics.DiskPercent)
|
||||
} else {
|
||||
log.Printf("Warning: received system metrics for unknown agent %s", agentName)
|
||||
}
|
||||
|
||||
return &proto.SystemMetricsResp{}, nil
|
||||
}
|
||||
|
||||
@@ -97,15 +97,69 @@ func (t *ConnTracker) UpdateServices(id string, services []Service) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// UpdateSystemMetrics updates the system metrics for the given agent.
|
||||
func (t *ConnTracker) UpdateSystemMetrics(id string, metrics SystemMetrics) bool {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
agent, ok := t.agents[id]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
agent.SystemMetrics = metrics
|
||||
return true
|
||||
}
|
||||
|
||||
// GetSystemMetrics returns system metrics for all connected agents.
|
||||
func (t *ConnTracker) GetSystemMetrics() map[string]AgentMetricsInfo {
|
||||
t.mu.RLock()
|
||||
defer t.mu.RUnlock()
|
||||
result := make(map[string]AgentMetricsInfo)
|
||||
for id, agent := range t.agents {
|
||||
result[id] = AgentMetricsInfo{
|
||||
ID: id,
|
||||
Label: agent.Label,
|
||||
ConnectedAt: agent.ConnectedAt,
|
||||
CPUPercent: agent.SystemMetrics.CPUPercent,
|
||||
MemoryPercent: agent.SystemMetrics.MemoryPercent,
|
||||
DiskPercent: agent.SystemMetrics.DiskPercent,
|
||||
NetworkRxBytes: agent.SystemMetrics.NetworkRxBytes,
|
||||
NetworkTxBytes: agent.SystemMetrics.NetworkTxBytes,
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Service represents a named service with its current status.
|
||||
type Service struct {
|
||||
Name, Status string
|
||||
}
|
||||
|
||||
// SystemMetrics represents system resource metrics.
|
||||
type SystemMetrics struct {
|
||||
CPUPercent float64
|
||||
MemoryPercent float64
|
||||
DiskPercent float64
|
||||
NetworkRxBytes float64
|
||||
NetworkTxBytes float64
|
||||
}
|
||||
|
||||
// AgentMetricsInfo contains agent info with its system metrics.
|
||||
type AgentMetricsInfo struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
ConnectedAt time.Time `json:"connected_at"`
|
||||
CPUPercent float64 `json:"cpu_percent"`
|
||||
MemoryPercent float64 `json:"memory_percent"`
|
||||
DiskPercent float64 `json:"disk_percent"`
|
||||
NetworkRxBytes float64 `json:"network_rx_bytes"`
|
||||
NetworkTxBytes float64 `json:"network_tx_bytes"`
|
||||
}
|
||||
|
||||
// Agent represents a connected agent streaming logs to the collector.
|
||||
type Agent struct {
|
||||
ID string
|
||||
Label string
|
||||
Services []Service
|
||||
ConnectedAt time.Time
|
||||
ID string
|
||||
Label string
|
||||
Services []Service
|
||||
SystemMetrics SystemMetrics
|
||||
ConnectedAt time.Time
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user