Golang SNMP的使用

SNMP OID的獲取方法

Get:

支持單個或者多個oid,獲取結果爲固定oid的一對一值,例如:品牌/型號/轉發功能/

[root@es-9 ~]# snmpwalk -v 2c -c public123 192.168.40.2  .1.3.6.1.2.1.1.2.0
SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.2011.2.23.426
ObjectIdentifier .1.3.6.1.4.1.2011.2.23.426 ObjectIdentifier

Walk

包含了:GetNext,也許需要WalkFunc這個參數,(retrieves a subtree of values using GETNEXT.)

BulkWalk

包含了:GetBulk( retrieves a subtree of values using GETBULK.)

支持單個或者多個oid,發現數據後就取走,不往下面走了,例如: 序列號

[root@es-9 ~]# snmpwalk -v 2c -c public123 192.168.40.2 .1.3.6.1.2.1.47.1.1.1.1.11
SNMPv2-SMI::mib-2.47.1.1.1.1.11.67108867 = STRING: "21980106012SHB602060"
SNMPv2-SMI::mib-2.47.1.1.1.1.11.67108869 = ""
SNMPv2-SMI::mib-2.47.1.1.1.1.11.67108873 = STRING: "21980106012SHB602060"
SNMPv2-SMI::mib-2.47.1.1.1.1.11.67125260 = ""
OctetString  21980106012SHB602060 OctetString

其他廢棄

WalkAll和BulkWalkAll:

使用方法一樣,BulkWalkAll,只是使用了更爲高效的GetBulk操作

傳入參數單個OID,獲取單個OID下面所有的同類信息的值,獲取設備端口數量

[root@es-9 ~]# snmpwalk -v 2c -c public123 192.168.40.2 .1.3.6.1.2.1.2.2.1.2
IF-MIB::ifDescr.1 = STRING: InLoopBack0
IF-MIB::ifDescr.2 = STRING: NULL0
IF-MIB::ifDescr.3 = STRING: Console9/0/0
IF-MIB::ifDescr.4 = STRING: Vlanif1
IF-MIB::ifDescr.5 = STRING: GigabitEthernet0/0/1
IF-MIB::ifDescr.6 = STRING: GigabitEthernet0/0/2
IF-MIB::ifDescr.7 = STRING: GigabitEthernet0/0/3
OctetString  Vlanif20

用例:需要WalkFunc這個參數,獲取設備端口數量

err2 := s.client.Walk(".1.3.6.1.2.1.2.2.1.2",printValue)
if err2 != nil {
	fmt.Println("GetModeV2 snmp conn switch v3, info: %v", err2)
}
	
func printValue(pdu g.SnmpPDU) error {
	fmt.Printf("%s = ", pdu.Name)

	switch pdu.Type {
	case g.OctetString:
		b := pdu.Value.([]byte)
		fmt.Printf("STRING: %s\n", string(b))
	default:
		fmt.Printf("TYPE %d: %d\n", pdu.Type, g.ToBigInt(pdu.Value))
	}
	return nil
}

測試代碼

package main

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

func main() {
	oids := []string{".1.3.6.1.2.1.1.2.0"}
	s := NewSnmpClientV2("192.168.40.2", "public123", uint16(161))

	err1 := s.client.Connect()
	if err1 != nil {
		fmt.Println("GetModeV2 snmp connect failure,err: %v", err1.Error())
	}
	defer s.client.Conn.Close()

	res, err2 := s.client.Get(oids)
	if err2 != nil {
		fmt.Println("GetModeV2 snmp conn switch v3, info: %v", err2)
	} else {
		for _, v := range res.Variables {
			switch v.Type {
			case g.ObjectIdentifier:
				fmt.Println(v.Value.(string), "ObjectIdentifier")
			case g.OctetString:
				tempStr := strings.Split(string(v.Value.([]uint8)), "\r")
				fmt.Println(tempStr[0], "OctetString")
			case g.Integer:
				temp := v.Value.(int)
				fmt.Println(strconv.Itoa(temp), "g.Integer")
			}
		}
	}
}

func NewSnmpClientV2(target, community string, port uint16) *SnmpClient {
	return &SnmpClient{
		g.GoSNMP{
			Target:         target,
			Community:      community,
			Port:           port,
			Version:        g.Version2c,
			Timeout:        time.Duration(2) * time.Second,
			Retries:        1,
			MaxOids:        1,
			MaxRepetitions: 2,
			Logger:         nil,
		},
	}
}

type SnmpClient struct {
	client g.GoSNMP
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章