move agentx into its own directory, simplify main.go
This commit is contained in:
46
agentx/agentx.go
Normal file
46
agentx/agentx.go
Normal file
@ -0,0 +1,46 @@
|
||||
package agentx
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/posteo/go-agentx"
|
||||
|
||||
"govpp-snmp-example/ifmib"
|
||||
"govpp-snmp-example/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
// Flags for AgentX configuration
|
||||
AgentXAddr = flag.String("agentx.addr", "localhost:705", "Address to connect to (hostname:port or Unix socket path)")
|
||||
)
|
||||
|
||||
// StartAgentXRoutine initializes the AgentX client and registers the interface MIB
|
||||
func StartAgentXRoutine(interfaceMIB *ifmib.InterfaceMIB) error {
|
||||
var network, address string
|
||||
if strings.HasPrefix(*AgentXAddr, "/") {
|
||||
network = "unix"
|
||||
address = *AgentXAddr
|
||||
} else {
|
||||
network = "tcp"
|
||||
address = *AgentXAddr
|
||||
}
|
||||
|
||||
logger.Debugf("Connecting to AgentX at %s://%s", network, address)
|
||||
|
||||
client, err := agentx.Dial(network, address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
client.Timeout = 1 * time.Minute
|
||||
client.ReconnectInterval = 1 * time.Second
|
||||
|
||||
// Register the interface MIB with the AgentX client
|
||||
if err := interfaceMIB.RegisterWithClient(client); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Printf("Successfully registered with AgentX at %s://%s", network, address)
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user