Use interface details to populate the ifmib, on startup and after each event

This commit is contained in:
Pim van Pelt
2025-06-23 19:11:28 +02:00
parent 35165b0464
commit 4f368e625d
4 changed files with 165 additions and 21 deletions

View File

@@ -17,6 +17,9 @@ import (
type StatsCallback func(*api.InterfaceStats)
// Global callback for interface events
var interfaceEventCallback InterfaceEventCallback
var (
// Flags for VPP stats configuration
ApiAddr = flag.String("vppstats.api.addr", "/var/run/vpp/api.sock", "VPP API socket path")
@@ -25,6 +28,11 @@ var (
Period = flag.Int("vppstats.period", 10, "Interval in seconds for querying VPP interface stats")
)
// SetInterfaceEventCallback sets the callback for interface events
func SetInterfaceEventCallback(callback InterfaceEventCallback) {
interfaceEventCallback = callback
}
// StartStatsRoutine starts a goroutine that queries VPP interface stats at the configured interval
func StartStatsRoutine(callback StatsCallback) {
period := time.Duration(*Period) * time.Second
@@ -139,11 +147,22 @@ func statsRoutine(period time.Duration, callback StatsCallback) {
logger.Debugf("Failed to create API channel for interface events: %v", err)
} else {
logger.Debugf("API channel created successfully, calling WatchInterfaceEvents...")
if err := WatchInterfaceEvents(ch); err != nil {
if err := WatchInterfaceEvents(ch, interfaceEventCallback); err != nil {
logger.Debugf("Failed to start interface event watching: %v", err)
ch.Close()
} else {
logger.Printf("Interface event watching started successfully")
// Do initial retrieval of interface details
if interfaceEventCallback != nil {
details, err := GetAllInterfaceDetails(ch)
if err != nil {
logger.Debugf("Failed to get initial interface details: %v", err)
} else {
logger.Debugf("Retrieved initial interface details for %d interfaces", len(details))
interfaceEventCallback(details)
}
}
}
}
}