snmpgo

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2019 License: MIT Imports: 28 Imported by: 0

README

Build Status GoDoc

snmpgo

Originally from : https://github.com/k-sone/snmpgo Additionally added TCP transport.

snmpgo is a golang implementation for SNMP.

Supported Message Types

Sending
  • SNMP V1
    • GetRequest
    • GetNextRequest
  • SNMP V2c, V3
    • GetRequest
    • GetNextRequest
    • GetBulkRequest
    • V2Trap
    • InformRequest
Receiving
  • SNMP V2c
    • V2Trap
    • InformRequest
  • SNMP V3
    • V2Trap

Examples

getv2.go, getv3.go

Example for sending a GetRequest. Explain how to use basic of the API.

package main

import (
    "fmt"

    "github.com/k-sone/snmpgo"
)

func main() {
    snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
        Version:   snmpgo.V2c,
        Address:   "127.0.0.1:161",
        Retries:   1,
        Community: "public",
    })
    if err != nil {
        // Failed to create snmpgo.SNMP object
        fmt.Println(err)
        return
    }

    oids, err := snmpgo.NewOids([]string{
        "1.3.6.1.2.1.1.1.0",
        "1.3.6.1.2.1.1.2.0",
        "1.3.6.1.2.1.1.3.0",
    })
    if err != nil {
        // Failed to parse Oids
        fmt.Println(err)
        return
    }

    if err = snmp.Open(); err != nil {
        // Failed to open connection
        fmt.Println(err)
        return
    }
    defer snmp.Close()

    pdu, err := snmp.GetRequest(oids)
    if err != nil {
        // Failed to request
        fmt.Println(err)
        return
    }
    if pdu.ErrorStatus() != snmpgo.NoError {
        // Received an error from the agent
        fmt.Println(pdu.ErrorStatus(), pdu.ErrorIndex())
    }

    // get VarBind list
    fmt.Println(pdu.VarBinds())

    // select a VarBind
    fmt.Println(pdu.VarBinds().MatchOid(oids[0]))
}

trapv2.go, trapv3.go

Example for sending a V2Trap. Explain how to build varbinds using API.

multiget.go

Example for sending a GetRequest to multiple agents.

ifstat.go

This command displays the traffic of agent at regular intervals. Explain how to process the obtained information.

snmpgoget.go

snmpget@Net-SNMP like command.

snmpgobulkwalk.go

snmpbulkwalk@Net-SNMP like command.

snmpgotrap.go

snmptrap@Net-SNMP like command.

Dependencies

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	OidSysUpTime = MustNewOid("1.3.6.1.2.1.1.3.0")
	OidSnmpTrap  = MustNewOid("1.3.6.1.6.3.1.1.4.1.0")
)
View Source
var UnsupportedOperation error = errors.New("Unsupported operation")

Functions

This section is empty.

Types

type ArgumentError

type ArgumentError struct {
	Value   interface{} // Argument that has a problem
	Message string      // Error message
}

An ArgumentError suggests that the arguments are wrong

func (*ArgumentError) Error

func (e *ArgumentError) Error() string

type AuthProtocol

type AuthProtocol string
const (
	Md5 AuthProtocol = "MD5"
	Sha AuthProtocol = "SHA"
)

type Counter32

type Counter32 struct {
	Value uint32
}

func NewCounter32

func NewCounter32(i uint32) *Counter32

func (*Counter32) BigInt

func (v *Counter32) BigInt() (*big.Int, error)

func (*Counter32) Marshal

func (v *Counter32) Marshal() ([]byte, error)

func (*Counter32) String

func (v *Counter32) String() string

func (*Counter32) Type

func (v *Counter32) Type() string

func (*Counter32) Unmarshal

func (v *Counter32) Unmarshal(b []byte) (rest []byte, err error)

type Counter64

type Counter64 struct {
	Value uint64
}

func NewCounter64

func NewCounter64(i uint64) *Counter64

func (*Counter64) BigInt

func (v *Counter64) BigInt() (*big.Int, error)

func (*Counter64) Marshal

func (v *Counter64) Marshal() ([]byte, error)

func (*Counter64) String

func (v *Counter64) String() string

func (*Counter64) Type

func (v *Counter64) Type() string

func (*Counter64) Unmarshal

func (v *Counter64) Unmarshal(b []byte) (rest []byte, err error)

type EndOfMibView

type EndOfMibView struct {
	Null
}

func NewEndOfMibView

func NewEndOfMibView() *EndOfMibView

func (*EndOfMibView) Marshal

func (v *EndOfMibView) Marshal() ([]byte, error)

