agentx

package module
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2025 License: LGPL-3.0 Imports: 12 Imported by: 0

README

AgentX

Documentation

A library with a pure Go implementation of the AgentX-Protocol. The library is not yet feature-complete, but should be far enough to used in a production environment.

The AgentX-Protocol can be used to extend a snmp-daemon such that it dispatches the requests to an OID-subtree to your Go application. Those requests are than handled by this library and can be replied with metrics about your applications state.

State

The library implements all variable types (Integer, OctetString, Null, ObjectIdentifier, IPAddress, Counter32, Gauge32, TimeTicks, Opaque, Counter64, NoSuchObject, NoSuchInstance, EndOfMIBView), and all requests (Get, GetNext, GetBulk, Set operations, and Traps).

Helper

In order to provided metrics, your have to implement the agentx.Handler interface. For convenience, you can use the agentx.ListHandler implementation, which takes a list of OIDs and values and serves them if requested. An example is listed below.

Example

package main

import (
    "log"
    "net"
    "time"

    "github.com/LJS360d/go-agentx"
    "github.com/LJS360d/go-agentx/pdu"
    "github.com/LJS360d/go-agentx/value"
)

func main() {
    client, err := agentx.Dial("tcp", "localhost:705",
        agentx.WithTimeout(1 * time.Minute),
        agentx.WithReconnectInterval(1 * time.Second))
    if err != nil {
        log.Fatal(err)
    }

    listHandler := &agentx.ListHandler{}

    item := listHandler.Add("1.3.6.1.4.1.45995.3.1")
    item.Type = pdu.VariableTypeInteger
    item.Value = int32(-123)

    item = listHandler.Add("1.3.6.1.4.1.45995.3.2")
    item.Type = pdu.VariableTypeOctetString
    item.Value = "echo test"

    item = listHandler.Add("1.3.6.1.4.1.45995.3.3")
    item.Type = pdu.VariableTypeNull
    item.Value = nil

    item = listHandler.Add("1.3.6.1.4.1.45995.3.4")
    item.Type = pdu.VariableTypeObjectIdentifier
    item.Value = "1.3.6.1.4.1.45995.1.5"

    item = listHandler.Add("1.3.6.1.4.1.45995.3.5")
    item.Type = pdu.VariableTypeIPAddress
    item.Value = net.IP{10, 10, 10, 10}

    item = listHandler.Add("1.3.6.1.4.1.45995.3.6")
    item.Type = pdu.VariableTypeCounter32
    item.Value = uint32(123)

    item = listHandler.Add("1.3.6.1.4.1.45995.3.7")
    item.Type = pdu.VariableTypeGauge32
    item.Value = uint32(123)

    item = listHandler.Add("1.3.6.1.4.1.45995.3.8")
    item.Type = pdu.VariableTypeTimeTicks
    item.Value = 123 * time.Second

    item = listHandler.Add("1.3.6.1.4.1.45995.3.9")
    item.Type = pdu.VariableTypeOpaque
    item.Value = []byte{1, 2, 3}

    item = listHandler.Add("1.3.6.1.4.1.45995.3.10")
    item.Type = pdu.VariableTypeCounter64
    item.Value = uint64(12345678901234567890)

    session, err := client.Session(value.MustParseOID("1.3.6.1.4.1.45995"), "test client", listHandler)
    if err != nil {
        log.Fatal(err)
    }

    if err := session.Register(127, value.MustParseOID("1.3.6.1.4.1.45995.3")); err != nil {
        log.Fatal(err)
    }

    for {
        time.Sleep(100 * time.Millisecond)
    }
}

Connection lost

If the connection to the snmp-daemon is lost, the client tries to reconnect. Therefor the property ReconnectInterval has be set. It specifies a duration that is waited before a re-connect is tried. If the client has open session or registrations, the client try to re-establish both on a successful re-connect.

Project

The implementation was provided by simia.tech (haftungsbeschränkt).

License

The project is licensed under LGPL 3.0 (see LICENSE file).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PacketID

func PacketID(ctx context.Context) uint32

func SessionID

func SessionID(ctx context.Context) uint32

func TransactionID

func TransactionID(ctx context.Context) uint32

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client defines an agentx client.

func Dial

func Dial(network, address string, opts ...DialOption) (*Client, error)

Dial connects to the provided agentX endpoint.

func (*Client) Close

func (c *Client) Close() error

Close tears down the client.

func (*Client) Error added in v0.3.3

func (c *Client) Error() <-chan error

func (*Client) Session

func (c *Client) Session(nameOID value.OID, name string, handler Handler) (*Session, error)

Session sets up a new session.

type DialOption

type DialOption func(o *dialOptions)

func WithErrorHandler added in v0.3.3

func WithErrorHandler(handler func(error)) DialOption

func WithLogger

func WithLogger(value *slog.Logger) DialOption

func WithReconnectInterval

func WithReconnectInterval(value time.Duration) DialOption

func WithTimeout

func WithTimeout(value time.Duration) DialOption

type Handler

Handler defines an interface for a handler of events that might occure during a session.

type ListHandler

type ListHandler struct {
	// contains filtered or unexported fields
}

ListHandler is a helper that takes a list of oids and implements a default behaviour for that list.

func (*ListHandler) Add

func (l *ListHandler) Add(oid string) *ListItem

Add adds a list item for the provided oid and returns it.

func (*ListHandler) Get

Get tries to find the provided oid and returns the corresponding value.

func (*ListHandler) GetNext

func (l *ListHandler) GetNext(ctx context.Context, from value.OID, includeFrom bool, to value.OID) (value.OID, pdu.VariableType, any, error)

GetNext tries to find the value that follows the provided oid and returns it.

func (*ListHandler) Set

func (l *ListHandler) Set(ctx context.Context, oid value.OID, t pdu.VariableType, value any) error

Set updates the value for the provided oid.

type ListItem

type ListItem struct {
	Type  pdu.VariableType
	Value interface{}
}

ListItem defines an item of the list handler.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session defines an agentx session.

func (*Session) Close

func (s *Session) Close() error

Close tears down the session with the master agent.

func (*Session) ID

func (s *Session) ID() uint32

ID returns the session id.

func (*Session) Register

func (s *Session) Register(priority byte, baseOID value.OID) error

Register registers the client under the provided rootID with the provided priority on the master agent.

func (*Session) SendTrap

func (s *Session) SendTrap(timestamp time.Duration, variables pdu.Variables) error

SendTrap sends a trap/notification to the master agent.

func (*Session) Unregister

func (s *Session) Unregister(priority byte, baseOID value.OID) error

Unregister removes the registration for the provided subtree.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL