Add -debug flag

This commit is contained in:
Pim van Pelt
2025-06-09 18:00:59 +02:00
parent a96b853d06
commit 6371e8eee2
3 changed files with 42 additions and 23 deletions

View File

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