Move flags to their own modules

This commit is contained in:
Pim van Pelt
2025-06-09 18:18:38 +02:00
parent fb3c545e11
commit b6cdcd16ba
4 changed files with 23 additions and 17 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
vpp-snmp-agent
govpp-snmp-example

View File

@ -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,
}
}
@ -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)

View File

@ -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)

View File

@ -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)