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

View File

@ -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 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 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 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 ## Build Instructions
@ -61,10 +62,10 @@ CGO_ENABLED=0 go build -ldflags "-X main.version=${VERSION} -X main.buildTime=${
./vpp-snmp-agent ./vpp-snmp-agent
# Run with custom AgentX address # 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 # 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 ### Command Line Flags
@ -73,10 +74,15 @@ CGO_ENABLED=0 go build -ldflags "-X main.version=${VERSION} -X main.buildTime=${
| Flag | Default | Description | | Flag | Default | Description |
|------|---------|-------------| |------|---------|-------------|
| `-agentx-addr` | `localhost:705` | AgentX master agent address (hostname:port or Unix socket path) |
| `-debug` | `false` | Enable debug logging | | `-debug` | `false` | Enable debug logging |
| `-vppcfg` | `""` | VPP configuration YAML file to read interface descriptions from | | `-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 #### VPP Statistics Module Flags
| Flag | Default | Description | | Flag | Default | Description |
@ -105,7 +111,7 @@ CGO_ENABLED=0 go build -ldflags "-X main.version=${VERSION} -X main.buildTime=${
# Full configuration # Full configuration
./vpp-snmp-agent \ ./vpp-snmp-agent \
-agentx-addr /var/agentx/master \ -agentx.addr /var/agentx/master \
-debug \ -debug \
-vppcfg /etc/vpp/vppcfg.yaml \ -vppcfg /etc/vpp/vppcfg.yaml \
-vppstats.addr /var/run/vpp/stats.sock \ -vppstats.addr /var/run/vpp/stats.sock \

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
}

33
main.go
View File

@ -3,18 +3,14 @@ package main
import ( import (
"flag" "flag"
"log" "log"
"strings"
"time"
"github.com/posteo/go-agentx"
"govpp-snmp-example/agentx"
"govpp-snmp-example/config" "govpp-snmp-example/config"
"govpp-snmp-example/ifmib" "govpp-snmp-example/ifmib"
"govpp-snmp-example/vppstats" "govpp-snmp-example/vppstats"
) )
func main() { 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") debug := flag.Bool("debug", false, "Enable debug logging")
vppcfg := flag.String("vppcfg", "", "VPP configuration YAML file to read interface descriptions from") vppcfg := flag.String("vppcfg", "", "VPP configuration YAML file to read interface descriptions from")
flag.Parse() flag.Parse()
@ -22,22 +18,6 @@ func main() {
// Set global debug flag // Set global debug flag
config.Debug = *debug 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 // Create the interface MIB
interfaceMIB := ifmib.NewInterfaceMIB() interfaceMIB := ifmib.NewInterfaceMIB()
@ -48,15 +28,14 @@ func main() {
} }
} }
// Register the interface MIB with the AgentX client // Start AgentX routine
if err := interfaceMIB.RegisterWithClient(client); err != nil { if err := agentx.StartAgentXRoutine(interfaceMIB); err != nil {
log.Fatalf("Failed to register interface MIB: %v", err) log.Fatalf("Failed to start AgentX: %v", err)
} }
// Start VPP stats routine with callback to update MIB // Start VPP stats routine with callback to update MIB
vppstats.StartStatsRoutine(interfaceMIB.UpdateStats) vppstats.StartStatsRoutine(interfaceMIB.UpdateStats)
for { // Keep the main routine running
time.Sleep(100 * time.Millisecond) select {}
}
} }