Make -debug a global config flag, clean up logger.Debugf() callsites

This commit is contained in:
Pim van Pelt
2025-06-09 18:10:44 +02:00
parent 29d0417452
commit fb3c545e11
5 changed files with 36 additions and 28 deletions

6
config/config.go Normal file
View File

@ -0,0 +1,6 @@
package config
// Global configuration variables
var (
Debug bool
)

View File

@ -66,15 +66,13 @@ type InterfaceMIB struct {
ifXTableSession *agentx.Session ifXTableSession *agentx.Session
stats map[uint32]*api.InterfaceCounters // indexed by interface index stats map[uint32]*api.InterfaceCounters // indexed by interface index
indexOffset int indexOffset int
debug bool
} }
func NewInterfaceMIB(indexOffset int, debug bool) *InterfaceMIB { func NewInterfaceMIB(indexOffset int) *InterfaceMIB {
return &InterfaceMIB{ return &InterfaceMIB{
handler: &agentx.ListHandler{}, handler: &agentx.ListHandler{},
stats: make(map[uint32]*api.InterfaceCounters), stats: make(map[uint32]*api.InterfaceCounters),
indexOffset: indexOffset, indexOffset: indexOffset,
debug: debug,
} }
} }
@ -86,7 +84,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) {
m.mutex.Lock() m.mutex.Lock()
defer m.mutex.Unlock() 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 // Clear existing entries
m.handler = &agentx.ListHandler{} m.handler = &agentx.ListHandler{}
@ -94,7 +92,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) {
// Add new entries // Add new entries
for _, iface := range interfaceStats.Interfaces { 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.stats[iface.InterfaceIndex] = &iface
m.addInterfaceToMIB(&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.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) { func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) {
@ -120,7 +118,7 @@ func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) {
// Add ifXTable (extended interface table) entries // Add ifXTable (extended interface table) entries
m.addIfXTable(iface, idx) 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) { 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) return fmt.Errorf("failed to register ifXTable: %v", err)
} }
logger.Debugf(m.debug, "Registered IF-MIB ifEntry at OID %s", ifEntryOID) logger.Debugf("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 ifXTable at OID %s", ifXTableOID)
return nil return nil
} }

View File

@ -5,6 +5,8 @@ import (
"log" "log"
"path/filepath" "path/filepath"
"runtime" "runtime"
"govpp-snmp-example/config"
) )
// logf logs a message with automatic caller information (file:function) // logf logs a message with automatic caller information (file:function)
@ -34,9 +36,9 @@ func Printf(format string, args ...interface{}) {
logf(format, args...) logf(format, args...)
} }
// Debugf logs a debug message with caller information if debug is true // Debugf logs a debug message with caller information if global debug is enabled
func Debugf(debug bool, format string, args ...interface{}) { func Debugf(format string, args ...interface{}) {
if debug { if config.Debug {
logf(format, args...) logf(format, args...)
} }
} }

View File

@ -8,6 +8,7 @@ import (
"github.com/posteo/go-agentx" "github.com/posteo/go-agentx"
"govpp-snmp-example/config"
"govpp-snmp-example/ifmib" "govpp-snmp-example/ifmib"
"govpp-snmp-example/vppstats" "govpp-snmp-example/vppstats"
) )
@ -20,6 +21,9 @@ func main() {
debug := flag.Bool("debug", false, "Enable debug logging") debug := flag.Bool("debug", false, "Enable debug logging")
flag.Parse() flag.Parse()
// Set global debug flag
config.Debug = *debug
var network, address string var network, address string
if strings.HasPrefix(*addr, "/") { if strings.HasPrefix(*addr, "/") {
network = "unix" network = "unix"
@ -37,7 +41,7 @@ func main() {
client.ReconnectInterval = 1 * time.Second client.ReconnectInterval = 1 * time.Second
// Create the interface MIB // Create the interface MIB
interfaceMIB := ifmib.NewInterfaceMIB(*ifIndexOffset, *debug) interfaceMIB := ifmib.NewInterfaceMIB(*ifIndexOffset)
// Register the interface MIB with the AgentX client // Register the interface MIB with the AgentX client
if err := interfaceMIB.RegisterWithClient(client); err != nil { if err := interfaceMIB.RegisterWithClient(client); err != nil {
@ -45,7 +49,7 @@ func main() {
} }
// Start VPP stats routine with callback to update MIB // 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 { for {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)

View File

@ -14,12 +14,12 @@ import (
type StatsCallback func(*api.InterfaceStats) type StatsCallback func(*api.InterfaceStats)
// StartStatsRoutine starts a goroutine that queries VPP interface stats at the specified interval // StartStatsRoutine starts a goroutine that queries VPP interface stats at the specified interval
func StartStatsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback, debug bool) { func StartStatsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback) {
go statsRoutine(statsSocketPath, period, callback, debug) go statsRoutine(statsSocketPath, period, callback)
} }
func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback, debug bool) { func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback) {
logger.Debugf(debug, "Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period) logger.Debugf("Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period)
// Create stats client // Create stats client
client := statsclient.NewStatsClient(statsSocketPath) client := statsclient.NewStatsClient(statsSocketPath)
@ -33,7 +33,7 @@ func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCa
defer c.Disconnect() defer c.Disconnect()
// Query stats immediately on startup // Query stats immediately on startup
queryInterfaceStats(c, callback, debug) queryInterfaceStats(c, callback)
ticker := time.NewTicker(period) ticker := time.NewTicker(period)
defer ticker.Stop() defer ticker.Stop()
@ -41,12 +41,12 @@ func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCa
for { for {
select { select {
case <-ticker.C: 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 // Create the proper struct for interface stats
stats := new(api.InterfaceStats) 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)) logger.Printf("Retrieved stats for %d interfaces", len(stats.Interfaces))
// Debug logging for individual interfaces // Debug logging for individual interfaces
if debug { for _, iface := range stats.Interfaces {
for _, iface := range stats.Interfaces { logger.Debugf("Interface %d (%s): RX %d pkts/%d bytes, TX %d pkts/%d bytes",
logger.Debugf(debug, "Interface %d (%s): RX %d pkts/%d bytes, TX %d pkts/%d bytes", iface.InterfaceIndex, iface.InterfaceName,
iface.InterfaceIndex, iface.InterfaceName, iface.Rx.Packets, iface.Rx.Bytes,
iface.Rx.Packets, iface.Rx.Bytes, iface.Tx.Packets, iface.Tx.Bytes)
iface.Tx.Packets, iface.Tx.Bytes)
}
} }
// Call the callback to update the MIB // Call the callback to update the MIB