chore: add system metrics
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user