Move flags to their own modules
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
vpp-snmp-agent
|
vpp-snmp-agent
|
||||||
|
govpp-snmp-example
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"go.fd.io/govpp/api"
|
"go.fd.io/govpp/api"
|
||||||
|
|
||||||
"govpp-snmp-example/logger"
|
"govpp-snmp-example/logger"
|
||||||
|
"govpp-snmp-example/vppstats"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IF-MIB OID bases:
|
// IF-MIB OID bases:
|
||||||
@ -65,14 +66,12 @@ type InterfaceMIB struct {
|
|||||||
ifEntrySession *agentx.Session
|
ifEntrySession *agentx.Session
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewInterfaceMIB(indexOffset int) *InterfaceMIB {
|
func NewInterfaceMIB() *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,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +109,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) {
|
func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) {
|
||||||
idx := int(iface.InterfaceIndex) + m.indexOffset
|
idx := int(iface.InterfaceIndex) + *vppstats.IfIndexOffset
|
||||||
|
|
||||||
// Add ifEntry (classic interface table) entries
|
// Add ifEntry (classic interface table) entries
|
||||||
m.addIfEntry(iface, idx)
|
m.addIfEntry(iface, idx)
|
||||||
|
7
main.go
7
main.go
@ -15,9 +15,6 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
addr := flag.String("agentx-addr", "localhost:705", "Address to connect to (hostname:port or Unix socket path)")
|
addr := flag.String("agentx-addr", "localhost:705", "Address to connect to (hostname:port or Unix 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")
|
|
||||||
ifIndexOffset := flag.Int("vpp-ifindex-offset", 1000, "Offset to add to VPP interface indices for SNMP")
|
|
||||||
debug := flag.Bool("debug", false, "Enable debug logging")
|
debug := flag.Bool("debug", false, "Enable debug logging")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@ -41,7 +38,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()
|
||||||
|
|
||||||
// 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 {
|
||||||
@ -49,7 +46,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(interfaceMIB.UpdateStats)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package vppstats
|
package vppstats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -13,16 +14,24 @@ import (
|
|||||||
|
|
||||||
type StatsCallback func(*api.InterfaceStats)
|
type StatsCallback func(*api.InterfaceStats)
|
||||||
|
|
||||||
// StartStatsRoutine starts a goroutine that queries VPP interface stats at the specified interval
|
var (
|
||||||
func StartStatsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback) {
|
// Flags for VPP stats configuration
|
||||||
go statsRoutine(statsSocketPath, period, callback)
|
StatsAddr = flag.String("vppstats.addr", "/var/run/vpp/stats.sock", "VPP stats socket path")
|
||||||
|
IfIndexOffset = flag.Int("vppstats.ifindex-offset", 1000, "Offset to add to VPP interface indices for SNMP")
|
||||||
|
Period = flag.Int("vppstats.period", 10, "Interval in seconds for querying VPP interface stats")
|
||||||
|
)
|
||||||
|
|
||||||
|
// StartStatsRoutine starts a goroutine that queries VPP interface stats at the configured interval
|
||||||
|
func StartStatsRoutine(callback StatsCallback) {
|
||||||
|
period := time.Duration(*Period) * time.Second
|
||||||
|
go statsRoutine(period, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback) {
|
func statsRoutine(period time.Duration, callback StatsCallback) {
|
||||||
logger.Debugf("Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period)
|
logger.Debugf("Starting VPP stats routine with socket: %s, period: %v", *StatsAddr, period)
|
||||||
|
|
||||||
// Create stats client
|
// Create stats client
|
||||||
client := statsclient.NewStatsClient(statsSocketPath)
|
client := statsclient.NewStatsClient(*StatsAddr)
|
||||||
|
|
||||||
// Connect using core.ConnectStats (proper way)
|
// Connect using core.ConnectStats (proper way)
|
||||||
c, err := core.ConnectStats(client)
|
c, err := core.ConnectStats(client)
|
||||||
|
Reference in New Issue
Block a user