From 467975b9d6bd02843fbdf91d5b458b82672d7fee Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Tue, 10 Jun 2025 13:28:30 +0200 Subject: [PATCH] move agentx into its own directory, simplify main.go --- README.md | 18 ++++++++++++------ agentx/agentx.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 33 ++++++--------------------------- 3 files changed, 64 insertions(+), 33 deletions(-) create mode 100644 agentx/agentx.go diff --git a/README.md b/README.md index 8670a7d..b83c5fc 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,12 @@ A Go application that bridges VPP (Vector Packet Processing) interface statistic VPP Stats Socket → VPP Stats Client → Interface MIB → AgentX → SNMP Master Agent ``` -The application consists of three main components: +The application consists of four main components: 1. **VPP Stats Client** (`vppstats/`): Connects to VPP stats socket and retrieves interface counters 2. **Interface MIB** (`ifmib/`): Maps VPP statistics to SNMP IF-MIB structure -3. **AgentX Client**: Registers with SNMP master agent to serve the MIB data +3. **AgentX Client** (`agentx/`): Handles AgentX protocol connection and MIB registration +4. **Main Application**: Orchestrates the components and handles configuration ## Build Instructions @@ -61,10 +62,10 @@ CGO_ENABLED=0 go build -ldflags "-X main.version=${VERSION} -X main.buildTime=${ ./vpp-snmp-agent # Run with custom AgentX address -./vpp-snmp-agent -agentx-addr 127.0.0.1:705 +./vpp-snmp-agent -agentx.addr 127.0.0.1:705 # Run with Unix socket AgentX connection -./vpp-snmp-agent -agentx-addr /var/agentx/master +./vpp-snmp-agent -agentx.addr /var/agentx/master ``` ### Command Line Flags @@ -73,10 +74,15 @@ CGO_ENABLED=0 go build -ldflags "-X main.version=${VERSION} -X main.buildTime=${ | Flag | Default | Description | |------|---------|-------------| -| `-agentx-addr` | `localhost:705` | AgentX master agent address (hostname:port or Unix socket path) | | `-debug` | `false` | Enable debug logging | | `-vppcfg` | `""` | VPP configuration YAML file to read interface descriptions from | +#### AgentX Module Flags + +| Flag | Default | Description | +|------|---------|-------------| +| `-agentx.addr` | `localhost:705` | AgentX master agent address (hostname:port or Unix socket path) | + #### VPP Statistics Module Flags | Flag | Default | Description | @@ -105,7 +111,7 @@ CGO_ENABLED=0 go build -ldflags "-X main.version=${VERSION} -X main.buildTime=${ # Full configuration ./vpp-snmp-agent \ - -agentx-addr /var/agentx/master \ + -agentx.addr /var/agentx/master \ -debug \ -vppcfg /etc/vpp/vppcfg.yaml \ -vppstats.addr /var/run/vpp/stats.sock \ diff --git a/agentx/agentx.go b/agentx/agentx.go new file mode 100644 index 0000000..1170e0d --- /dev/null +++ b/agentx/agentx.go @@ -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 +} \ No newline at end of file diff --git a/main.go b/main.go index 2dd437d..3b03808 100644 --- a/main.go +++ b/main.go @@ -3,18 +3,14 @@ package main import ( "flag" "log" - "strings" - "time" - - "github.com/posteo/go-agentx" + "govpp-snmp-example/agentx" "govpp-snmp-example/config" "govpp-snmp-example/ifmib" "govpp-snmp-example/vppstats" ) func main() { - addr := flag.String("agentx-addr", "localhost:705", "Address to connect to (hostname:port or Unix socket path)") debug := flag.Bool("debug", false, "Enable debug logging") vppcfg := flag.String("vppcfg", "", "VPP configuration YAML file to read interface descriptions from") flag.Parse() @@ -22,22 +18,6 @@ func main() { // Set global debug flag config.Debug = *debug - var network, address string - if strings.HasPrefix(*addr, "/") { - network = "unix" - address = *addr - } else { - network = "tcp" - address = *addr - } - - client, err := agentx.Dial(network, address) - if err != nil { - log.Fatalf("Failed to dial %s %s: %v", network, address, err) - } - client.Timeout = 1 * time.Minute - client.ReconnectInterval = 1 * time.Second - // Create the interface MIB interfaceMIB := ifmib.NewInterfaceMIB() @@ -48,15 +28,14 @@ func main() { } } - // Register the interface MIB with the AgentX client - if err := interfaceMIB.RegisterWithClient(client); err != nil { - log.Fatalf("Failed to register interface MIB: %v", err) + // Start AgentX routine + if err := agentx.StartAgentXRoutine(interfaceMIB); err != nil { + log.Fatalf("Failed to start AgentX: %v", err) } // Start VPP stats routine with callback to update MIB vppstats.StartStatsRoutine(interfaceMIB.UpdateStats) - for { - time.Sleep(100 * time.Millisecond) - } + // Keep the main routine running + select {} }