func (*EndOfMibView) Type

func (v *EndOfMibView) Type() string

func (*EndOfMibView) Unmarshal

func (v *EndOfMibView) Unmarshal(b []byte) (rest []byte, err error)

type ErrorStatus

type ErrorStatus int
const (
	NoError ErrorStatus = iota
	TooBig
	NoSuchName
	BadValue
	ReadOnly
	GenError
	NoAccess
	WrongType
	WrongLength
	WrongEncoding
	WrongValue
	NoCreation
	InconsistentValue
	ResourceUnavailable
	CommitFailed
	UndoFailed
	AuthorizationError
	NotWritable
	InconsistentName
)

func (ErrorStatus) String

func (e ErrorStatus) String() string

type Gauge32

type Gauge32 struct {
	Counter32
}

func NewGauge32

func NewGauge32(i uint32) *Gauge32

func (*Gauge32) Marshal

func (v *Gauge32) Marshal() ([]byte, error)

func (*Gauge32) Type

func (v *Gauge32) Type() string

func (*Gauge32) Unmarshal

func (v *Gauge32) Unmarshal(b []byte) (rest []byte, err error)

type Integer

type Integer struct {
	Value int32
}

func NewInteger

func NewInteger(i int32) *Integer

func (*Integer) BigInt

func (v *Integer) BigInt() (*big.Int, error)

func (*Integer) Marshal

func (v *Integer) Marshal() ([]byte, error)

func (*Integer) String

func (v *Integer) String() string

func (*Integer) Type

func (v *Integer) Type() string

func (*Integer) Unmarshal

func (v *Integer) Unmarshal(b []byte) (rest []byte, err error)

type Ipaddress

type Ipaddress struct {
	OctetString
}

func NewIpaddress

func NewIpaddress(a, b, c, d byte) *Ipaddress

func (*Ipaddress) BigInt

func (v *Ipaddress) BigInt() (*big.Int, error)

func (*Ipaddress) Marshal

func (v *Ipaddress) Marshal() ([]byte, error)

func (*Ipaddress) String

func (v *Ipaddress) String() string

func (*Ipaddress) Type

func (v *Ipaddress) Type() string

func (*Ipaddress) Unmarshal

func (v *Ipaddress) Unmarshal(b []byte) (rest []byte, err error)

type MessageError

type MessageError struct {
	Cause   error  // Cause of the error
	Message string // Error message
	Detail  string // Detail of the error for debugging
}

A MessageError suggests that the received message is wrong or is not obtained

func (*MessageError) Error

func (e *MessageError) Error() string

type NoSucheInstance

type NoSucheInstance struct {
	Null
}

func NewNoSucheInstance

func NewNoSucheInstance() *NoSucheInstance

func (*NoSucheInstance) Marshal

func (v *NoSucheInstance) Marshal() ([]byte, error)

func (*NoSucheInstance) Type

func (v *NoSucheInstance) Type() string

func (*NoSucheInstance) Unmarshal

func (v *NoSucheInstance) Unmarshal(b []byte) (rest []byte, err error)

type NoSucheObject

type NoSucheObject struct {
	Null
}

func NewNoSucheObject

func NewNoSucheObject() *NoSucheObject

func (*NoSucheObject) Marshal

func (v *NoSucheObject) Marshal() ([]byte, error)

func (*NoSucheObject) Type

func (v *NoSucheObject) Type() string

func (*NoSucheObject) Unmarshal

func (v *NoSucheObject) Unmarshal(b []byte) (rest []byte, err error)

type Null

type Null struct{}

func NewNull

func NewNull() *Null

func (*Null) BigInt

func (v *Null) BigInt() (*big.Int, error)

func (*Null) Marshal

func (v *Null) Marshal() ([]byte, error)

func (*Null) String

func (v *Null) String() string

func (*Null) Type

func (v *Null) Type() string

func (*Null) Unmarshal

func (v *Null) Unmarshal(b []byte) (rest []byte, err error)

type OctetString

type OctetString struct {
	Value []byte
}

func NewOctetString

func NewOctetString(b []byte) *OctetString

func (*OctetString) BigInt

func (v *OctetString) BigInt() (*big.Int, error)

func (*OctetString) Marshal

func (v *OctetString) Marshal() ([]byte, error)

func (*OctetString) String

func (v *OctetString) String() string

func (*OctetString) Type

func (v *OctetString) Type() string

func (*OctetString) Unmarshal

func (v *OctetString) Unmarshal(b []byte) (rest []byte, err error)

type Oid

type Oid struct {
	Value asn1.ObjectIdentifier
}

