snmp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2020 License: MIT Imports: 11 Imported by: 0

README

Documentation Build Status CircleCI codecov GolangCI Go Report Card GitHub tag (latest SemVer)

snmp

Simple but powerful snmp client.

Minimal example

conn, err := net.ListenPacket("udp", *addr)
if err != nil { /* ... */}

c := snmp.NewClient(conn)
defer c.Close() // <- conn is Closed here

go c.Run() // read incoming packets.

root, err := asn1.ParseOID(os.Args[1])
if err != nil { /* ... */}

p, err := c.Walk(a, root)
if err != nil {
	log.Fatalf("walk: %v", err)
}

for _, v := range p.Vars {
	fmt.Printf("%v\n", v)
}

See full example in ./cmd/ dir.

Documentation

Index

Constants

View Source
const (
	NoError = iota
	TooBigError
	NoSuchNameError
	BadValueError
	ReadOnlyError
	GeneralError
)

Variables

View Source
var (
	ErrReadTimeout = errors.New("timeout")
	ErrExtraData   = errors.New("extra data")
	ErrClosed      = errors.New("closed client")
)

Functions

func CommandString

func CommandString(t Command) string

CommandString formats Command as a string

func Dumper

func Dumper(conn net.PacketConn) error

func TypeString

func TypeString(t Type) string

TypeString formats Type as a string

Types

type Client

type Client struct {
	Receive chan<- Packet

	ReadTimeout time.Duration
	Retries     int

	Version   Version
	Community string

	NonRepeaters   int
	MaxRepetitions int
	// contains filtered or unexported fields
}

func NewClient

func NewClient(conn net.PacketConn) *Client

NewClient creates SNMP client with default options

func (*Client) Call

func (c *Client) Call(addr net.Addr, p *PDU) (_ *PDU, err error)

Call sends Protocol Data Unit to address and waits for response for c.ReadTimeout

func (*Client) Close

func (c *Client) Close() error

Close stops Run and closes connection

func (*Client) Read

func (c *Client) Read(buf []byte, p *PDU) (net.Addr, *PDU, error)

Read reads Protocol Data Unit from connection

func (*Client) Run

func (c *Client) Run()

Run does background tasks reqiured for work. It's blocked until Client Closed

func (*Client) Send

func (c *Client) Send(addr net.Addr, p *PDU) error

Send sends Protocol Data Unit to addr

func (*Client) Walk

func (c *Client) Walk(addr net.Addr, root asn1.OID, args ...interface{}) (*PDU, error)

Walk scans addr for variables with prefix root. It retries on fail and downgrade protocol version if no answer. Additional arguments are supported:

*PDU - use that pdu instead of default
Version - set protocol version
Command - set command
*Telemetry - collect telemetry of requests

type Command

type Command = Type
const (
	Get      Command = asn1.Context | asn1.Constructor | 0x00
	GetNext  Command = asn1.Context | asn1.Constructor | 0x01
	Response Command = asn1.Context | asn1.Constructor | 0x02
	Set      Command = asn1.Context | asn1.Constructor | 0x03
	Trap     Command = asn1.Context | asn1.Constructor | 0x04
	GetBulk  Command = asn1.Context | asn1.Constructor | 0x05
)

type PDU

type PDU struct {
	Version   Version
	Community string

	Command Command
	ReqID   int32

	NonRepeaters, MaxRepetitions int

	ErrorStatus, ErrorIndex int

	Vars []Var
}

func (*PDU) Decode

func (p *PDU) Decode(b []byte) (err error)

Decode decodes Protocol Data Unit from the buffer

func (*PDU) Dump

func (p *PDU) Dump() string

Dump returns Protocol Data Unit header info and all Variables

func (*PDU) EncodeTo

func (p *PDU) EncodeTo(b []byte) []byte

EncodeTo encodes Protocol Data Unit to buffer. If buffer is nil or not big enough new buffer is allocated.

func (*PDU) String

func (p *PDU) String() string

type Packet

type Packet struct {
	Addr net.Addr
	PDU  *PDU
	Err  error
}

type Telemetry

type Telemetry struct {
	Requests *quantile.Stream
	Duration time.Duration
	Errors   int
}

func NewTelemetry

func NewTelemetry() *Telemetry

NewTelemetry returns inited telemetry object

type Type

type Type = asn1.Type
const (
	Boolean     Type = asn1.Universal | 0x1
	Integer     Type = asn1.Universal | 0x2
	BitString   Type = asn1.Universal | 0x3
	OctetString Type = asn1.Universal | 0x4
	Null        Type = asn1.Universal | 0x5
	ObjectID    Type = asn1.Universal | 0x6
	Sequence    Type = asn1.Universal | 0x10
	TypeSet     Type = asn1.Universal | 0x11

	IPAddress  Type = asn1.Application | 0x0
	Counter    Type = asn1.Application | 0x1
	Gauge      Type = asn1.Application | 0x2
	Timeticks  Type = asn1.Application | 0x3
	Opaque     Type = asn1.Application | 0x4
	Counter64  Type = asn1.Application | 0x6
	Float      Type = asn1.Application | 0x8
	Double     Type = asn1.Application | 0x9
	Integer64  Type = asn1.Application | 0x10
	Unsigned64 Type = asn1.Application | 0x11

	NoSuchObject   Type = asn1.Context | asn1.Primitive | 0x0
	NoSuchInstance Type = asn1.Context | asn1.Primitive | 0x1
	EndOfMIBView   Type = asn1.Context | asn1.Primitive | 0x2
)

type Var

type Var struct {
	ObjectID asn1.OID
	Type     asn1.Type
	Value    interface{}
}

func (*Var) Decode

func (v *Var) Decode(b []byte) ([]byte, error)

Decode decodes Variable from the buffer

func (*Var) EncodeTo

func (v *Var) EncodeTo(b []byte) []byte

EncodeTo encodes Variable to buffer. If buffer is nil or not big enough new buffer is allocated.

func (Var) String

func (v Var) String() string

type Version

type Version int
const (
	Version1  Version = 0
	Version2c Version = 1
	Version3  Version = 3
)

func ParseVersion

func ParseVersion(s string) (Version, error)

func (Version) String

func (v Version) String() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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