golang snmp开发学习笔记(一)

wbchen2330 · · 5659 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

概述

  soniah/gosnmp是一个纯go语言编写的SNMP包,基于alouca/gosnmp开发。目前提供GetRequest、GetNext、GetBulk、Walk和SetRequest功能,支持IPv4和IPv6, 支持SNMPv2c和SNMPv3。gosnmp还在开发,目前不支持Trap,Walk和SetRequest也还不够完善,但作为一般的网络监控用途已经足够,如果需要还可以直接对源码进行修改补充。
  本文介绍soniah/gosnmp的GetRequest、GetNext和GetBulk操作。

资源

安装

go get github.com/soniah/gosnmp

范例 1. GetRequest

package main

import (
    "fmt"
    "log"
    g "github.com/soniah/gosnmp"
)

func main() {
    // Default is a pointer to a GoSNMP struct that contains
    // sensible defaults eg port 161, community public, etc
    g.Default.Target = "172.18.0.2"
    g.Default.Community = "community"
    err := g.Default.Connect()
    if err != nil {
        log.Fatalf("Connect() err: %v", err)
    }
    defer g.Default.Conn.Close()

    oids := []string{"1.3.6.1.2.1.1.5.0","1.3.6.1.2.1.1.7.0"}
    result, err := g.Default.Get(oids) // Get() accepts up to g.MAX_OIDS
    if err != nil {
        log.Fatalf("Get() err: %v", err)
    }

    for i, v := range result.Variables {
        fmt.Printf("%d. oid: %s ", i, v.Name)

        // the Value of each variable returned by Get() implements
        // interface{}. You could do a type switch...
        switch v.Type{
        case g.OctetString:
            fmt.Printf("string: %s\n", string(v.Value.([]byte)))
        default:
            // ... or often you're just interested in numeric values.
            // ToBigInt() will return the Value as a BigInt, for plugging
            // into your calculations.
            fmt.Printf("number: %d\n", g.ToBigInt(v.Value))
        }
    }
}

输出结果:

0. oid: .1.3.6.1.2.1.1.5.0 string: YD_WLZX1F_S2652
1. oid: .1.3.6.1.2.1.1.7.0 number: 3

范例 2. 自己构造GoSNMP结构

package main

import (
    "fmt"
    "log"
    "time"
    g "github.com/soniah/gosnmp"
)

func main() {
    // build our own GoSNMP struct, rather than using g.Default
    snmp := &g.GoSNMP{
        Target:    "172.18.0.2",
        Port:      161,
        Community: "community",
        Version:   g.Version2c,
        Timeout:   time.Duration(2) * time.Second,      
    }
    err := snmp.Connect()
    if err != nil {
        log.Fatalf("Connect() err: %v", err)
    }
    defer snmp.Conn.Close()

    oids := []string{"1.3.6.1.2.1.1.5.0","1.3.6.1.2.1.1.7.0"}
    result, err := snmp.Get(oids)
    if err != nil {
        log.Fatalf("Get() err: %v", err)
    }

    for i, v := range result.Variables {
        fmt.Printf("%d. oid: %s ", i, v.Name)
        switch v.Type{
        case g.OctetString:
            fmt.Printf("string: %s\n", string(v.Value.([]byte)))
        default:
            fmt.Printf("number: %d\n", g.ToBigInt(v.Value))
        }
    }   
}

输出结果:

0. oid: .1.3.6.1.2.1.1.5.0 string: YD_WLZX1F_S2652
1. oid: .1.3.6.1.2.1.1.7.0 number: 3

范例 3. SNMPv3

package main

import (
    "fmt"
    "log"
    "time"
    g "github.com/soniah/gosnmp"
)

func main() {
    snmp := &GoSNMP{
        Target:    "172.17.0.10",
        Port:      161,
        Community: "community",
        Version:   g.Version3,
        Timeout:   time.Duration(30) * time.Second, 
        SecurityModel: g.UserSecurityModel,
        MsgFlags: g.AuthPriv,  //认证且加密
        SecurityParameters: &g.UsmSecurityParameters {
            UserName: "user",
            AuthenticationProtocol: g.SHA,
            AuthenticationPassphrase: "password",
            PrivacyProtocol: g.DES,
            PrivacyPassphrase: "password",
        },
    }
    err := snmp.Connect()
    if err != nil {
        log.Fatalf("Connect() err: %v", err)
    }
    defer snmp.Conn.Close()

    oids := []string{"1.3.6.1.2.1.1.5.0","1.3.6.1.2.1.1.7.0"}
    result, err := snmp.Get(oids)
    if err != nil {
        log.Fatalf("Get() err: %v", err)
    }

    for i, v := range result.Variables {
        fmt.Printf("%d. oid: %s ", i, v.Name)
        switch v.Type{
        case g.OctetString:
            fmt.Printf("string: %s\n", string(v.Value.([]byte)))
        default:
            fmt.Printf("number: %d\n", g.ToBigInt(v.Value))
        }
    }   
}

