Documentation
¶
Overview ¶
Package wapsnmp provides an SNMP query library.
Index ¶
- func DecodeInteger(toparse []byte) (int64, error)
- func DecodeLength(toparse []byte) (uint64, int, error)
- func DecodeSequence(toparse []byte) ([]interface{}, error)
- func DecodeUInt(toparse []byte) (uint64, error)
- func EncodeInteger(toEncode int64) []byte
- func EncodeLength(length uint64) []byte
- func EncodeSequence(toEncode []interface{}) ([]byte, error)
- func EncodeUInt(toEncode uint64) []byte
- func RandomRequestID() int
- type BERType
- type Counter
- type Counter64
- type Gauge
- type Gauge64
- type Oid
- type SNMPValue
- type SNMPVersion
- type UnsupportedBerType
- type WapSNMP
- func (w WapSNMP) Close() error
- func (w WapSNMP) Get(oid Oid) (interface{}, error)
- func (w WapSNMP) GetBulk(oid Oid, maxRepetitions int) (map[string]interface{}, error)
- func (w WapSNMP) GetBulkArray(oid Oid, maxRepetitions int) ([]SNMPValue, error)
- func (w WapSNMP) GetMultiple(oids []Oid) (map[string]interface{}, error)
- func (w WapSNMP) GetNext(oid Oid) (*Oid, interface{}, error)
- func (w WapSNMP) GetTable(oid Oid) (map[string]interface{}, error)
- func (w WapSNMP) Set(oid Oid, value interface{}) (interface{}, error)
- func (w WapSNMP) SetMultiple(toset map[string]interface{}) (map[string]interface{}, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeLength ¶
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 ¶
DecodeSequence decodes BER binary data into into *[]interface{}.
func EncodeInteger ¶
EncodeInteger encodes an integer to BER format.
func EncodeLength ¶
EncodeLength encodes an integer value as a BER compliant length value.
func EncodeSequence ¶
EncodeSequence will encode an []interface{} into an SNMP bytestream.
func EncodeUInt ¶
EncodeUInt encodes an unsigned integer to BER format.
func RandomRequestID ¶
func RandomRequestID() int
RandomRequestID generates a valid SNMP request ID.
Types ¶
type BERType ¶
type BERType uint8
BERType is a type for 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 AsnIpaddress BERType = AsnApplication | 0x00 AsnCounter BERType = AsnApplication | 0x01 AsnCounter32 BERType = AsnApplication | 0x01 AsnGauge BERType = AsnApplication | 0x02 AsnGauge32 BERType = AsnApplication | 0x02 AsnTimeticks BERType = AsnApplication | 0x03 Opaque BERType = AsnApplication | 0x04 AsnCounter64 BERType = AsnApplication | 0x06 AsnGetRequest BERType = 0xa0 AsnGetNextRequest BERType = 0xa1 AsnGetResponse BERType = 0xa2 AsnSetRequest BERType = 0xa3 AsnGetBulkRequest BERType = 0xa5 AsnTrapV2 BERType = 0xa7 NoSuchInstance BERType = 0x81 EndOfMibView BERType = 0x82 )
Constants for the different types of the TLV fields.
type Counter64 ¶
type Counter64 uint64
Counter64 is a type to distinguish Counter64 from just an int.
type Oid ¶
type Oid []int
Oid is the SNMP object identifier type.
func MustParseOid ¶
MustParseOid parses a string oid to an Oid instance. Panics on error.
type SNMPValue ¶
type SNMPValue struct { Oid Oid Value interface{} }
SNMPValue type to express an oid value pair.
type SNMPVersion ¶
type SNMPVersion uint8
SNMPVersion is a type to indicate which SNMP version is in use.
const ( SNMPv1 SNMPVersion = 0 SNMPv2c SNMPVersion = 1 )
List of the supported snmp versions.
type UnsupportedBerType ¶
type UnsupportedBerType []byte
UnsupportedBerType will be used if data couldn't be decoded.
type WapSNMP ¶
type WapSNMP 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. // contains filtered or unexported fields }
WapSNMP is the type that lets you do SNMP requests.
func NewWapSNMP ¶
func NewWapSNMP(target, community string, version SNMPVersion, timeout time.Duration, retries int) (*WapSNMP, error)
NewWapSNMP creates a new WapSNMP object. Opens a udp connection to the device that will be used for the SNMP packets.
func NewWapSNMPOnConn ¶
func NewWapSNMPOnConn(target, community string, version SNMPVersion, timeout time.Duration, retries int, conn net.Conn) *WapSNMP
NewWapSNMPOnConn creates a new WapSNMP object from an existing net.Conn.
It does not check if the provided target is valid.
func (WapSNMP) Get ¶
Get sends an SNMP get request requesting the value for an oid.
Example ¶
target := "1.2.3.4" community := "public" version := SNMPv2c oids := []Oid{ MustParseOid(".1.3.6.1.2.1.1.1.0"), MustParseOid(".1.3.6.1.2.1.1.2.0"), MustParseOid(".1.3.6.1.2.1.2.1.0"), } wsnmp, err := NewWapSNMP(target, community, version, 2*time.Second, 5) defer wsnmp.Close() if err != nil { fmt.Printf("Error creating wsnmp => %v\n", wsnmp) return } for _, oid := range oids { val, err := wsnmp.Get(oid) fmt.Printf("Getting %v\n", oid) if err != nil { fmt.Printf("Get error => %v\n", wsnmp) return } fmt.Printf("Get(%v, %v, %v, %v) => %v\n", target, community, version, oid, val) }
Output:
func (WapSNMP) GetBulk ¶
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.
Caveat: as codedance (on github) pointed out, iteration order on a map is indeterminate. You can alternatively use GetBulkArray to get the entries as a list, with deterministic iteration order.
Example ¶
target := "1.2.3.4" community := "public" version := SNMPv2c oid := MustParseOid(".1.3.6.1.2.1") fmt.Printf("Contacting %v %v %v\n", target, community, version) wsnmp, err := NewWapSNMP(target, community, version, 2*time.Second, 5) defer wsnmp.Close() if err != nil { fmt.Printf("Error creating wsnmp => %v\n", wsnmp) return } defer wsnmp.Close() for { results, err := wsnmp.GetBulk(oid, 50) if err != nil { fmt.Printf("GetBulk error => %v\n", err) return } for o, v := range results { fmt.Printf("%v => %v\n", o, v) oid = MustParseOid(o) } /* Old version without GETBULK result_oid, val, err := wsnmp.GetNext(oid) if err != nil { fmt.Printf("GetNext error => %v\n", err) return } fmt.Printf("GetNext(%v, %v, %v, %v) => %s, %v\n", target, community, version, oid, result_oid, val) oid = *result_oid */ }
Output:
func (WapSNMP) GetBulkArray ¶
GetBulkArray is the same as GetBulk, but returns it's results as a list, for those who want deterministic iteration instead of convenient access.
func (WapSNMP) GetMultiple ¶
GetMultiple issues a single GET SNMP request requesting multiple values
func (WapSNMP) GetTable ¶
GetTable efficiently gets an entire table from an SNMP agent. Uses GETBULK requests to go fast.
Example ¶
target := "1.2.3.4" community := "public" version := SNMPv2c oid := MustParseOid(".1.3.6.1.4.1.2636.3.2.3.1.20") fmt.Printf("Contacting %v %v %v\n", target, community, version) wsnmp, err := NewWapSNMP(target, community, version, 2*time.Second, 5) defer wsnmp.Close() if err != nil { fmt.Printf("Error creating wsnmp => %v\n", wsnmp) return } table, err := wsnmp.GetTable(oid) if err != nil { fmt.Printf("Error getting table => %v\n", wsnmp) return } for k, v := range table { fmt.Printf("%v => %v\n", k, v) }
Output: