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
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
}

View File

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

View File

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

View File

@ -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,14 +60,12 @@ 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",
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
if callback != nil {