Add per-resource history and guest network groups

This commit is contained in:
Maxim
2026-08-01 12:43:05 +03:00
parent d3fbbf2a6b
commit 92607f62d5
6 changed files with 202 additions and 18 deletions

View File

@@ -19,13 +19,18 @@ type NetworkMetrics struct {
}
type NetworkInterface struct {
Name string `json:"name"`
ReceiveBytes uint64 `json:"receiveBytes"`
TransmitBytes uint64 `json:"transmitBytes"`
ReceiveErrors uint64 `json:"receiveErrors"`
TransmitErrors uint64 `json:"transmitErrors"`
ReceiveDrops uint64 `json:"receiveDrops"`
TransmitDrops uint64 `json:"transmitDrops"`
Name string `json:"name"`
ReceiveBytes uint64 `json:"receiveBytes"`
TransmitBytes uint64 `json:"transmitBytes"`
ReceiveErrors uint64 `json:"receiveErrors"`
TransmitErrors uint64 `json:"transmitErrors"`
ReceiveDrops uint64 `json:"receiveDrops"`
TransmitDrops uint64 `json:"transmitDrops"`
ReceivePerSec float64 `json:"receivePerSec"`
TransmitPerSec float64 `json:"transmitPerSec"`
VMID int `json:"vmid,omitempty"`
GuestName string `json:"guestName,omitempty"`
GuestType string `json:"guestType,omitempty"`
}
type networkSnapshot struct {
@@ -35,9 +40,10 @@ type networkSnapshot struct {
}
type networkCollector struct {
mu sync.Mutex
path string
previous networkSnapshot
mu sync.Mutex
path string
previous networkSnapshot
previousInterfaces map[string]NetworkInterface
}
func newNetworkCollector(path string) (*networkCollector, error) {
@@ -45,7 +51,12 @@ func newNetworkCollector(path string) (*networkCollector, error) {
if err != nil {
return nil, err
}
return &networkCollector{path: path, previous: snapshot}, nil
_, _, details, _ := readNetworkSnapshot(path)
previous := map[string]NetworkInterface{}
for _, v := range details {
previous[v.Name] = v
}
return &networkCollector{path: path, previous: snapshot, previousInterfaces: previous}, nil
}
func (c *networkCollector) collect() (NetworkMetrics, error) {
@@ -62,6 +73,17 @@ func (c *networkCollector) collect() (NetworkMetrics, error) {
receivedRate = float64(current.received-c.previous.received) / elapsed
sentRate = float64(current.sent-c.previous.sent) / elapsed
}
for i := range details {
v := &details[i]
if old, ok := c.previousInterfaces[v.Name]; ok && elapsed > 0 && v.ReceiveBytes >= old.ReceiveBytes && v.TransmitBytes >= old.TransmitBytes {
v.ReceivePerSec = float64(v.ReceiveBytes-old.ReceiveBytes) / elapsed
v.TransmitPerSec = float64(v.TransmitBytes-old.TransmitBytes) / elapsed
}
}
c.previousInterfaces = map[string]NetworkInterface{}
for _, v := range details {
c.previousInterfaces[v.Name] = v
}
c.previous = current
return NetworkMetrics{
Interface: strings.Join(interfaceNames, ", "), Interfaces: details, ReceiveBytes: current.received,