gosnmplib

package
v0.0.0-...-a768f24 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package gosnmplib provides helpers to go with gosnmp.

Index

Constants

This section is empty.

Variables

View Source
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

func GetValueFromPDU(pduVariable gosnmp.SnmpPDU) (interface{}, error)

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:

func IsStringPrintable

func IsStringPrintable(bytesValue []byte) bool

IsStringPrintable returns true if the provided byte array is only composed of printable characeters

func OIDToInts

func OIDToInts(oid string) ([]int, error)

OIDToInts converts a string OID into a list of integers.

func PacketAsString

func PacketAsString(packet *gosnmp.SnmpPacket) string

PacketAsString used to format gosnmp.SnmpPacket for debug/trace logging

func SkipOIDRowsNaive

func SkipOIDRowsNaive(oid string) string

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

func StandardTypeToString(value interface{}) (string, error)

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

func PDUFromSNMP(pdu *gosnmp.SnmpPDU) (*PDU, error)

PDUFromSNMP packages a gosnmp.SnmpPDU as a PDU

func (*PDU) RawValue

func (d *PDU) RawValue() (any, error)

RawValue returns the value as a []byte, int64, float64, or plain string, depending on the type.

func (*PDU) SetValue

func (d *PDU) SetValue(value any) error

SetValue sets the value. The input should be a []byte, int64, float64, or plain string, depending on the type.

type Replacer

type Replacer struct {
	Regex *regexp.Regexp
	Repl  []byte
}

Replacer structure to store regex matching logs parts to replace

type TraceLevelLogWriter

type TraceLevelLogWriter struct{}

TraceLevelLogWriter is a log writer for gosnmp logs, it removes sensitive info

func (*TraceLevelLogWriter) Write

func (sw *TraceLevelLogWriter) Write(logInput []byte) (n int, err error)

Jump to

Keyboard shortcuts

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