From fb3c545e113814c97a534780da99709e665eacb6 Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Mon, 9 Jun 2025 18:10:44 +0200 Subject: [PATCH] Make -debug a global config flag, clean up logger.Debugf() callsites --- config/config.go | 6 ++++++ ifmib/ifmib.go | 16 +++++++--------- logger/logger.go | 8 +++++--- main.go | 8 ++++++-- vppstats/stats.go | 26 ++++++++++++-------------- 5 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 config/config.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..92f3e58 --- /dev/null +++ b/config/config.go @@ -0,0 +1,6 @@ +package config + +// Global configuration variables +var ( + Debug bool +) \ No newline at end of file diff --git a/ifmib/ifmib.go b/ifmib/ifmib.go index 56bea75..40e18ee 100644 --- a/ifmib/ifmib.go +++ b/ifmib/ifmib.go @@ -66,15 +66,13 @@ type InterfaceMIB struct { ifXTableSession *agentx.Session stats map[uint32]*api.InterfaceCounters // indexed by interface index indexOffset int - debug bool } -func NewInterfaceMIB(indexOffset int, debug bool) *InterfaceMIB { +func NewInterfaceMIB(indexOffset int) *InterfaceMIB { return &InterfaceMIB{ handler: &agentx.ListHandler{}, stats: make(map[uint32]*api.InterfaceCounters), indexOffset: indexOffset, - debug: debug, } } @@ -86,7 +84,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) { m.mutex.Lock() defer m.mutex.Unlock() - logger.Debugf(m.debug, "Updating IF-MIB with %d interfaces", len(interfaceStats.Interfaces)) + logger.Debugf("Updating IF-MIB with %d interfaces", len(interfaceStats.Interfaces)) // Clear existing entries m.handler = &agentx.ListHandler{} @@ -94,7 +92,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) { // Add new entries for _, iface := range interfaceStats.Interfaces { - logger.Debugf(m.debug, "Processing interface %d (%s)", iface.InterfaceIndex, iface.InterfaceName) + logger.Debugf("Processing interface %d (%s)", iface.InterfaceIndex, iface.InterfaceName) m.stats[iface.InterfaceIndex] = &iface m.addInterfaceToMIB(&iface) } @@ -108,7 +106,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) { logger.Printf("Updated session handlers with new IF-MIB data") } - logger.Debugf(m.debug, "IF-MIB now contains %d interfaces", len(m.stats)) + logger.Debugf("IF-MIB now contains %d interfaces", len(m.stats)) } func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) { @@ -120,7 +118,7 @@ func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) { // Add ifXTable (extended interface table) entries m.addIfXTable(iface, idx) - logger.Debugf(m.debug, "Added interface %d (%s) to IF-MIB with SNMP index %d", iface.InterfaceIndex, iface.InterfaceName, idx) + logger.Debugf("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) { @@ -337,7 +335,7 @@ func (m *InterfaceMIB) RegisterWithClient(client *agentx.Client) error { return fmt.Errorf("failed to register ifXTable: %v", err) } - logger.Debugf(m.debug, "Registered IF-MIB ifEntry at OID %s", ifEntryOID) - logger.Debugf(m.debug, "Registered IF-MIB ifXTable at OID %s", ifXTableOID) + logger.Debugf("Registered IF-MIB ifEntry at OID %s", ifEntryOID) + logger.Debugf("Registered IF-MIB ifXTable at OID %s", ifXTableOID) return nil } diff --git a/logger/logger.go b/logger/logger.go index ada8fd4..91ac627 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -5,6 +5,8 @@ import ( "log" "path/filepath" "runtime" + + "govpp-snmp-example/config" ) // logf logs a message with automatic caller information (file:function) @@ -34,9 +36,9 @@ func Printf(format string, args ...interface{}) { logf(format, args...) } -// Debugf logs a debug message with caller information if debug is true -func Debugf(debug bool, format string, args ...interface{}) { - if debug { +// Debugf logs a debug message with caller information if global debug is enabled +func Debugf(format string, args ...interface{}) { + if config.Debug { logf(format, args...) } } \ No newline at end of file diff --git a/main.go b/main.go index 296c575..2de755e 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "github.com/posteo/go-agentx" + "govpp-snmp-example/config" "govpp-snmp-example/ifmib" "govpp-snmp-example/vppstats" ) @@ -20,6 +21,9 @@ func main() { debug := flag.Bool("debug", false, "Enable debug logging") flag.Parse() + // Set global debug flag + config.Debug = *debug + var network, address string if strings.HasPrefix(*addr, "/") { network = "unix" @@ -37,7 +41,7 @@ func main() { client.ReconnectInterval = 1 * time.Second // Create the interface MIB - interfaceMIB := ifmib.NewInterfaceMIB(*ifIndexOffset, *debug) + interfaceMIB := ifmib.NewInterfaceMIB(*ifIndexOffset) // Register the interface MIB with the AgentX client if err := interfaceMIB.RegisterWithClient(client); err != nil { @@ -45,7 +49,7 @@ func main() { } // Start VPP stats routine with callback to update MIB - vppstats.StartStatsRoutine(*vppStatsAddr, time.Duration(*period*1000)*time.Millisecond, interfaceMIB.UpdateStats, *debug) + vppstats.StartStatsRoutine(*vppStatsAddr, time.Duration(*period*1000)*time.Millisecond, interfaceMIB.UpdateStats) for { time.Sleep(100 * time.Millisecond) diff --git a/vppstats/stats.go b/vppstats/stats.go index 2c2efbd..3b1bdfe 100644 --- a/vppstats/stats.go +++ b/vppstats/stats.go @@ -14,12 +14,12 @@ 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, debug bool) { - go statsRoutine(statsSocketPath, period, callback, debug) +func StartStatsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback) { + go statsRoutine(statsSocketPath, period, callback) } -func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback, debug bool) { - logger.Debugf(debug, "Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period) +func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback) { + logger.Debugf("Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period) // Create stats client client := statsclient.NewStatsClient(statsSocketPath) @@ -33,7 +33,7 @@ func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCa defer c.Disconnect() // Query stats immediately on startup - queryInterfaceStats(c, callback, debug) + queryInterfaceStats(c, callback) ticker := time.NewTicker(period) defer ticker.Stop() @@ -41,12 +41,12 @@ func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCa for { select { case <-ticker.C: - queryInterfaceStats(c, callback, debug) + queryInterfaceStats(c, callback) } } } -func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback, debug bool) { +func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback) { // Create the proper struct for interface stats stats := new(api.InterfaceStats) @@ -60,13 +60,11 @@ func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback, debug logger.Printf("Retrieved stats for %d interfaces", len(stats.Interfaces)) // Debug logging for individual interfaces - if debug { - for _, iface := range stats.Interfaces { - logger.Debugf(debug, "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) - } + for _, iface := range stats.Interfaces { + logger.Debugf("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