|
|
|
|
@@ -5,6 +5,7 @@ package ifmib
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"reflect"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
@@ -161,14 +162,32 @@ func (m *InterfaceMIB) UpdateInterfaceDetails(details []vpp.InterfaceDetails) {
|
|
|
|
|
logger.Debugf("Interface details updated for %d interfaces", len(details))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *InterfaceMIB) clearHandler() {
|
|
|
|
|
// Use reflection to access and clear the private fields of ListHandler
|
|
|
|
|
// since it doesn't have a Clear() method
|
|
|
|
|
handlerValue := reflect.ValueOf(m.handler).Elem()
|
|
|
|
|
|
|
|
|
|
oidsField := handlerValue.FieldByName("oids")
|
|
|
|
|
itemsField := handlerValue.FieldByName("items")
|
|
|
|
|
|
|
|
|
|
if oidsField.IsValid() && oidsField.CanSet() {
|
|
|
|
|
oidsField.Set(reflect.Zero(oidsField.Type()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if itemsField.IsValid() && itemsField.CanSet() {
|
|
|
|
|
itemsField.Set(reflect.Zero(itemsField.Type()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) {
|
|
|
|
|
m.mutex.Lock()
|
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
|
|
logger.Debugf("Updating IF-MIB with %d interfaces", len(interfaceStats.Interfaces))
|
|
|
|
|
|
|
|
|
|
// Clear existing entries
|
|
|
|
|
m.handler = &agentx.ListHandler{}
|
|
|
|
|
// Clear existing entries while preserving the handler reference
|
|
|
|
|
// Since go-agentx 0.3.0 binds handlers to sessions at creation time
|
|
|
|
|
m.clearHandler()
|
|
|
|
|
m.stats = make(map[uint32]*api.InterfaceCounters)
|
|
|
|
|
|
|
|
|
|
// Add new entries
|
|
|
|
|
@@ -178,11 +197,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) {
|
|
|
|
|
m.addInterfaceToMIB(&iface)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Note: With go-agentx 0.3.0, handlers are set during session creation and cannot be changed
|
|
|
|
|
if m.ifXTableSession != nil {
|
|
|
|
|
logger.Printf("Updated IF-MIB data for %d interfaces", len(m.stats))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Debugf("IF-MIB now contains %d interfaces", len(m.stats))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -452,37 +467,55 @@ func (m *InterfaceMIB) addIfXTable(iface *api.InterfaceCounters, idx int) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *InterfaceMIB) createAndRegisterSession(client *agentx.Client, oid, name string) (*agentx.Session, error) {
|
|
|
|
|
session, err := client.Session(value.MustParseOID(oid), name, m.handler)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to create %s session: %v", name, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = session.Register(127, value.MustParseOID(oid))
|
|
|
|
|
if err != nil {
|
|
|
|
|
session.Close()
|
|
|
|
|
return nil, fmt.Errorf("failed to register %s: %v", name, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return session, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *InterfaceMIB) RegisterWithClient(client *agentx.Client) error {
|
|
|
|
|
m.mutex.Lock()
|
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
|
|
// Create separate sessions for each MIB with the handler
|
|
|
|
|
ifEntrySession, err := client.Session(value.MustParseOID(ifEntryOID), "ifEntry", m.handler)
|
|
|
|
|
// Create and register sessions
|
|
|
|
|
ifEntrySession, err := m.createAndRegisterSession(client, ifEntryOID, "ifEntry")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to create ifEntry session: %v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ifXTableSession, err := client.Session(value.MustParseOID(ifXTableOID), "ifXTable", m.handler)
|
|
|
|
|
ifXTableSession, err := m.createAndRegisterSession(client, ifXTableOID, "ifXTable")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to create ifXTable session: %v", err)
|
|
|
|
|
ifEntrySession.Close()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.ifEntrySession = ifEntrySession
|
|
|
|
|
m.ifXTableSession = ifXTableSession
|
|
|
|
|
|
|
|
|
|
// Register the classic ifEntry
|
|
|
|
|
err = ifEntrySession.Register(127, value.MustParseOID(ifEntryOID))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to register ifEntry: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register the extended ifXTable
|
|
|
|
|
err = ifXTableSession.Register(127, value.MustParseOID(ifXTableOID))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to register ifXTable: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Debugf("Registered IF-MIB ifEntry at OID %s", ifEntryOID)
|
|
|
|
|
logger.Debugf("Registered IF-MIB ifXTable at OID %s", ifXTableOID)
|
|
|
|
|
logger.Debugf("Registered IF-MIB sessions: ifEntry (%s) and ifXTable (%s)", ifEntryOID, ifXTableOID)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close cleans up AgentX sessions
|
|
|
|
|
func (m *InterfaceMIB) Close() {
|
|
|
|
|
m.mutex.Lock()
|
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
|
|
if m.ifEntrySession != nil {
|
|
|
|
|
m.ifEntrySession.Close()
|
|
|
|
|
m.ifEntrySession = nil
|
|
|
|
|
}
|
|
|
|
|
if m.ifXTableSession != nil {
|
|
|
|
|
m.ifXTableSession.Close()
|
|
|
|
|
m.ifXTableSession = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|