GoSNMPServer 是一个完全用 Go 编写的 SNMP 服务器库。它提供 Server Get、GetNext、GetBulk、Walk、BulkWalk、Set 和 Traps 等功能。它支持 IPv4 和 IPv6,使用 SNMPv2c 或 SNMPv3。其构建版本已针对 linux/amd64 和 linux/386 进行测试。
构建您自己的 SNMP 服务器,尝试以下操作:
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
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()
此库提供了一些常用的 OID。代码请见 mibImps,GoDoc 此处。
将 GoSNMPServer.PDUValueControlItem 附加到您的 SubAgent OID 中:
{
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",
},
支持类型:请参阅 RFC-2578 FOR SMI
可以使用 wrap 函数来检测类型错误。请参阅 GoSNMPServer.Asn1IntegerWrap / GoSNMPServer.Asn1IntegerUnwrap 等函数。
此库基于 soniah/gosnmp 进行编码器/解码器开发。