Add -debug flag
This commit is contained in:
@ -65,13 +65,15 @@ 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) *InterfaceMIB {
|
func NewInterfaceMIB(indexOffset int, debug bool) *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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,7 +85,9 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) {
|
|||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
defer m.mutex.Unlock()
|
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
|
// Clear existing entries
|
||||||
m.handler = &agentx.ListHandler{}
|
m.handler = &agentx.ListHandler{}
|
||||||
@ -91,7 +95,9 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) {
|
|||||||
|
|
||||||
// Add new entries
|
// Add new entries
|
||||||
for _, iface := range interfaceStats.Interfaces {
|
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.stats[iface.InterfaceIndex] = &iface
|
||||||
m.addInterfaceToMIB(&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("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) {
|
func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) {
|
||||||
@ -117,7 +125,9 @@ 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)
|
||||||
|
|
||||||
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) {
|
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)
|
return fmt.Errorf("failed to register ifXTable: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Registered IF-MIB ifEntry at OID %s", ifEntryOID)
|
if m.debug {
|
||||||
log.Printf("Registered IF-MIB ifXTable at OID %s", ifXTableOID)
|
log.Printf("Registered IF-MIB ifEntry at OID %s", ifEntryOID)
|
||||||
|
log.Printf("Registered IF-MIB ifXTable at OID %s", ifXTableOID)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
5
main.go
5
main.go
@ -17,6 +17,7 @@ func main() {
|
|||||||
vppStatsAddr := flag.String("vpp-stats-addr", "/var/run/vpp/stats.sock", "VPP stats socket path")
|
vppStatsAddr := flag.String("vpp-stats-addr", "/var/run/vpp/stats.sock", "VPP stats socket path")
|
||||||
period := flag.Float64("period", 10.0, "Interval in seconds for querying VPP interface stats")
|
period := flag.Float64("period", 10.0, "Interval in seconds for querying VPP interface stats")
|
||||||
ifIndexOffset := flag.Int("vpp-ifindex-offset", 1000, "Offset to add to VPP interface indices for SNMP")
|
ifIndexOffset := flag.Int("vpp-ifindex-offset", 1000, "Offset to add to VPP interface indices for SNMP")
|
||||||
|
debug := flag.Bool("debug", false, "Enable debug logging")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
var network, address string
|
var network, address string
|
||||||
@ -36,7 +37,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)
|
interfaceMIB := ifmib.NewInterfaceMIB(*ifIndexOffset, *debug)
|
||||||
|
|
||||||
// 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 {
|
||||||
@ -44,7 +45,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)
|
vppstats.StartStatsRoutine(*vppStatsAddr, time.Duration(*period*1000)*time.Millisecond, interfaceMIB.UpdateStats, *debug)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
@ -12,12 +12,14 @@ 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) {
|
func StartStatsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback, debug bool) {
|
||||||
go statsRoutine(statsSocketPath, period, callback)
|
go statsRoutine(statsSocketPath, period, callback, debug)
|
||||||
}
|
}
|
||||||
|
|
||||||
func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback) {
|
func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback, debug bool) {
|
||||||
log.Printf("Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period)
|
if debug {
|
||||||
|
log.Printf("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)
|
||||||
@ -31,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)
|
queryInterfaceStats(c, callback, debug)
|
||||||
|
|
||||||
ticker := time.NewTicker(period)
|
ticker := time.NewTicker(period)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
@ -39,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)
|
queryInterfaceStats(c, callback, debug)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback) {
|
func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback, debug bool) {
|
||||||
// Create the proper struct for interface stats
|
// Create the proper struct for interface stats
|
||||||
stats := new(api.InterfaceStats)
|
stats := new(api.InterfaceStats)
|
||||||
|
|
||||||
@ -54,17 +56,21 @@ func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log basic info
|
// Always log basic info
|
||||||
log.Printf("Retrieved stats for %d interfaces", len(stats.Interfaces))
|
log.Printf("Retrieved stats for %d interfaces", len(stats.Interfaces))
|
||||||
for _, iface := range stats.Interfaces {
|
|
||||||
log.Printf("Interface %d (%s): RX %d pkts/%d bytes, TX %d pkts/%d bytes",
|
// Debug logging for individual interfaces
|
||||||
iface.InterfaceIndex, iface.InterfaceName,
|
if debug {
|
||||||
iface.Rx.Packets, iface.Rx.Bytes,
|
for _, iface := range stats.Interfaces {
|
||||||
iface.Tx.Packets, iface.Tx.Bytes)
|
log.Printf("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
|
// Call the callback to update the MIB
|
||||||
if callback != nil {
|
if callback != nil {
|
||||||
callback(stats)
|
callback(stats)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user