todo: 范例3代码未验证。

范例 4. GetNext

package main

import (
    "fmt"
    "log"
    g "github.com/soniah/gosnmp"
)

func main() {
    g.Default.Target = "172.18.0.2"
    g.Default.Community = "community"
    err := g.Default.Connect()
    if err != nil {
        log.Fatalf("Connect() err: %v", err)
    }
    defer g.Default.Conn.Close()

    oids := []string{"1.3.6.1.2.1.1.5", "1.3.6.1.2.1.2.2.1.10"}
    result, err := g.Default.GetNext(oids)
    if err != nil {
        log.Fatalf("Get() err: %v", err)
    }

    for i, v := range result.Variables {
        fmt.Printf("%d. oid: %s ", i, v.Name)

        switch v.Type{
        case g.OctetString:
            fmt.Printf("string: %s\n", string(v.Value.([]byte)))
        default:
            fmt.Printf("number: %d\n", g.ToBigInt(v.Value))
        }
    }
}

输出结果:

0. oid: .1.3.6.1.2.1.1.5.0 string: YD_WLZX1F_S2652
1. oid: .1.3.6.1.2.1.2.2.1.10.1 number: 144611

范例 5. GetBulk

package main

import (
    "fmt"
    "log"
    g "github.com/soniah/gosnmp"
)

func main() {
    g.Default.Target = "172.18.0.2"
    g.Default.Community = "community"
    err := g.Default.Connect()
    if err != nil {
        log.Fatalf("Connect() err: %v", err)
    }
    defer g.Default.Conn.Close()

    oids := []string{
        "1.3.6.1.2.1.1.2",  //sysObjectID
        "1.3.6.1.2.1.1.5",  //sysName
        "1.3.6.1.2.1.2.2.1.1",   //ifIndex
        "1.3.6.1.2.1.2.2.1.10",  //ifInOctets
        "1.3.6.1.2.1.2.2.1.16",   //ifOutOctets
    }
    //前2个oid不重复,后3个重复10次,相当于执行5次GetNext
    result, err := g.Default.GetBulk(oids, 2, 5)
    if err != nil {
        log.Fatalf("Get() err: %v", err)
    }

    for i, v := range result.Variables {
        fmt.Printf("%d: %s = ", i, v.Name)
        switch v.Type{
        case g.OctetString:
            fmt.Printf("%s\n", string(v.Value.([]byte)))
        default:
            fmt.Printf("%d\n", g.ToBigInt(v.Value))
        }
    }   
}

输出结果:

0: .1.3.6.1.2.1.1.2.0 = 0
1: .1.3.6.1.2.1.1.5.0 = YD_WLZX1F_S2652
2: .1.3.6.1.2.1.2.2.1.1.1 = 1
3: .1.3.6.1.2.1.2.2.1.10.1 = 144611
4: .1.3.6.1.2.1.2.2.1.16.1 = 539306284
5: .1.3.6.1.2.1.2.2.1.1.2 = 2
6: .1.3.6.1.2.1.2.2.1.10.2 = 0
7: .1.3.6.1.2.1.2.2.1.16.2 = 0
8: .1.3.6.1.2.1.2.2.1.1.3 = 3
9: .1.3.6.1.2.1.2.2.1.10.3 = 70688630
10: .1.3.6.1.2.1.2.2.1.16.3 = 216291621
11: .1.3.6.1.2.1.2.2.1.1.4 = 4
12: .1.3.6.1.2.1.2.2.1.10.4 = 129777391
13: .1.3.6.1.2.1.2.2.1.16.4 = 1052726443
14: .1.3.6.1.2.1.2.2.1.1.5 = 5
15: .1.3.6.1.2.1.2.2.1.10.5 = 169733222
16: .1.3.6.1.2.1.2.2.1.16.5 = 641244267

有疑问加站长微信联系

本文来自:CSDN博客

感谢作者:wbchen2330

查看原文:golang snmp开发学习笔记(一)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:1006366459

5659 次点击  ∙  1 赞  
加入收藏 微博
1 回复  |  直到 2000-01-01 00:00:00
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传