README
¶
GoSNMPServer
GoSNMPServer is an SNMP server library fully written in Go. It provides Server Get, GetNext, GetBulk, Walk, BulkWalk, Set and Traps. It supports IPv4 and IPv6, using SNMPv2c or SNMPv3. Builds are tested against linux/amd64 and linux/386.
TL;DR
Build your own SNMP Server, try this:
go install github.com/slayercat/GoSNMPServer/cmd/gosnmpserver
$(go env GOPATH)/bin/gosnmpserver run-server
snmpwalk -v 3 -l authPriv -n public -u testuser -a md5 -A testauth -x des -X testpriv 127.0.0.1:1161 1
Quick Start
import "github.com/gosnmp/gosnmp"
import "github.com/slayercat/GoSNMPServer"
import "github.com/slayercat/GoSNMPServer/mibImps"
master := GoSNMPServer.MasterAgent{
Logger: GoSNMPServer.NewDefaultLogger(),
SecurityConfig: GoSNMPServer.SecurityConfig{
AuthoritativeEngineBoots: 1,
Users: []gosnmp.UsmSecurityParameters{
{
UserName: c.String("v3Username"),
AuthenticationProtocol: gosnmp.MD5,
PrivacyProtocol: gosnmp.DES,
AuthenticationPassphrase: c.String("v3AuthenticationPassphrase"),
PrivacyPassphrase: c.String("v3PrivacyPassphrase"),
},
},
},
SubAgents: []*GoSNMPServer.SubAgent{
{
CommunityIDs: []string{c.String("community")},
OIDs: mibImps.All(),
},
},
}
server := GoSNMPServer.NewSNMPServer(master)
err := server.ListenUDP("udp", "127.0.0.1:1161")
if err != nil {
logger.Errorf("Error in listen: %+v", err)
}
server.ServeForever()
Serve your own oids
This library provides some common oid for use. See mibImps for code, See here.
Append GoSNMPServer.PDUValueControlItem
to your SubAgent OIDS:
{
OID: fmt.Sprintf("1.3.6.1.2.1.2.2.1.1.%d", ifIndex),
Type: gosnmp.Integer,
OnGet: func() (value interface{}, err error) { return GoSNMPServer.Asn1IntegerWrap(ifIndex), nil },
Document: "ifIndex",
},
Supports Types: See RFC-2578 FOR SMI
- Integer
- OctetString
- ObjectIdentifier
- IPAddress
- Counter32
- Gauge32
- TimeTicks
- Counter64
- Uinteger32
- OpaqueFloat
- OpaqueDouble
Could use wrap function for detect type error. See GoSNMPServer.Asn1IntegerWrap
/ GoSNMPServer.Asn1IntegerUnwrap
and so on.
Thanks
This library is based on soniah/gosnmp for encoder / decoders. (made a fork for maintenance)
Documentation
¶
Overview ¶
GoSNMPServer is an SNMP server library fully written in Go. It **WILL** provides Server Get, GetNext, GetBulk, Walk, BulkWalk, Set and Traps. It supports IPv4 and IPv6, using __SNMPv2c__ or __SNMPv3__. Builds are tested against linux/amd64 and linux/386.
Build your own SNMP Server, try this:
go install github.com/slayercat/GoSNMPServer/cmd/gosnmpserver $(go env GOPATH)/bin/gosnmpserver run-server snmpwalk -v 3 -l authPriv -n public -u testuser -a md5 -A testauth -x des -X testpriv 127.0.0.1:1161 1
Some Code Here:
import "github.com/gosnmp/gosnmp" import "github.com/slayercat/GoSNMPServer" import "github.com/slayercat/GoSNMPServer/mibImps" master := GoSNMPServer.MasterAgent{ Logger: GoSNMPServer.NewDefaultLogger(), SecurityConfig: GoSNMPServer.SecurityConfig{ AuthoritativeEngineBoots: 1, Users: []gosnmp.UsmSecurityParameters{ { UserName: c.String("v3Username"), AuthenticationProtocol: gosnmp.MD5, PrivacyProtocol: gosnmp.DES, AuthenticationPassphrase: c.String("v3AuthenticationPassphrase"), PrivacyPassphrase: c.String("v3PrivacyPassphrase"), }, }, }, SubAgents: []*GoSNMPServer.SubAgent{ { CommunityIDs: []string{c.String("community")}, OIDs: mibImps.All(), }, }, } server := GoSNMPServer.NewSNMPServer(master) err := server.ListenUDP("udp", "127.0.0.1:1161") if err != nil { logger.Errorf("Error in listen: %+v", err) } server.ServeForever()
Serve your own oids ¶
This library provides some common oid for use. See godoc for details.
See https://github.com/slayercat/GoSNMPServer/tree/master/mibImps for code.
Append `GoSNMPServer.PDUValueControlItem` to your SubAgent OIDS:
{ OID: fmt.Sprintf("1.3.6.1.2.1.2.2.1.1.%d", ifIndex), Type: gosnmp.Integer, OnGet: func() (value interface{}, err error) { return GoSNMPServer.Asn1IntegerWrap(ifIndex), nil }, Document: "ifIndex", },
Index ¶
- Variables
- func Asn1Counter32Unwrap(i interface{}) uint
- func Asn1Counter32Wrap(i uint) interface{}
- func Asn1Counter64Unwrap(i interface{}) uint64
- func Asn1Counter64Wrap(i uint64) interface{}
- func Asn1Gauge32Unwrap(i interface{}) uint
- func Asn1Gauge32Wrap(i uint) interface{}
- func Asn1IPAddressUnwrap(i interface{}) net.IP
- func Asn1IPAddressWrap(i net.IP) interface{}
- func Asn1IntegerUnwrap(i interface{}) int
- func Asn1IntegerWrap(i int) interface{}
- func Asn1ObjectIdentifierUnwrap(i interface{}) string
- func Asn1ObjectIdentifierWrap(i string) interface{}
- func Asn1OctetStringUnwrap(i interface{}) string
- func Asn1OctetStringWrap(i string) interface{}
- func Asn1OpaqueDoubleUnwrap(i interface{}) float64
- func Asn1OpaqueDoubleWrap(i float64) interface{}
- func Asn1OpaqueFloatUnwrap(i interface{}) float32
- func Asn1OpaqueFloatWrap(i float32) interface{}
- func Asn1TimeTicksUnwrap(i interface{}) uint32
- func Asn1TimeTicksWrap(i uint32) interface{}
- func Asn1Uinteger32Unwrap(i interface{}) uint32
- func Asn1Uinteger32Wrap(i uint32) interface{}
- func DefaultGetAuthoritativeEngineTime() uint32
- func IsValidObjectIdentifier(oid string) (result bool)
- type DefaultLogger
- type DiscardLogger
- func (*DiscardLogger) Debug(args ...interface{})
- func (*DiscardLogger) Debugf(format string, args ...interface{})
- func (*DiscardLogger) Debugln(args ...interface{})
- func (*DiscardLogger) Error(args ...interface{})
- func (*DiscardLogger) Errorf(format string, args ...interface{})
- func (*DiscardLogger) Errorln(args ...interface{})
- func (*DiscardLogger) Fatal(args ...interface{})
- func (*DiscardLogger) Fatalf(format string, args ...interface{})
- func (*DiscardLogger) Fatalln(args ...interface{})
- func (*DiscardLogger) Info(args ...interface{})
- func (*DiscardLogger) Infof(format string, args ...interface{})
- func (*DiscardLogger) Infoln(args ...interface{})
- func (*DiscardLogger) Trace(args ...interface{})
- func (*DiscardLogger) Tracef(format string, args ...interface{})
- func (*DiscardLogger) Traceln(args ...interface{})
- func (*DiscardLogger) Warn(args ...interface{})
- func (*DiscardLogger) Warnf(format string, args ...interface{})
- func (*DiscardLogger) Warning(args ...interface{})
- func (*DiscardLogger) Warningf(format string, args ...interface{})
- func (*DiscardLogger) Warningln(args ...interface{})
- func (*DiscardLogger) Warnln(args ...interface{})
- type FuncGetAuthoritativeEngineTime
- type FuncPDUControlCheckPermission
- type FuncPDUControlGet
- type FuncPDUControlSet
- type FuncPDUControlTrap
- type ILogger
- type IReplyer
- type ISnmpServerListener
- type MasterAgent
- type PDUValueControlItem
- type PermissionAllowance
- type SNMPEngineID
- type SNMPServer
- type SecurityConfig
- type SnmpLoggerAdapter
- type SubAgent
- type UDPListener
- type UDPReplyer
Constants ¶
This section is empty.
Variables ¶
var ErrNoPermission = errors.New("ErrNoPermission")
var ErrNoSNMPInstance = errors.New("ErrNoSNMPInstance")
var ErrUnsupportedOperation = errors.New("ErrUnsupportedOperation")
var ErrUnsupportedPacketData = errors.New("ErrUnsupportedPacketData")
var ErrUnsupportedProtoVersion = errors.New("ErrUnsupportedProtoVersion")
Functions ¶
func Asn1Counter32Unwrap ¶
func Asn1Counter32Unwrap(i interface{}) uint
func Asn1Counter32Wrap ¶
func Asn1Counter32Wrap(i uint) interface{}
func Asn1Counter64Unwrap ¶
func Asn1Counter64Unwrap(i interface{}) uint64
func Asn1Counter64Wrap ¶
func Asn1Counter64Wrap(i uint64) interface{}
func Asn1Gauge32Unwrap ¶
func Asn1Gauge32Unwrap(i interface{}) uint
func Asn1Gauge32Wrap ¶
func Asn1Gauge32Wrap(i uint) interface{}
func Asn1IPAddressUnwrap ¶
func Asn1IPAddressWrap ¶
func Asn1IntegerUnwrap ¶
func Asn1IntegerUnwrap(i interface{}) int
func Asn1IntegerWrap ¶
func Asn1IntegerWrap(i int) interface{}
func Asn1ObjectIdentifierUnwrap ¶
func Asn1ObjectIdentifierUnwrap(i interface{}) string
func Asn1ObjectIdentifierWrap ¶
func Asn1ObjectIdentifierWrap(i string) interface{}
func Asn1OctetStringUnwrap ¶
func Asn1OctetStringUnwrap(i interface{}) string
func Asn1OctetStringWrap ¶
func Asn1OctetStringWrap(i string) interface{}
func Asn1OpaqueDoubleUnwrap ¶
func Asn1OpaqueDoubleUnwrap(i interface{}) float64
func Asn1OpaqueDoubleWrap ¶
func Asn1OpaqueDoubleWrap(i float64) interface{}
func Asn1OpaqueFloatUnwrap ¶
func Asn1OpaqueFloatUnwrap(i interface{}) float32
func Asn1OpaqueFloatWrap ¶
func Asn1OpaqueFloatWrap(i float32) interface{}
func Asn1TimeTicksUnwrap ¶
func Asn1TimeTicksUnwrap(i interface{}) uint32
func Asn1TimeTicksWrap ¶
func Asn1TimeTicksWrap(i uint32) interface{}
func Asn1Uinteger32Unwrap ¶
func Asn1Uinteger32Unwrap(i interface{}) uint32
func Asn1Uinteger32Wrap ¶
func Asn1Uinteger32Wrap(i uint32) interface{}
func DefaultGetAuthoritativeEngineTime ¶
func DefaultGetAuthoritativeEngineTime() uint32
func IsValidObjectIdentifier ¶
IsValidObjectIdentifier will check a oid string is valid oid
Types ¶
type DefaultLogger ¶
DefaultLogger is a logger warps logrus
type DiscardLogger ¶
type DiscardLogger struct{}
DiscardLogger throws away everything
func (*DiscardLogger) Debug ¶
func (*DiscardLogger) Debug(args ...interface{})
Debug throws away logmessage
func (*DiscardLogger) Debugf ¶
func (*DiscardLogger) Debugf(format string, args ...interface{})
Debugf throws away logmessage
func (*DiscardLogger) Debugln ¶
func (*DiscardLogger) Debugln(args ...interface{})
Debugln throws away logmessage
func (*DiscardLogger) Error ¶
func (*DiscardLogger) Error(args ...interface{})
Error throws away logmessage
func (*DiscardLogger) Errorf ¶
func (*DiscardLogger) Errorf(format string, args ...interface{})
Errorf throws away logmessage
func (*DiscardLogger) Errorln ¶
func (*DiscardLogger) Errorln(args ...interface{})
Errorln throws away logmessage
func (*DiscardLogger) Fatal ¶
func (*DiscardLogger) Fatal(args ...interface{})
Fatal throws away logmessage
func (*DiscardLogger) Fatalf ¶
func (*DiscardLogger) Fatalf(format string, args ...interface{})
Fatalf throws away logmessage
func (*DiscardLogger) Fatalln ¶
func (*DiscardLogger) Fatalln(args ...interface{})
Fatalln throws away logmessage
func (*DiscardLogger) Info ¶
func (*DiscardLogger) Info(args ...interface{})
Info throws away logmessage
func (*DiscardLogger) Infof ¶
func (*DiscardLogger) Infof(format string, args ...interface{})
Infof throws away logmessage
func (*DiscardLogger) Infoln ¶
func (*DiscardLogger) Infoln(args ...interface{})
Infoln throws away logmessage
func (*DiscardLogger) Trace ¶
func (*DiscardLogger) Trace(args ...interface{})
Trace throws away logmessage
func (*DiscardLogger) Tracef ¶
func (*DiscardLogger) Tracef(format string, args ...interface{})
Tracef throws away logmessage
func (*DiscardLogger) Traceln ¶
func (*DiscardLogger) Traceln(args ...interface{})
Traceln throws away logmessage
func (*DiscardLogger) Warn ¶
func (*DiscardLogger) Warn(args ...interface{})
Warn throws away logmessage
func (*DiscardLogger) Warnf ¶
func (*DiscardLogger) Warnf(format string, args ...interface{})
Warnf throws away logmessage
func (*DiscardLogger) Warning ¶
func (*DiscardLogger) Warning(args ...interface{})
Warning throws away logmessage
func (*DiscardLogger) Warningf ¶
func (*DiscardLogger) Warningf(format string, args ...interface{})
Warningf throws away logmessage
func (*DiscardLogger) Warningln ¶
func (*DiscardLogger) Warningln(args ...interface{})
Warningln throws away logmessage
func (*DiscardLogger) Warnln ¶
func (*DiscardLogger) Warnln(args ...interface{})
Warnln throws away logmessage
type FuncGetAuthoritativeEngineTime ¶
type FuncGetAuthoritativeEngineTime func() uint32
type FuncPDUControlCheckPermission ¶
type FuncPDUControlCheckPermission func(pktVersion gosnmp.SnmpVersion, pduType gosnmp.PDUType, contextName string) PermissionAllowance
FuncPDUControlCheckPermission checks for permission.
return PermissionAllowanceAllowed / PermissionAllowanceDenied
type FuncPDUControlGet ¶
type FuncPDUControlGet func() (value interface{}, err error)
FuncPDUControlGet will be called on get value
type FuncPDUControlSet ¶
type FuncPDUControlSet func(value interface{}) error
FuncPDUControlSet will be called on set value
type FuncPDUControlTrap ¶
type FuncPDUControlTrap func(isInform bool, trapdata gosnmp.SnmpPDU) (dataret interface{}, err error)
FuncPDUControlTrap will be called on trap.
args: isInform: indicate if the request is a InformRequest. true -- It's a InformRequest. data will be returns to the client false -- It's a trap. data to returned will drop silencely. trapdata: what client asks for. returns: dataret -- try to return to client. nil for nothing to return err -- any error?(will return to client by string)
type ILogger ¶
type ILogger interface { Debug(args ...interface{}) Debugf(format string, args ...interface{}) Debugln(args ...interface{}) Error(args ...interface{}) Errorf(format string, args ...interface{}) Errorln(args ...interface{}) Fatal(args ...interface{}) Fatalf(format string, args ...interface{}) Fatalln(args ...interface{}) Info(args ...interface{}) Infof(format string, args ...interface{}) Infoln(args ...interface{}) Trace(args ...interface{}) Tracef(format string, args ...interface{}) Traceln(args ...interface{}) Warn(args ...interface{}) Warnf(format string, args ...interface{}) Warning(args ...interface{}) Warningf(format string, args ...interface{}) Warningln(args ...interface{}) Warnln(args ...interface{}) }
ILogger is a logger
type ISnmpServerListener ¶
type ISnmpServerListener interface { SetupLogger(ILogger) Address() net.Addr NextSnmp() (snmpbytes []byte, replyer IReplyer, err error) Shutdown() }
func NewUDPListener ¶
func NewUDPListener(l3proto, address string) (ISnmpServerListener, error)
type MasterAgent ¶
type MasterAgent struct { SecurityConfig SecurityConfig SubAgents []*SubAgent Logger ILogger // contains filtered or unexported fields }
MasterAgent identifys software which runs on managed devices
One server (port) could ONLY have one MasterAgent
func (*MasterAgent) ReadyForWork ¶
func (t *MasterAgent) ReadyForWork() error
func (*MasterAgent) ResponseForBuffer ¶
func (t *MasterAgent) ResponseForBuffer(i []byte) ([]byte, error)
func (*MasterAgent) ResponseForPkt ¶
func (t *MasterAgent) ResponseForPkt(i *gosnmp.SnmpPacket) (*gosnmp.SnmpPacket, error)
func (*MasterAgent) SyncConfig ¶
func (t *MasterAgent) SyncConfig() error
type PDUValueControlItem ¶
type PDUValueControlItem struct { // OID controls which OID does this PDUValue works OID string // Type defines which type this OID is. Type gosnmp.Asn1BER // NonWalkable marks this oid as not walkable. It **WILL NOT** returned in walk items. but do retuend // in direct get. // All write only item will be NonWalkable NonWalkable bool // OnCheckPermission will be called on access this OID. set to nil to allow all access. // return PermissionAllowanceAllowed for allow this access. // (otherwrise) PermissionAllowanceDenied for disable access. OnCheckPermission FuncPDUControlCheckPermission // OnGet will be called on any GET / walk option. set to nil for mark this as a write-only item OnGet FuncPDUControlGet // OnSet will be called on any Set option. set to nil for mark as a read-only item. OnSet FuncPDUControlSet // OnTrap will be called on TRAP. OnTrap FuncPDUControlTrap //Document for this PDU Item. ignored by the program. Document string }
PDUValueControlItem describe the action of get / set / walk in pdu tree
type PermissionAllowance ¶
type PermissionAllowance int
PermissionAllowance ENUM controls for Allowance
const PermissionAllowanceAllowed PermissionAllowance = 0
PermissionAllowanceAllowed allowed for access
const PermissionAllowanceDenied PermissionAllowance = 1
PermissionAllowanceDenied denies for access
type SNMPEngineID ¶
type SNMPEngineID struct { // See https://tools.ietf.org/html/rfc3411#section-5 // SnmpEngineID ::= TEXTUAL-CONVENTION // SYNTAX OCTET STRING (SIZE(5..32)) EngineIDData string }
func DefaultAuthoritativeEngineID ¶
func DefaultAuthoritativeEngineID() SNMPEngineID
func (*SNMPEngineID) Marshal ¶
func (t *SNMPEngineID) Marshal() []byte
type SNMPServer ¶
type SNMPServer struct {
// contains filtered or unexported fields
}
func NewSNMPServer ¶
func NewSNMPServer(master MasterAgent) *SNMPServer
func (*SNMPServer) Address ¶
func (server *SNMPServer) Address() net.Addr
func (*SNMPServer) ListenUDP ¶
func (server *SNMPServer) ListenUDP(l3proto, address string) error
func (*SNMPServer) ServeForever ¶
func (server *SNMPServer) ServeForever() error
func (*SNMPServer) ServeNextRequest ¶
func (server *SNMPServer) ServeNextRequest() (err error)
func (*SNMPServer) Shutdown ¶
func (server *SNMPServer) Shutdown()
type SecurityConfig ¶
type SecurityConfig struct { NoSecurity bool // AuthoritativeEngineID is SNMPV3 AuthoritativeEngineID AuthoritativeEngineID SNMPEngineID // AuthoritativeEngineBoots is SNMPV3 AuthoritativeEngineBoots AuthoritativeEngineBoots uint32 // OnGetAuthoritativeEngineTime will be called to get SNMPV3 AuthoritativeEngineTime // if sets to nil, the sys boottime will be used OnGetAuthoritativeEngineTime FuncGetAuthoritativeEngineTime Users []gosnmp.UsmSecurityParameters }
func (*SecurityConfig) FindForUser ¶
func (v *SecurityConfig) FindForUser(name string) *gosnmp.UsmSecurityParameters
type SnmpLoggerAdapter ¶
type SnmpLoggerAdapter struct {
ILogger
}
SnmpLoggerAdapter adapts a logger to gosnmp. wraps logger as trace
func (*SnmpLoggerAdapter) Print ¶
func (i *SnmpLoggerAdapter) Print(args ...interface{})
Print wraps trace
func (*SnmpLoggerAdapter) Printf ¶
func (i *SnmpLoggerAdapter) Printf(format string, args ...interface{})
Printf wraps trace
type SubAgent ¶
type SubAgent struct { CommunityIDs []string // OIDs for Read/Write actions OIDs []*PDUValueControlItem // UserErrorMarkPacket decides if shll treat user returned error as generr UserErrorMarkPacket bool Logger ILogger // contains filtered or unexported fields }
func (*SubAgent) Serve ¶
func (t *SubAgent) Serve(i *gosnmp.SnmpPacket) (*gosnmp.SnmpPacket, error)
func (*SubAgent) SyncConfig ¶
type UDPListener ¶
type UDPListener struct {
// contains filtered or unexported fields
}
func (*UDPListener) Address ¶
func (udp *UDPListener) Address() net.Addr
func (*UDPListener) SetupLogger ¶
func (udp *UDPListener) SetupLogger(i ILogger)
func (*UDPListener) Shutdown ¶
func (udp *UDPListener) Shutdown()
type UDPReplyer ¶
type UDPReplyer struct {
// contains filtered or unexported fields
}
func (*UDPReplyer) ReplyPDU ¶
func (r *UDPReplyer) ReplyPDU(i []byte) error
func (*UDPReplyer) Shutdown ¶
func (r *UDPReplyer) Shutdown()