snmplib

package module
v0.0.0-...-c71fe8c Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2020 License: Apache-2.0 Imports: 20 Imported by: 1

README

SNMP lib: SNMP client and trap receiver for golang

Currently supported operations:

  • SNMP v1/v2c/v3 trap receiver with V3 EngineID auto discovery
  • SNMP v1/v2c Get, GetMultiple, GetNext, GetBulk, Walk
  • SNMP V3 Get, Walk, GetNext

SNMP trap receiver server

This package includes a helper for running a SNMP trap receiver server. See trapserver.go for more details. Note that the server does not perform any Community verification. This can be done manually in the OnTrap function using the provided Trap object.

Using the code

  • The *_test.go files provide good examples of how to use these functions
  • Files under examples/ contain the several examples, including an example trap server.

Not supported yet:

  • SNMP Informs receiver
  • SNMP v3 GetMultiple, GetBulk (these can be easily implemented since SNMP v3 Walk/Get/GetNext is working)

Documentation

Overview

Package snmplib provides an SNMP query and trapper library.

Index

Constants

View Source
const (
	SnmpAES    string = "AES"
	SnmpDES    string = "DES"
	SnmpNOPRIV string = "NOPRIV"
	SnmpSHA1   string = "SHA1"
	SnmpMD5    string = "MD5"
)

SNMP constants.

Variables

This section is empty.

Functions

func DecodeCounter64

func DecodeCounter64(toparse []byte) (uint64, error)

DecodeCounter64 decodes a counter64.

func DecodeIPAddress

func DecodeIPAddress(toparse []byte) (string, error)

DecodeIPAddress decodes an IP address.

func DecodeInteger

func DecodeInteger(toparse []byte) (int, error)

DecodeInteger decodes an integer. Will error out if it's longer than 64 bits.

func DecodeIntegerSigned

func DecodeIntegerSigned(toparse []byte) (int, error)

DecodeIntegerSigned decodes a signed integer. Will error out if it's longer than 64 bits.

func DecodeLength

func DecodeLength(toparse []byte) (int, int, error)

DecodeLength returns the length and the length of the length or an error. Caveats: Does not support indefinite length. Couldn't find any SNMP packet dump actually using that.

func DecodeSequence

func DecodeSequence(toparse []byte) ([]interface{}, error)

DecodeSequence decodes BER binary data into into *[]interface{}.

func DoGetTableTest

func DoGetTableTest(target string)

func DoGetTest

func DoGetTest(target string)

func DoGetTestV3

func DoGetTestV3(target string, oidstr, username, authAlg, authKey, privAlg, privKey string)

func DoWalkTest

func DoWalkTest(target string)

func DoWalkTestV3

func DoWalkTestV3(target string, oidstr, username, authAlg, authKey, privAlg, privKey string)

func EncodeInteger

func EncodeInteger(toEncode int) []byte

EncodeInteger encodes an integer to BER format.

func EncodeLength

func EncodeLength(length int) []byte

EncodeLength encodes an integer value as a BER compliant length value.

func EncodeSequence

func EncodeSequence(toEncode []interface{}) ([]byte, error)

EncodeSequence will encode an []interface{} into an SNMP bytestream.

func NewUdpStub

func NewUdpStub(t *testing.T) *udpStub

NewUdpStub creates a new udpStub.

Types

type BERType

type BERType uint8

BERType constants for the Type of the TLV field.

const (
	AsnBoolean     BERType = 0x01
	AsnInteger     BERType = 0x02
	AsnBitStr      BERType = 0x03
	AsnOctetStr    BERType = 0x04
	AsnNull        BERType = 0x05
	AsnObjectID    BERType = 0x06
	AsnSequence    BERType = 0x10
	AsnSet         BERType = 0x11
	AsnUniversal   BERType = 0x00
	AsnApplication BERType = 0x40
	AsnContext     BERType = 0x80
	AsnPrivate     BERType = 0xC0
	AsnPrimitive   BERType = 0x00
	AsnConstructor BERType = 0x20

	AsnLongLen     BERType = 0x80
	AsnExtensionID BERType = 0x1F
	AsnBit8        BERType = 0x80

	Integer     BERType = AsnUniversal | 0x02
	Integer32   BERType = AsnUniversal | 0x02
	Bitstring   BERType = AsnUniversal | 0x03
	Octetstring BERType = AsnUniversal | 0x04
	Null        BERType = AsnUniversal | 0x05
	UOid        BERType = AsnUniversal | 0x06
	Sequence    BERType = AsnConstructor | 0x10

	Ipaddress BERType = AsnApplication | 0x00
	Counter   BERType = AsnApplication | 0x01
	Counter32 BERType = AsnApplication | 0x01
	Gauge     BERType = AsnApplication | 0x02
	Gauge32   BERType = AsnApplication | 0x02
	Timeticks BERType = AsnApplication | 0x03
	Opaque    BERType = AsnApplication | 0x04
	Counter64 BERType = AsnApplication | 0x06

	AsnGetRequest     BERType = 0xa0
	AsnGetNextRequest BERType = 0xa1
	AsnGetResponse    BERType = 0xa2
	AsnSetRequest     BERType = 0xa3
	AsnTrap           BERType = 0xa4
	AsnGetBulkRequest BERType = 0xa5
	AsnInform         BERType = 0xa6
	AsnTrap2          BERType = 0xa7
	AsnReport         BERType = 0xa8
)

Constants for the different types of the TLV fields.

type Oid

type Oid []int

The SNMP object identifier type.

func DecodeOid

func DecodeOid(raw []byte) (*Oid, error)

