3 Star 0 Fork 0

Gitee 极速下载/gosnmp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/soniah/gosnmp
克隆/下载
walk.go 2.46 KB
一键复制 编辑 原始数据 按行查看 历史
Sonia Hamilton 提交于 2017-12-09 13:22 . fix copyright, license
// Copyright 2012-2018 The GoSNMP Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
package gosnmp
import (
"fmt"
"strings"
)
func (x *GoSNMP) walk(getRequestType PDUType, rootOid string, walkFn WalkFunc) error {
if rootOid == "" || rootOid == "." {
rootOid = baseOid
}
if !strings.HasPrefix(rootOid, ".") {
rootOid = string(".") + rootOid
}
oid := rootOid
requests := 0
maxReps := x.MaxRepetitions
if maxReps == 0 {
maxReps = defaultMaxRepetitions
}
RequestLoop:
for {
requests++
var response *SnmpPacket
var err error
switch getRequestType {
case GetBulkRequest:
response, err = x.GetBulk([]string{oid}, uint8(x.NonRepeaters), uint8(maxReps))
case GetNextRequest:
response, err = x.GetNext([]string{oid})
case GetRequest:
response, err = x.Get([]string{oid})
default:
response, err = nil, fmt.Errorf("Unsupported request type: %d", getRequestType)
}
if err != nil {
return err
}
if len(response.Variables) == 0 {
break RequestLoop
}
if response.Error == NoSuchName {
x.Logger.Print("Walk terminated with NoSuchName")
break RequestLoop
}
for k, v := range response.Variables {
if v.Type == EndOfMibView || v.Type == NoSuchObject || v.Type == NoSuchInstance {
x.Logger.Printf("BulkWalk terminated with type 0x%x", v.Type)
break RequestLoop
}
if !strings.HasPrefix(v.Name, rootOid+".") {
// Not in the requested root range.
// if this is the first request, and the first variable in that request
// and this condition is triggered - the first result is out of range
// need to perform a regular get request
// this request has been too narrowly defined to be found with a getNext
// Issue #78 #93
if requests == 1 && k == 0 {
getRequestType = GetRequest
continue RequestLoop
}
break RequestLoop
}
if v.Name == oid {
return fmt.Errorf("OID not increasing: %s", v.Name)
}
// Report our pdu
if err := walkFn(v); err != nil {
return err
}
}
// Save last oid for next request
oid = response.Variables[len(response.Variables)-1].Name
}
x.Logger.Printf("BulkWalk completed in %d requests", requests)
return nil
}
func (x *GoSNMP) walkAll(getRequestType PDUType, rootOid string) (results []SnmpPDU, err error) {
err = x.walk(getRequestType, rootOid, func(dataUnit SnmpPDU) error {
results = append(results, dataUnit)
return nil
})
return results, err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/gosnmp.git
git@gitee.com:mirrors/gosnmp.git
mirrors
gosnmp
gosnmp
v1.12.0

搜索帮助