func MustNewOid

func MustNewOid(s string) *Oid

MustNewOid is like NewOid but panics if argument cannot be parsed

func NewOid

func NewOid(s string) (oid *Oid, err error)

func (*Oid) AppendSubIds

func (v *Oid) AppendSubIds(subs []int) (*Oid, error)

Returns Oid with additional sub-ids

func (*Oid) BigInt

func (v *Oid) BigInt() (*big.Int, error)

func (*Oid) Compare

func (v *Oid) Compare(o *Oid) int

Returns 0 this OID is equal to the specified OID, -1 this OID is lexicographically less than the specified OID, 1 this OID is lexicographically greater than the specified OID

func (*Oid) Contains

func (v *Oid) Contains(o *Oid) bool

Returns true if this OID contains the specified OID

func (*Oid) Equal

func (v *Oid) Equal(o *Oid) bool

Returns true if this OID is same the specified OID

func (*Oid) Marshal

func (v *Oid) Marshal() ([]byte, error)

func (*Oid) String

func (v *Oid) String() string

func (*Oid) Type

func (v *Oid) Type() string

func (*Oid) Unmarshal

func (v *Oid) Unmarshal(b []byte) (rest []byte, err error)

type Oids

type Oids []*Oid

func NewOids

func NewOids(s []string) (oids Oids, err error)

func (Oids) Sort

func (o Oids) Sort() Oids

Sort a Oid list

func (Oids) Uniq

func (o Oids) Uniq() Oids

Filter out adjacent OID list

func (Oids) UniqBase

func (o Oids) UniqBase() Oids

Filter out adjacent OID list with the same prefix

type Opaque

type Opaque struct {
	OctetString
}

func NewOpaque

func NewOpaque(b []byte) *Opaque

func (*Opaque) Marshal

func (v *Opaque) Marshal() ([]byte, error)

func (*Opaque) String

func (v *Opaque) String() string

func (*Opaque) Type

func (v *Opaque) Type() string

func (*Opaque) Unmarshal

func (v *Opaque) Unmarshal(b []byte) (rest []byte, err error)

type Pdu

type Pdu interface {
	PduType() PduType
	RequestId() int
	SetRequestId(int)
	ErrorStatus() ErrorStatus
	SetErrorStatus(ErrorStatus)
	ErrorIndex() int
	SetErrorIndex(int)
	SetNonrepeaters(int)
	SetMaxRepetitions(int)
	AppendVarBind(*Oid, Variable)
	VarBinds() VarBinds
	Marshal() ([]byte, error)
	Unmarshal([]byte) (rest []byte, err error)
	String() string
}

The protocol data unit of SNMP

func NewPdu

func NewPdu(ver SNMPVersion, t PduType) (pdu Pdu)

func NewPduWithOids

func NewPduWithOids(ver SNMPVersion, t PduType, oids Oids) (pdu Pdu)

func NewPduWithVarBinds

func NewPduWithVarBinds(ver SNMPVersion, t PduType, varBinds VarBinds) (pdu Pdu)

type PduType

type PduType int
const (
	GetRequest PduType = iota
	GetNextRequest
	GetResponse
	SetRequest
	Trap
	GetBulkRequest
	InformRequest
	SNMPTrapV2
	Report
)

func (PduType) String

func (t PduType) String() string

type PduV1

type PduV1 struct {
	// contains filtered or unexported fields
}

The PduV1 is used by SNMP V1 and V2c, other than the SNMP V1 Trap

func (*PduV1) AppendVarBind

func (pdu *PduV1) AppendVarBind(oid *Oid, variable Variable)

func (*PduV1) ErrorIndex

func (pdu *PduV1) ErrorIndex() int

func (*PduV1) ErrorStatus

func (pdu *PduV1) ErrorStatus() ErrorStatus

func (*PduV1) Marshal

func (pdu *PduV1) Marshal() (b []byte, err error)

func (*PduV1) PduType

func (pdu *PduV1) PduType() PduType

func (*PduV1) RequestId

func (pdu *PduV1) RequestId() int

func (*PduV1) SetErrorIndex

func (pdu *PduV1) SetErrorIndex(i int)

func (*PduV1) SetErrorStatus

func (pdu *PduV1) SetErrorStatus(i ErrorStatus)

func (*PduV1) SetMaxRepetitions

func (pdu *PduV1) SetMaxRepetitions(i int)

func (*PduV1) SetNonrepeaters

func (pdu *PduV1) SetNonrepeaters(i int)

func (*PduV1) SetRequestId

func (pdu *PduV1) SetRequestId(i int)

