forked from gosnmp/gosnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
walk.go
98 lines (85 loc) · 2.46 KB
/
walk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2012-2016 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
}