diff --git a/.gitignore b/.gitignore index 585563f..3715a4f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ vpp-snmp-agent +govpp-snmp-example diff --git a/ifmib/ifmib.go b/ifmib/ifmib.go index 40e18ee..f3c4184 100644 --- a/ifmib/ifmib.go +++ b/ifmib/ifmib.go @@ -11,6 +11,7 @@ import ( "go.fd.io/govpp/api" "govpp-snmp-example/logger" + "govpp-snmp-example/vppstats" ) // IF-MIB OID bases: @@ -65,14 +66,12 @@ type InterfaceMIB struct { ifEntrySession *agentx.Session ifXTableSession *agentx.Session stats map[uint32]*api.InterfaceCounters // indexed by interface index - indexOffset int } -func NewInterfaceMIB(indexOffset int) *InterfaceMIB { +func NewInterfaceMIB() *InterfaceMIB { return &InterfaceMIB{ - handler: &agentx.ListHandler{}, - stats: make(map[uint32]*api.InterfaceCounters), - indexOffset: indexOffset, + handler: &agentx.ListHandler{}, + stats: make(map[uint32]*api.InterfaceCounters), } } @@ -110,7 +109,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) { } func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) { - idx := int(iface.InterfaceIndex) + m.indexOffset + idx := int(iface.InterfaceIndex) + *vppstats.IfIndexOffset // Add ifEntry (classic interface table) entries m.addIfEntry(iface, idx) diff --git a/main.go b/main.go index 2de755e..bad1d9d 100644 --- a/main.go +++ b/main.go @@ -15,9 +15,6 @@ import ( func main() { addr := flag.String("agentx-addr", "localhost:705", "Address to connect to (hostname:port or Unix socket path)") - vppStatsAddr := flag.String("vpp-stats-addr", "/var/run/vpp/stats.sock", "VPP stats socket path") - period := flag.Float64("period", 10.0, "Interval in seconds for querying VPP interface stats") - ifIndexOffset := flag.Int("vpp-ifindex-offset", 1000, "Offset to add to VPP interface indices for SNMP") debug := flag.Bool("debug", false, "Enable debug logging") flag.Parse() @@ -41,7 +38,7 @@ func main() { client.ReconnectInterval = 1 * time.Second // Create the interface MIB - interfaceMIB := ifmib.NewInterfaceMIB(*ifIndexOffset) + interfaceMIB := ifmib.NewInterfaceMIB() // Register the interface MIB with the AgentX client if err := interfaceMIB.RegisterWithClient(client); err != nil { @@ -49,7 +46,7 @@ func main() { } // Start VPP stats routine with callback to update MIB - vppstats.StartStatsRoutine(*vppStatsAddr, time.Duration(*period*1000)*time.Millisecond, interfaceMIB.UpdateStats) + vppstats.StartStatsRoutine(interfaceMIB.UpdateStats) for { time.Sleep(100 * time.Millisecond) diff --git a/vppstats/stats.go b/vppstats/stats.go index 3b1bdfe..38d9baa 100644 --- a/vppstats/stats.go +++ b/vppstats/stats.go @@ -1,6 +1,7 @@ package vppstats import ( + "flag" "log" "time" @@ -13,16 +14,24 @@ import ( type StatsCallback func(*api.InterfaceStats) -// StartStatsRoutine starts a goroutine that queries VPP interface stats at the specified interval -func StartStatsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback) { - go statsRoutine(statsSocketPath, period, callback) +var ( + // Flags for VPP stats configuration + StatsAddr = flag.String("vppstats.addr", "/var/run/vpp/stats.sock", "VPP stats socket path") + IfIndexOffset = flag.Int("vppstats.ifindex-offset", 1000, "Offset to add to VPP interface indices for SNMP") + Period = flag.Int("vppstats.period", 10, "Interval in seconds for querying VPP interface stats") +) + +// StartStatsRoutine starts a goroutine that queries VPP interface stats at the configured interval +func StartStatsRoutine(callback StatsCallback) { + period := time.Duration(*Period) * time.Second + go statsRoutine(period, callback) } -func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback) { - logger.Debugf("Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period) +func statsRoutine(period time.Duration, callback StatsCallback) { + logger.Debugf("Starting VPP stats routine with socket: %s, period: %v", *StatsAddr, period) // Create stats client - client := statsclient.NewStatsClient(statsSocketPath) + client := statsclient.NewStatsClient(*StatsAddr) // Connect using core.ConnectStats (proper way) c, err := core.ConnectStats(client)