From 6371e8eee2727d888ddabdcf90f362cd0531582c Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Mon, 9 Jun 2025 18:00:59 +0200 Subject: [PATCH] Add -debug flag --- ifmib/ifmib.go | 26 +++++++++++++++++++------- main.go | 5 +++-- vppstats/stats.go | 34 ++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/ifmib/ifmib.go b/ifmib/ifmib.go index 1a86903..8bd6e9f 100644 --- a/ifmib/ifmib.go +++ b/ifmib/ifmib.go @@ -65,13 +65,15 @@ type InterfaceMIB struct { ifXTableSession *agentx.Session stats map[uint32]*api.InterfaceCounters // indexed by interface index indexOffset int + debug bool } -func NewInterfaceMIB(indexOffset int) *InterfaceMIB { +func NewInterfaceMIB(indexOffset int, debug bool) *InterfaceMIB { return &InterfaceMIB{ handler: &agentx.ListHandler{}, stats: make(map[uint32]*api.InterfaceCounters), indexOffset: indexOffset, + debug: debug, } } @@ -83,7 +85,9 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) { m.mutex.Lock() defer m.mutex.Unlock() - log.Printf("Updating IF-MIB with %d interfaces", len(interfaceStats.Interfaces)) + if m.debug { + log.Printf("Updating IF-MIB with %d interfaces", len(interfaceStats.Interfaces)) + } // Clear existing entries m.handler = &agentx.ListHandler{} @@ -91,7 +95,9 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) { // Add new entries for _, iface := range interfaceStats.Interfaces { - log.Printf("Processing interface %d (%s)", iface.InterfaceIndex, iface.InterfaceName) + if m.debug { + log.Printf("Processing interface %d (%s)", iface.InterfaceIndex, iface.InterfaceName) + } m.stats[iface.InterfaceIndex] = &iface m.addInterfaceToMIB(&iface) } @@ -105,7 +111,9 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) { log.Printf("Updated session handlers with new IF-MIB data") } - log.Printf("IF-MIB now contains %d interfaces", len(m.stats)) + if m.debug { + log.Printf("IF-MIB now contains %d interfaces", len(m.stats)) + } } func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) { @@ -117,7 +125,9 @@ func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) { // Add ifXTable (extended interface table) entries m.addIfXTable(iface, idx) - log.Printf("Added interface %d (%s) to IF-MIB with SNMP index %d", iface.InterfaceIndex, iface.InterfaceName, idx) + if m.debug { + log.Printf("Added interface %d (%s) to IF-MIB with SNMP index %d", iface.InterfaceIndex, iface.InterfaceName, idx) + } } func (m *InterfaceMIB) addIfEntry(iface *api.InterfaceCounters, idx int) { @@ -334,7 +344,9 @@ func (m *InterfaceMIB) RegisterWithClient(client *agentx.Client) error { return fmt.Errorf("failed to register ifXTable: %v", err) } - log.Printf("Registered IF-MIB ifEntry at OID %s", ifEntryOID) - log.Printf("Registered IF-MIB ifXTable at OID %s", ifXTableOID) + if m.debug { + log.Printf("Registered IF-MIB ifEntry at OID %s", ifEntryOID) + log.Printf("Registered IF-MIB ifXTable at OID %s", ifXTableOID) + } return nil } diff --git a/main.go b/main.go index 26d531b..296c575 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ func main() { 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() var network, address string @@ -36,7 +37,7 @@ func main() { client.ReconnectInterval = 1 * time.Second // Create the interface MIB - interfaceMIB := ifmib.NewInterfaceMIB(*ifIndexOffset) + interfaceMIB := ifmib.NewInterfaceMIB(*ifIndexOffset, *debug) // Register the interface MIB with the AgentX client if err := interfaceMIB.RegisterWithClient(client); err != nil { @@ -44,7 +45,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(*vppStatsAddr, time.Duration(*period*1000)*time.Millisecond, interfaceMIB.UpdateStats, *debug) for { time.Sleep(100 * time.Millisecond) diff --git a/vppstats/stats.go b/vppstats/stats.go index 406712f..959bf84 100644 --- a/vppstats/stats.go +++ b/vppstats/stats.go @@ -12,12 +12,14 @@ 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) +func StartStatsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback, debug bool) { + go statsRoutine(statsSocketPath, period, callback, debug) } -func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback) { - log.Printf("Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period) +func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback, debug bool) { + if debug { + log.Printf("Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period) + } // Create stats client client := statsclient.NewStatsClient(statsSocketPath) @@ -31,7 +33,7 @@ func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCa defer c.Disconnect() // Query stats immediately on startup - queryInterfaceStats(c, callback) + queryInterfaceStats(c, callback, debug) ticker := time.NewTicker(period) defer ticker.Stop() @@ -39,12 +41,12 @@ func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCa for { select { case <-ticker.C: - queryInterfaceStats(c, callback) + queryInterfaceStats(c, callback, debug) } } } -func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback) { +func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback, debug bool) { // Create the proper struct for interface stats stats := new(api.InterfaceStats) @@ -54,17 +56,21 @@ func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback) { return } - // Log basic info + // Always log basic info log.Printf("Retrieved stats for %d interfaces", len(stats.Interfaces)) - for _, iface := range stats.Interfaces { - log.Printf("Interface %d (%s): RX %d pkts/%d bytes, TX %d pkts/%d bytes", - iface.InterfaceIndex, iface.InterfaceName, - iface.Rx.Packets, iface.Rx.Bytes, - iface.Tx.Packets, iface.Tx.Bytes) + + // Debug logging for individual interfaces + if debug { + for _, iface := range stats.Interfaces { + log.Printf("Interface %d (%s): RX %d pkts/%d bytes, TX %d pkts/%d bytes", + iface.InterfaceIndex, iface.InterfaceName, + iface.Rx.Packets, iface.Rx.Bytes, + iface.Tx.Packets, iface.Tx.Bytes) + } } // Call the callback to update the MIB if callback != nil { callback(stats) } -} \ No newline at end of file +}