snmp

package
v0.0.0-...-6a97c18 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Overview

snmp implements a rudimentary encoder and decoder of SNMP packets. It understands GetNextRequest, GetRequest, and GetResponse.

Index

Constants

View Source
const (
	// TagASN1 is the magic tag corresponding to ASN.1 data type in SNMP packet.
	TagASN1 = 0x30
	// TagNoSuchInstance is the magic tag corresponding to a response made toward non-existing OID.
	TagNoSuchInstance = 0x81
	// TagEndOfMIBView is the magic tag corresponding to a response made toward one OID beyond the last one in hierarchy.
	TagEndOfMIBView = 0x82
	// ProtocolV2C is the protocol version magic corresponding to SNMP version 2.0 with community name.
	ProtocolV2C = 0x01

	// PDUGetNextRequest asks for the subsequent OID in a walk operation.
	PDUGetNextRequest = 0xa1
	// PDUGetResponse is a response to either Get or GetNext request.
	PDUGetResponse = 0xa2
	// PDUGetRequest asks for value corresponding to an OID.
	PDUGetRequest = 0xa0
)

Variables

View Source
var (
	// FirstOID is the very first OID among all supported nodes (OIDNodes).
	FirstOID = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 52535, 121, 100}
	// OIDNodes is a comprehensive list of suffix OIDs that correspond to nodes supported by laitos SNMP server.
	OIDNodes = map[int]OIDNodeFunc{

		100: func() interface{} {
			return []byte(inet.GetPublicIP())
		},

		101: func() interface{} {
			return time.Now().In(time.UTC).Unix()
		},

		102: func() interface{} {
			return int64(time.Since(misc.StartupTime).Seconds())
		},

		103: func() interface{} {
			return int64(runtime.NumCPU())
		},

		104: func() interface{} {
			return int64(runtime.GOMAXPROCS(-1))
		},

		105: func() interface{} {
			return int64(runtime.NumGoroutine())
		},

		110: func() interface{} {
			return int64(misc.CommandStats.Count())
		},

		111: func() interface{} {
			return int64(misc.HTTPDStats.Count())
		},

		112: func() interface{} {
			return int64(misc.SMTPDStats.Count())
		},

		114: func() interface{} {
			return int64(misc.AutoUnlockStats.Count())
		},

		115: func() interface{} {
			return int64(misc.OutstandingMailBytes)
		},
	}
	/*
		OIDSuffixList is a sorted list of suffix number among the OID nodes supported by laitos SNMP server. It is
		initialised from OIDNodes via package init function.
	*/
	OIDSuffixList []int
)
View Source
var ParentOID = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 52535, 121}

ParentOID is the very top of OID hierarchy used in laitos SNMP server. It contains a private enterprise number registered by Houzuo (Howard) Guo, 52535: {iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 52535}

And laitos occupies number 121 underneath it.

Functions

func GetNextNode

func GetNextNode(baseOID asn1.ObjectIdentifier) (asn1.ObjectIdentifier, bool)

GetNextNode returns the OID subsequent to the input OID, and whether the input OID already is the very last one.

func MissedExpectation

func MissedExpectation(kind, expected, actual interface{}) error

func ReadBytes

func ReadBytes(in *bufio.Reader, n int) (bytes []byte, err error)

ReadBytes returns consecutive bytes read from input.

func ReadSize

func ReadSize(in *bufio.Reader) (size byte, err error)

ReadTag returns a primitive object size read from input.

func ReadTag

func ReadTag(in *bufio.Reader) (tag byte, err error)

ReadTag returns a primitive tag read from input.

func ReadTagSize

func ReadTagSize(in *bufio.Reader) (tag byte, size byte, err error)

ReadTag returns a primitive tag and primitive object size read from input.

func UnmarshalInteger

func UnmarshalInteger(in *[]byte) (i int64, err error)

UnmarshalInteger reads a primitive integer and advances the slice.

func UnmarshalString

func UnmarshalString(in *[]byte) (s string, err error)

UnmarshalInteger reads a primitive string and advances the slice.

Types

type GetNextRequest

type GetNextRequest struct {
	// BaseOID is the base OID to descend from.
	BaseOID asn1.ObjectIdentifier
}

GetNextRequest describes a request for exactly one subsequent OID during a walk operation.

func ReadGetNextRequest

func ReadGetNextRequest(in []byte) (req GetNextRequest, err error)

ReadGetNextRequest dissects the content of a GetNextRequest from input.

type GetRequest

type GetRequest struct {
	// RequestedOID is the desired OID.
	RequestedOID asn1.ObjectIdentifier
}

GetRequest describes a request for a value corresponding to requested OID.

func ReadGetRequest

func ReadGetRequest(in []byte) (req GetRequest, err error)

ReadGetRequest dissects the content of a GetNextRequest from input.

type GetResponse

type GetResponse struct {
	// RequestedOID is the OID presented in corresponding request.
	RequestedOID asn1.ObjectIdentifier
	// Value is the primitive value corresponding to requested OID.
	Value interface{}
	// NoSuchInstance describes a response toward non-existing OID.
	NoSuchInstance bool
	// EndOfMIBView describes a response toward the OID immediately beyond the last one in hierarchy, hence the OID does not exist.
	EndOfMIBView bool
}

GetRequest describes an answer toward GetRequest.

func (GetResponse) Encode

func (resp GetResponse) Encode() (ret []byte, err error)

Encode encodes the response into a byte array as answer toward a GetRequest.

type OIDNodeFunc

type OIDNodeFunc func() interface{}

OIDNodeFunc is a function that retrieves a latest system/application performance indicator value.

func GetNode

func GetNode(oid asn1.ObjectIdentifier) (nodeFun OIDNodeFunc, exists bool)

GetNode returns the calculation function for the input OID node, or false if the input OID is not supported (does not exist).

type Packet

type Packet struct {
	Version       int64  // Version is the SNMP version, laitos only supports version 2 (value 1).
	CommunityName string // CommunityName is a password string communicated in plain text.
	PDU           byte   // PDU determines the type of SNMP request or response described in a packet.
	RequestID     int64  // RequestID is an integer shared by pairs of request and response packets.
	ErrorStatus   int64  // ErrorStatus is an SNMP magic not handled by laitos.
	ErrorIndex    int64  // ErrorIndex is an SNMP magic not handled by laitos.

	Structure interface{} // Structure contains details of an SNMP PDU request or response.
}

Packet describes a complete SNMP packet, no matter it is a request or a response.

func (*Packet) Encode

func (packet *Packet) Encode() (ret []byte, err error)

Encode encodes a response packet into a byte array.

func (*Packet) ReadFrom

func (packet *Packet) ReadFrom(in *bufio.Reader) (err error)

ReadPacket deserialises an SNMP packet from input.

Jump to

Keyboard shortcuts

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