func (*PduV1) String

func (pdu *PduV1) String() string

func (*PduV1) Unmarshal

func (pdu *PduV1) Unmarshal(b []byte) (rest []byte, err error)

func (*PduV1) VarBinds

func (pdu *PduV1) VarBinds() VarBinds

type PrivProtocol

type PrivProtocol string
const (
	Des PrivProtocol = "DES"
	Aes PrivProtocol = "AES"
)

type SNMP

type SNMP struct {
	// contains filtered or unexported fields
}

SNMP Object provides functions for the SNMP Client

func NewSNMP

func NewSNMP(args SNMPArguments) (*SNMP, error)

Create a SNMP Object

func (*SNMP) Close

func (s *SNMP) Close()

Close a connection

func (*SNMP) GetBulkRequest

func (s *SNMP) GetBulkRequest(oids Oids, nonRepeaters, maxRepetitions int) (result Pdu, err error)

func (*SNMP) GetBulkWalk

func (s *SNMP) GetBulkWalk(oids Oids, nonRepeaters, maxRepetitions int) (result Pdu, err error)

This method inquire about OID subtrees by repeatedly using GetBulkRequest. Returned PDU contains the varbind list of all subtrees. however, if the ErrorStatus of PDU is not the NoError, return only the last query result.

func (*SNMP) GetNextRequest

func (s *SNMP) GetNextRequest(oids Oids) (result Pdu, err error)

func (*SNMP) GetRequest

func (s *SNMP) GetRequest(oids Oids) (result Pdu, err error)

func (*SNMP) InformRequest

func (s *SNMP) InformRequest(varBinds VarBinds) error

func (*SNMP) Open

func (s *SNMP) Open() (err error)

Open a connection

func (*SNMP) String

func (s *SNMP) String() string

func (*SNMP) V2Trap

func (s *SNMP) V2Trap(varBinds VarBinds) error

func (*SNMP) V2TrapWithBootsTime

func (s *SNMP) V2TrapWithBootsTime(varBinds VarBinds, eBoots, eTime int) error

Send trap with the authoritative engine boots and time when used with SNMP V3.

type SNMPArguments

type SNMPArguments struct {
	Version          SNMPVersion   // SNMP version to use
	Network          string        // See net.Dial parameter (The default is `udp`)
	Address          string        // See net.Dial parameter
	Timeout          time.Duration // Request timeout (The default is 5sec)
	Retries          uint          // Number of retries (The default is `0`)
	MessageMaxSize   int           // Maximum size of an SNMP message (The default is `1400`)
	Community        string        // Community (V1 or V2c specific)
	UserName         string        // Security name (V3 specific)
	SecurityLevel    SecurityLevel // Security level (V3 specific)
	AuthPassword     string        // Authentication protocol pass phrase (V3 specific)
	AuthProtocol     AuthProtocol  // Authentication protocol (V3 specific)
	PrivPassword     string        // Privacy protocol pass phrase (V3 specific)
	PrivProtocol     PrivProtocol  // Privacy protocol (V3 specific)
	SecurityEngineId string        // Security engine ID (V3 specific)
	ContextEngineId  string        // Context engine ID (V3 specific)
	ContextName      string        // Context name (V3 specific)
	// contains filtered or unexported fields
}

An argument for creating a SNMP Object

func (*SNMPArguments) String

func (a *SNMPArguments) String() string

type SNMPVersion

type SNMPVersion int
const (
	V1  SNMPVersion = 0
	V2c SNMPVersion = 1
	V3  SNMPVersion = 3
)

func (SNMPVersion) String

func (s SNMPVersion) String() string

type ScopedPdu

type ScopedPdu struct {
	ContextEngineId []byte
	ContextName     []byte
	PduV1
}

The ScopedPdu is used by SNMP V3. Includes the PduV1, and contains a SNMP context parameter

func (*ScopedPdu) Marshal

func (pdu *ScopedPdu) Marshal() (b []byte, err error)

func (*ScopedPdu) String

func (pdu *ScopedPdu) String() string

func (*ScopedPdu) Unmarshal

func (pdu *ScopedPdu) Unmarshal(b []byte) (rest []byte, err error)

type SecurityEntry

type SecurityEntry struct {
	Version          SNMPVersion   // SNMP version to use (V2c or V3)
	Community        string        // Community (V2c specific)
	UserName         string        // Security name (V3 specific)
	SecurityLevel    SecurityLevel // Security level (V3 specific)
	AuthPassword     string        // Authentication protocol pass phrase (V3 specific)
	AuthProtocol     AuthProtocol  // Authentication protocol (V3 specific)
	PrivPassword     string        // Privacy protocol pass phrase (V3 specific)
	PrivProtocol     PrivProtocol  // Privacy protocol (V3 specific)
	SecurityEngineId string        // Security engine ID (V3 Trap specific)
}

