Add a VppStatsRoutine() alongside main
This commit is contained in:
64
vppstats/stats.go
Normal file
64
vppstats/stats.go
Normal file
@ -0,0 +1,64 @@
|
||||
package vppstats
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"go.fd.io/govpp/adapter/statsclient"
|
||||
"go.fd.io/govpp/api"
|
||||
"go.fd.io/govpp/core"
|
||||
)
|
||||
|
||||
// StartStatsRoutine starts a goroutine that queries VPP interface stats every 10 seconds
|
||||
func StartStatsRoutine() {
|
||||
go statsRoutine()
|
||||
}
|
||||
|
||||
func statsRoutine() {
|
||||
log.Println("Starting VPP stats routine...")
|
||||
|
||||
// Create stats client
|
||||
client := statsclient.NewStatsClient("/var/run/vpp/stats.sock")
|
||||
|
||||
// Connect using core.ConnectStats (proper way)
|
||||
c, err := core.ConnectStats(client)
|
||||
if err != nil {
|
||||
log.Printf("Failed to connect to VPP stats: %v", err)
|
||||
return
|
||||
}
|
||||
defer c.Disconnect()
|
||||
|
||||
ticker := time.NewTicker(10 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
queryInterfaceStats(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func queryInterfaceStats(c *core.StatsConnection) {
|
||||
log.Printf("Querying VPP interface stats at %s", time.Now().Format(time.RFC3339))
|
||||
|
||||
// Create the proper struct for interface stats
|
||||
stats := new(api.InterfaceStats)
|
||||
|
||||
// Use the GetInterfaceStats method - this is the correct approach
|
||||
if err := c.GetInterfaceStats(stats); err != nil {
|
||||
log.Printf("Failed to get interface stats: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Now you have properly structured data
|
||||
for _, iface := range stats.Interfaces {
|
||||
log.Printf("Interface %d (%s):", iface.InterfaceIndex, iface.InterfaceName)
|
||||
log.Printf(" RX: %d packets, %d bytes", iface.Rx.Packets, iface.Rx.Bytes)
|
||||
log.Printf(" TX: %d packets, %d bytes", iface.Tx.Packets, iface.Tx.Bytes)
|
||||
log.Printf(" RX Errors: %d, TX Errors: %d", iface.RxErrors, iface.TxErrors)
|
||||
log.Printf(" Drops: %d, Punts: %d", iface.Drops, iface.Punts)
|
||||
}
|
||||
|
||||
log.Printf("Retrieved stats for %d interfaces", len(stats.Interfaces))
|
||||
}
|
Reference in New Issue
Block a user