move agentx into its own directory, simplify main.go

This commit is contained in:
Pim van Pelt
2025-06-10 13:28:30 +02:00
parent 458168e308
commit 467975b9d6
3 changed files with 64 additions and 33 deletions

46
agentx/agentx.go Normal file
View 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
}