Move Go code to src/
This commit is contained in:
48
src/agentx/agentx.go
Normal file
48
src/agentx/agentx.go
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright 2025, IPng Networks GmbH, Pim van Pelt <pim@ipng.ch>
|
||||
|
||||
package agentx
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/posteo/go-agentx"
|
||||
|
||||
"govpp-snmp-agentx/ifmib"
|
||||
"govpp-snmp-agentx/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
|
||||
}
|
54
src/agentx/agentx_test.go
Normal file
54
src/agentx/agentx_test.go
Normal file
@ -0,0 +1,54 @@
|
||||
// Copyright 2025, IPng Networks GmbH, Pim van Pelt <pim@ipng.ch>
|
||||
|
||||
package agentx
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAgentXAddrFlag(t *testing.T) {
|
||||
// Test that the flag is registered with correct default
|
||||
if *AgentXAddr != "localhost:705" {
|
||||
t.Errorf("Expected default AgentX address to be 'localhost:705', got '%s'", *AgentXAddr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentXAddrFlagParsing(t *testing.T) {
|
||||
// Save original flag value
|
||||
originalAddr := *AgentXAddr
|
||||
defer func() { *AgentXAddr = originalAddr }()
|
||||
|
||||
// Test Unix socket path
|
||||
testAddr := "/var/run/test.sock"
|
||||
*AgentXAddr = testAddr
|
||||
|
||||
if *AgentXAddr != testAddr {
|
||||
t.Errorf("Expected AgentX address to be '%s', got '%s'", testAddr, *AgentXAddr)
|
||||
}
|
||||
|
||||
// Test TCP address
|
||||
testAddr = "192.168.1.1:705"
|
||||
*AgentXAddr = testAddr
|
||||
|
||||
if *AgentXAddr != testAddr {
|
||||
t.Errorf("Expected AgentX address to be '%s', got '%s'", testAddr, *AgentXAddr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlagRegistration(t *testing.T) {
|
||||
// Test that our flag is properly registered
|
||||
f := flag.Lookup("agentx.addr")
|
||||
if f == nil {
|
||||
t.Error("Expected agentx.addr flag to be registered")
|
||||
return
|
||||
}
|
||||
|
||||
if f.DefValue != "localhost:705" {
|
||||
t.Errorf("Expected flag default value to be 'localhost:705', got '%s'", f.DefValue)
|
||||
}
|
||||
|
||||
if f.Usage != "Address to connect to (hostname:port or Unix socket path)" {
|
||||
t.Errorf("Unexpected flag usage string: %s", f.Usage)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user