SecurityEntry is used for authentication of the received SNMP message

func (*SecurityEntry) String

func (a *SecurityEntry) String() string

type SecurityLevel

type SecurityLevel int
const (
	NoAuthNoPriv SecurityLevel = iota
	AuthNoPriv
	AuthPriv
)

func (SecurityLevel) String

func (s SecurityLevel) String() string

type ServerArguments

type ServerArguments struct {
	Network        string        // "udp", "udp4", "udp6" (The default is `udp`)
	LocalAddr      string        // See net.Dial parameter
	WriteTimeout   time.Duration // Timeout for writing a response (The default is 5sec)
	MessageMaxSize int           // Maximum size of a SNMP message (The default is 2048)
}

An argument for creating a Server Object

func (*ServerArguments) String

func (a *ServerArguments) String() string

type StdLogger

type StdLogger interface {
	Print(v ...interface{})
	Printf(format string, v ...interface{})
}

type TimeTicks

type TimeTicks struct {
	Counter32
}

func NewTimeTicks

func NewTimeTicks(i uint32) *TimeTicks

func (*TimeTicks) Marshal

func (v *TimeTicks) Marshal() ([]byte, error)

func (*TimeTicks) Type

func (v *TimeTicks) Type() string

func (*TimeTicks) Unmarshal

func (v *TimeTicks) Unmarshal(b []byte) (rest []byte, err error)

type TrapListener

type TrapListener interface {
	OnTRAP(trap *TrapRequest)
}

TrapListener defines method that need to be implemented by Trap listeners. If OnTRAP panics, the server (caller of OnTRAP) assumes that affect of the panic is temporary and recovers by the panic and logs trace to the error log.

type TrapRequest

type TrapRequest struct {
	// The received PDU
	Pdu Pdu

	// The source address of trap
	Source net.Addr

	// Error is an optional field used to indicate
	// errors which may occur during the decoding
	// of the received packet
	Error error
}

TrapRequest is representing trap request that is send from the network element.

type TrapServer

type TrapServer struct {

	// Error Logger which will be used for logging of default errors
	ErrorLog StdLogger
	// contains filtered or unexported fields
}

A TrapServer defines parameters for running of TRAP daemon that listens for incoming trap messages.

func NewTrapServer

func NewTrapServer(args ServerArguments) (*TrapServer, error)

NewTrapServer returns a new Server and is using server arguments for configuration.

func (*TrapServer) AddSecurity

func (s *TrapServer) AddSecurity(entry *SecurityEntry) error

func (*TrapServer) Close

func (s *TrapServer) Close() error

Close shuts down the server.

func (*TrapServer) DeleteSecurity

func (s *TrapServer) DeleteSecurity(entry *SecurityEntry) error

func (*TrapServer) Serve

func (s *TrapServer) Serve(listener TrapListener) error

Serve starts the SNMP trap receiver. Serve blocks, the caller should call Close when finished, to shut it down.

type VarBind

type VarBind struct {
	Oid      *Oid
	Variable Variable
}

func NewVarBind

func NewVarBind(oid *Oid, val Variable) *VarBind

func (*VarBind) Marshal

func (v *VarBind) Marshal() (b []byte, err error)

func (*VarBind) String

func (v *VarBind) String() string

func (*VarBind) Unmarshal

func (v *VarBind) Unmarshal(b []byte) (rest []byte, err error)

type VarBinds

type VarBinds []*VarBind

func (VarBinds) MatchBaseOids

func (v VarBinds) MatchBaseOids(prefix *Oid) VarBinds

Gets a VarBind list that matches the prefix

func (VarBinds) MatchOid

func (v VarBinds) MatchOid(oid *Oid) *VarBind

Gets a VarBind that matches

func (VarBinds) Sort

func (v VarBinds) Sort() VarBinds

Sort a VarBind list by OID

func (VarBinds) String

func (v VarBinds) String() string

func (VarBinds) Uniq

func (v VarBinds) Uniq() VarBinds

Filter out adjacent VarBind list

type Variable

type Variable interface {
	// Return a BigInt representation of this Variable if such a representation exists
	BigInt() (*big.Int, error)
	// Return a string representation of this Variable
	String() string
	// Return a string of type
	Type() string
	Marshal() ([]byte, error)
	Unmarshal([]byte) (rest []byte, err error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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