分享一个go开发的工具-SNMP Server

GoSNMPServer是一个不错的开源工具,使用go开发,跨平台,可以很方便移植到各个平台上运行,之前用客户端一直是用的net-snmp,最近需要用到snmp的服务端(代理),net-snmp里的snmpd交叉编译有点麻烦,后来编译出来了,结果还用不了……
这个go开发的snmpserver是个不错的选择。

码云地址: https://gitee.com/wwhai/GoSNMPServer?_from=gitee_search

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/slayercat/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 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5ViU674o-1655102817857)(https://godoc.org/github.com/slayercat/GoSNMPServe/mibImpsr?status.png)] 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)

欢迎关注,发现与分享更多好用工具:

猜你喜欢

转载自blog.csdn.net/wuquan_1230/article/details/125259962