Documentation ¶
Overview ¶
Package gosnmplib provides helpers to go with gosnmp.
Index ¶
- Variables
- func ConditionalWalk(session *gosnmp.GoSNMP, rootOID string, useBulk bool, ...) error
- func GetAuthProtocol(authProtocolStr string) (gosnmp.SnmpV3AuthProtocol, error)
- func GetPrivProtocol(privProtocolStr string) (gosnmp.SnmpV3PrivProtocol, error)
- func GetValueFromPDU(pduVariable gosnmp.SnmpPDU) (interface{}, error)
- func IsStringPrintable(bytesValue []byte) bool
- func OIDToInts(oid string) ([]int, error)
- func PacketAsString(packet *gosnmp.SnmpPacket) string
- func SkipOIDRowsNaive(oid string) string
- func StandardTypeToString(value interface{}) (string, error)
- type OIDRelation
- type PDU
- type Replacer
- type TraceLevelLogWriter
Constants ¶
This section is empty.
Variables ¶
var MockValidReachableGetNextPacket = gosnmp.SnmpPacket{ Variables: []gosnmp.SnmpPDU{ { Name: "1.3.6.1.2.1.1.2.0", Type: gosnmp.ObjectIdentifier, Value: "1.3.6.1.4.1.3375.2.1.3.4.1", }, }, }
MockValidReachableGetNextPacket valid reachable packet
Functions ¶
func ConditionalWalk ¶
func ConditionalWalk(session *gosnmp.GoSNMP, rootOID string, useBulk bool, walkFn func(dataUnit gosnmp.SnmpPDU) (string, error)) error
ConditionalWalk mimics gosnmp.GoSNMP.Walk, except that the walkFn can return a next OID to walk from. Use e.g. SkipOIDRowsNaive to skip over additional rows. This code is adapated directly from gosnmp's walk function.
func GetAuthProtocol ¶
func GetAuthProtocol(authProtocolStr string) (gosnmp.SnmpV3AuthProtocol, error)
GetAuthProtocol converts auth protocol from string to type
func GetPrivProtocol ¶
func GetPrivProtocol(privProtocolStr string) (gosnmp.SnmpV3PrivProtocol, error)
GetPrivProtocol converts priv protocol from string to type Related resource: https://github.com/gosnmp/gosnmp/blob/f6fb3f74afc3fb0e5b44b3f60751b988bc960019/v3_usm.go#L458-L461 Reeder AES192/256: Used by many vendors, including Cisco. Blumenthal AES192/256: Not many vendors use this algorithm.
func GetValueFromPDU ¶
GetValueFromPDU converts the value from an SnmpPDU to a standard type. Octet and Bit strings will be returned as []byte. All integer and float types will be returned as float64. IPs and OIDs will be returned as strings; OIDs will have any leading '.' stripped. Unsupported types:
- gosnmp.Opaque: gosnmp never returns these, instead processing them recursively See https://github.com/gosnmp/gosnmp/blob/dc320dac5b53d95a366733fd95fb5851f2099387/helper.go#L195-L205
- Boolean, Null: Although ASN1 allows these, SNMP does not (if someone needs a bool they use an enumerated integer instead)
func IsStringPrintable ¶
IsStringPrintable returns true if the provided byte array is only composed of printable characeters
func PacketAsString ¶
func PacketAsString(packet *gosnmp.SnmpPacket) string
PacketAsString used to format gosnmp.SnmpPacket for debug/trace logging
func SkipOIDRowsNaive ¶
SkipOIDRowsNaive takes an OID and returns a suitable OID to pass to GetNext in order to fetch the next OID after the given one excluding additional table rows. If the OID is for a scalar value, it is returned unchanged; otherwise, it is the OID for a row in a table, and the returned OID will be just less than the first OID for the next column of the table.
This is a naive, MIB-less algorithm based on the fact that if a table has OID X, then the TableEntry that describes the table type will have OID `X.1` and every column in the table will have an OID like `X.1.n`. Thus, if we grab the last segment other than the end that is `1` in the input OID, and increment the next segment by 1, there's a good chance we'll get the next row.
Three caveats: first, if a table index ends in .0, this will mistake it for a scalar and return it. Second, if a table index contains '.1.', we will interpret that as the table index instead. Third, if the first row of a table has OID .1, then that will also be mistaken for the table entry. In all three of these cases, the result is that we end up grabbing additional rows of a table, but we will never skip over any rows or scalars, so it is safe to use this to determine a superset of all OIDs present on a device.
It is unlikely we can do better than this without having relevant MIB data.
func StandardTypeToString ¶
StandardTypeToString can be used to convert the output of `GetValueFromPDU` to a string
Types ¶
type OIDRelation ¶
type OIDRelation uint8
OIDRelation indicates how one OID relates to another
const ( // EQUAL indicates two OIDs are the same EQUAL OIDRelation = iota // GREATER indicates that at the first point where the two OIDs differ, the first is greater than the second. GREATER // LESS indicates that at the first point where the two OIDs differ, the first is less than the second. LESS // PARENT indicates that the first OID is a parent of the second PARENT // CHILD indicates that the first OID is a child of the second CHILD )
func CmpOIDs ¶
func CmpOIDs(oid1, oid2 []int) OIDRelation
CmpOIDs compares two OIDs (int slices) and indicates how the first relates to the second.
func (OIDRelation) IsAfter ¶
func (o OIDRelation) IsAfter() bool
IsAfter returns true if the first OID comes lexicographically after the second, either because it is strictly greater than the second or because it is a child of the second.
func (OIDRelation) IsBefore ¶
func (o OIDRelation) IsBefore() bool
IsBefore returns true if the first OID comes lexicographically before the second, either because it is strictly less than the second or because it is a parent of the second.
type PDU ¶
type PDU struct { OID string `json:"oid"` Type gosnmp.Asn1BER `json:"type"` // Value is the stringified version of the value; if Type is OctetString or BitString, it is base64 encoded. Value string `json:"value"` }
PDU represents a data unit from an SNMP request, with smart typing.
func PDUFromSNMP ¶
PDUFromSNMP packages a gosnmp.SnmpPDU as a PDU
type TraceLevelLogWriter ¶
type TraceLevelLogWriter struct{}
TraceLevelLogWriter is a log writer for gosnmp logs, it removes sensitive info