Files
govpp-snmp-agentx/main.go
2025-06-09 17:13:07 +02:00

101 lines
2.6 KiB
Go

package main
import (
"flag"
"log"
"net"
"strings"
"time"
"github.com/posteo/go-agentx"
"github.com/posteo/go-agentx/pdu"
"github.com/posteo/go-agentx/value"
"govpp-snmp-example/vppstats"
)
func main() {
addr := flag.String("addr", "localhost:705", "Address to connect to (hostname:port or Unix socket path)")
flag.Parse()
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
session, err := client.Session()
if err != nil {
log.Fatalf("Failed to create session: %v", err)
}
listHandler := &agentx.ListHandler{}
item := listHandler.Add("1.3.6.1.4.1.45995.3.0")
item.Type = pdu.VariableTypeInteger
item.Value = int32(-123)
item = listHandler.Add("1.3.6.1.4.1.45995.3.1")
item.Type = pdu.VariableTypeOctetString
item.Value = "echo test"
item = listHandler.Add("1.3.6.1.4.1.45995.3.2")
item.Type = pdu.VariableTypeOctetString
item.Value = "1.3.6.1.4.1.45995.3.0.5"
item = listHandler.Add("1.3.6.1.4.1.45995.3.3")
item.Type = pdu.VariableTypeIPAddress
item.Value = net.IP{10, 10, 10, 10}
item = listHandler.Add("1.3.6.1.4.1.45995.3.4")
item.Type = pdu.VariableTypeCounter32
item.Value = uint32(123)
item = listHandler.Add("1.3.6.1.4.1.45995.3.5")
item.Type = pdu.VariableTypeGauge32
item.Value = uint32(123)
item = listHandler.Add("1.3.6.1.4.1.45995.3.6")
item.Type = pdu.VariableTypeTimeTicks
item.Value = 123 * time.Second
item = listHandler.Add("1.3.6.1.4.1.45995.3.7")
item.Type = pdu.VariableTypeOpaque
item.Value = []byte{1, 2, 3}
item = listHandler.Add("1.3.6.1.4.1.45995.3.8")
item.Type = pdu.VariableTypeCounter64
item.Value = uint64(1234567890123456)
item = listHandler.Add("1.3.6.1.4.1.45995.3.9")
item.Type = pdu.VariableTypeCounter64
item.Value = uint64(12345)
item = listHandler.Add("1.3.6.1.4.1.45995.3.10")
item.Type = pdu.VariableTypeOctetString
item.Value = "hoi pim"
session.Handler = listHandler
if err := session.Register(127, value.MustParseOID("1.3.6.1.4.1.45995.3")); err != nil {
log.Fatalf("Failed to register: %v", err)
}
// Start VPP stats routine
vppstats.StartStatsRoutine()
for {
time.Sleep(100 * time.Millisecond)
}
}