DecodeOid decodes a ASN.1 BER raw oid into an Oid instance.

func MustParseOid

func MustParseOid(o string) Oid

MustParseOid parses a string oid to an Oid instance. Panics on error.

func ParseOid

func ParseOid(oid string) (Oid, error)

ParseOid a text format oid into an Oid instance.

func (Oid) Copy

func (o Oid) Copy() Oid

Copy copies an oid into a new object instance.

func (Oid) Encode

func (o Oid) Encode() ([]byte, error)

Encode encodes the oid into an ASN.1 BER byte array.

func (Oid) String

func (o Oid) String() string

String returns the string representation for this oid object.

func (Oid) Within

func (o Oid) Within(other Oid) bool
Within determines if an oid has this oid instance as a prefix.

E.g. MustParseOid("1.2.3").Within("1.2") => true.

type SNMP

type SNMP struct {
	Target    string      // Target device for these SNMP events.
	Community string      // Community to use to contact the device.
	Version   SNMPVersion // SNMPVersion to encode in the packets.

	TrapUsers []V3user
	// contains filtered or unexported fields
}

SNMP object type that lets you do SNMP requests.

func NewSNMP

func NewSNMP(target, community string, version SNMPVersion, timeout time.Duration, retries int) (*SNMP, error)

NewSNMP creates a new SNMP object. Opens a UDP connection to the device that will be used for the SNMP packets.

func NewSNMPOnConn

func NewSNMPOnConn(target, community string, version SNMPVersion, timeout time.Duration, retries int, conn net.Conn) *SNMP

NewSNMPOnConn creates a new SNMP object from an existing net.Conn. It does not check if the provided target is valid.

func NewSNMPv3

func NewSNMPv3(target, user, authAlg, authPwd, privAlg, privPwd string, timeout time.Duration, retries int) (*SNMP, error)

NewSNMPv3 creates a new SNMP object for SNMPv3. Opens a UDP connection to the device that will be used for the SNMP packets.

func (SNMP) Close

func (w SNMP) Close() error

Close the net.conn in SNMP.

func (*SNMP) Discover

func (w *SNMP) Discover() error

Discover : SNMP V3 requires a discover packet being sent before a request being sent, so that agent's engineID and other parameters can be automatically detected.

func (SNMP) Get

func (w SNMP) Get(oid Oid) (interface{}, error)

Get sends an SNMP get request requesting the value for an oid.

func (SNMP) GetBulk

func (w SNMP) GetBulk(oid Oid, maxRepetitions int) (map[string]interface{}, error)

GetBulk is semantically the same as maxRepetitions getnext requests, but in a single GETBULK SNMP packet. Caveat: many devices will silently drop GETBULK requests for more than some number of maxrepetitions, if it doesn't work, try with a lower value and/or use GetTable.

func (SNMP) GetMultiple

func (w SNMP) GetMultiple(oids []Oid) (map[string]interface{}, error)

GetMultiple issues a single GET SNMP request requesting multiple values

func (SNMP) GetNext

func (w SNMP) GetNext(oid Oid) (*Oid, interface{}, error)

GetNext issues a GETNEXT SNMP request.

func (*SNMP) GetNextV3

func (w *SNMP) GetNextV3(oid Oid) (*Oid, interface{}, error)

GetNextV3 issues a GETNEXT SNMPv3 request.

func (SNMP) GetTable

func (w SNMP) GetTable(oid Oid) (map[string]interface{}, error)

GetTable efficiently gets an entire table from an SNMP agent. Uses GETBULK requests to go fast.

func (*SNMP) GetV3

func (w *SNMP) GetV3(oid Oid) (interface{}, error)

GetV3 sends an SNMPv3 get request requesting the value for an oid.

func (SNMP) ParseTrap

func (w SNMP) ParseTrap(response []byte) (Trap, error)

ParseTrap parses a received SNMP trap and returns a map of oid to objects

type SNMPVersion

type SNMPVersion uint8

SNMPVersion indicates which SNMP version is in use.

const (
	SNMPv1  SNMPVersion = 0
	SNMPv2c SNMPVersion = 1
	SNMPv3  SNMPVersion = 3
)

List the supported snmp versions.

type Trap

type Trap struct {
	Version     int
	TrapType    int // for V1 traps
	OID         Oid
	Other       interface{}
	Community   string
	Username    string
	Address     string
	VarBinds    map[string]interface{}
	VarBindOIDs []string
}

Trap object.

type TrapHandler

type TrapHandler interface {
	OnError(addr net.Addr, err error)
	OnTrap(addr net.Addr, trap Trap)
}

TrapHandler interface.

type TrapServer

type TrapServer struct {
	PacketSize int
	IPAddress  net.UDPAddr
	Port       int
	Conn       *net.UDPConn
	Users      []V3user
}

TrapServer object.

func NewTrapServer

func NewTrapServer(ip string, port int) (TrapServer, error)

NewTrapServer creates a new TrapServer object.

func (*TrapServer) ListenAndServe

func (s *TrapServer) ListenAndServe(handler TrapHandler)

ListenAndServe starts the listen loop and will pause execution until server is shut down.

type V3user

type V3user struct {
	User    string
	AuthAlg string //MD5 or SHA1
	AuthPwd string
	PrivAlg string //AES or DES
	PrivPwd string
}

V3user object.

Directories

Path Synopsis
examples
trapserver
Package main provides an example of how to create an SNMP trap receiver server.
Package main provides an example of how to create an SNMP trap receiver server.

Jump to

Keyboard shortcuts

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