NO further developement will be made in the foreseen future.
-- import "github.com/PromonLogicalis/snmp"
Package snmp implements low-level support for SNMP with focus in SNMP agents.
At the encoding level it uses the PromonLogicalis/asn1 to parse and serialize SNMP messages providing Go types for that.
The package also provides transport-independent support for creating custom SNMP agents with small footprint.
A example of a simple SNMP UDP agent:
package main
import (
"log"
"net"
"time"
"github.com/PromonLogicalis/asn1"
"github.com/PromonLogicalis/snmp"
)
func main() {
agent := snmp.NewAgent()
// Set the read-only and read-write communities
agent.SetCommunities("publ", "priv")
// Register a read-only OID.
since := time.Now()
agent.AddRoManagedObject(
// sysUpTime
asn1.Oid{1, 3, 6, 1, 2, 1, 1, 3, 0},
func(oid asn1.Oid) (interface{}, error) {
seconds := int(time.Now().Sub(since) / time.Second)
return seconds, nil
})
// Register a read-write OID.
name := "example"
agent.AddRwManagedObject(
// sysName
asn1.Oid{1, 3, 6, 1, 2, 1, 1, 5, 0},
func(oid asn1.Oid) (interface{}, error) {
return name, nil
},
func(oid asn1.Oid, value interface{}) error {
strValue, ok := value.(string)
if !ok {
return snmp.VarErrorf(snmp.BadValue, "invalid type")
}
name = strValue
return nil
})
// Bind to an UDP port
addr, err := net.ResolveUDPAddr("udp", ":161")
if err != nil {
log.Fatal(err)
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
log.Fatal(err)
}
// Serve requests
for {
buffer := make([]byte, 1024)
n, source, err := conn.ReadFrom(buffer)
if err != nil {
log.Fatal(err)
}
buffer, err = agent.ProcessDatagram(buffer[:n])
if err != nil {
log.Println(err)
continue
}
_, err = conn.WriteTo(buffer, source)
if err != nil {
log.Fatal(err)
}
}
}
const (
NoError = 0
TooBig = 1
NoSuchName = 2
BadValue = 3
ReadOnly = 4
GenErr = 5
NoAccess = 6
WrongType = 7
WrongLength = 8
WrongEncoding = 9
WrongValue = 10
NoCreation = 11
InconsistentValue = 12
ResourceUnavailable = 13
CommitFailed = 14
UndoFailed = 15
AuthorizationError = 16
NotWritable = 17
InconsistentName = 18
)SNMP error codes.
func Asn1Context() *asn1.ContextAsn1Context returns a new allocated asn1.Context and registers all the choice types necessary for SNMPv1 and SNMPv2.
type Agent struct {
}Agent is a transport independent engine to process SNMP requests.
func NewAgent() *AgentNewAgent create and initialize an agent.
func (a *Agent) AddRoManagedObject(oid asn1.Oid, getter Getter) errorAddRoManagedObject registers a read-only managed object.
func (a *Agent) AddRwManagedObject(oid asn1.Oid, getter Getter,
setter Setter) errorAddRwManagedObject registers a read-write managed object.
The inteface{} values returned by a Getter or received by a Setter must be of one of the following types:
int
string
asn1.Null
asn1.Oid
snmp.Counter32
snmp.Counter64
snmp.IpAddress
snmp.Opaque
snmp.TimeTicks
snmp.Unsigned32
func (a *Agent) ProcessDatagram(requestBytes []byte) (responseBytes []byte, err error)ProcessDatagram handles a binany SNMP message.
func (a *Agent) ProcessMessage(request *Message) (response *Message, err error)ProcessMessage handles a SNMP Message.
func (a *Agent) SetCommunities(public, private string)SetCommunities defines the public and private communities.
func (a *Agent) SetLogger(logger *log.Logger)SetLogger defines the logger used for internal messages.
type BulkPdu struct {
Identifier int
NonRepeaters int
MaxRepetitions int
Variables []Variable
}BulkPdu is a generic type for other Protocol Data Units.
type Counter32 uint32Counter32 is a counter type.
type Counter64 uint64Counter64 is a counter type.
type EndOfMibView asn1.NullEndOfMibView exception.
func (e EndOfMibView) String() stringtype GetBulkRequestPdu BulkPduGetBulkRequestPdu is used for bulk requests.
type GetNextRequestPdu PduGetNextRequestPdu works similarly to GetRequestPdu, but it's returned the value for the next valid Oid.
type GetRequestPdu PduGetRequestPdu is used to request data.
type GetResponsePdu PduGetResponsePdu is used in responses to SNMP requests.
type Getter func(oid asn1.Oid) (interface{}, error)Getter is a function called to return a managed object value.
type IPAddress [4]byteIPAddress is a IPv4 address.
func (ip IPAddress) String() stringString returns a representation of IPAddress in dot notation.
type InformRequestPdu PduInformRequestPdu is used for inform requests.
type Message struct {
Version int
Community string
Pdu interface{} `asn1:"choice:pdu"`
}Message is the top level element of the SNMP protocol.
type NoSuchInstance asn1.NullNoSuchInstance exception.
func (e NoSuchInstance) String() stringtype NoSuchObject asn1.NullNoSuchObject exception.
func (e NoSuchObject) String() stringtype Opaque []byteOpaque is a type for blobs.
type Pdu struct {
Identifier int
ErrorStatus int
ErrorIndex int
Variables []Variable
}Pdu is a generic type for other Protocol Data Units.
type SetRequestPdu PduSetRequestPdu is used to request data to be updated.
type Setter func(oid asn1.Oid, value interface{}) errorSetter is a function called to set a managed object value.
type TimeTicks uint32TimeTicks is a type for time.
type Unsigned32 uint32Unsigned32 is an integer type.
type V1TrapPdu struct {
Enterprise asn1.Oid
AgentAddr IPAddress
GenericTrap int
SpecificTrap int
Timestamp TimeTicks
Variables []Variable
}V1TrapPdu is used when sending a trap in SNMPv1.
type V2TrapPdu PduV2TrapPdu is used when sending a trap in SNMPv2.
type VarError struct {
Status int
Message string
}VarError is an error type that can be returned by a Getter or a Setter. When VarError is returned, it Status is used in the SNMP response.
func VarErrorf(status int, format string, values ...interface{}) VarErrorVarErrorf creates a new Error with a formatted message.
func (e VarError) Error() stringtype Variable struct {
Name asn1.Oid
Value interface{} `asn1:"choice:val"`
}Variable represents an entry of the variable bindings