diff --git a/README.md b/README.md index 4757026..f4550c7 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,9 @@ For support and help with this fork, open an [issue](https://github.com/netdata/ GoSNMP has the following SNMP functions: * **NewClient** - makes a client of a `Config`, which it checks, with defaults for a zero port (161), timeout (1 s) and `MaxOids` (60). -* **Get** - one or more OIDs, up to `MaxOids` (60 by default). -* **WalkAll** - retrieve a subtree of values using GETNEXT. -* **BulkWalkAll** - retrieve a subtree of values using GETBULK (SNMPv2c and SNMPv3 only), with the walk's max-repetitions. +* **Get** - one or more OIDs, up to `MaxOids` (60 by default); an answer with an error-status fails it with a `*StatusError`. +* **WalkAll** - retrieve a subtree of values using GETNEXT, and how the walk ended (`WalkEnd`). +* **BulkWalkAll** - retrieve a subtree of values using GETBULK (SNMPv2c and SNMPv3 only), with the walk's max-repetitions, and how the walk ended. * **Receiver** - decodes received traps and informs with its SNMPv3 users, by engine ID, and returns the answers the RFCs give: the Response to an inform, as its authoritative engine, and the Reports a sender discovers its engine ID and time by; the library does not listen for notifications or send answers. * **EncodeMessage** and **DecodeMessage** - encode and decode messages for other transports and for tests. diff --git a/doc.go b/doc.go index f88080b..40f118c 100644 --- a/doc.go +++ b/doc.go @@ -87,9 +87,9 @@ // SNMPv2c Response with one of its request IDs, else the last one whose ID // could not be read, as one of another SNMP version; messages to other // requests do not count), and its text ends with ": last discarded reply: " -// and that reason's text. An answer's error status ([Message].ErrorStatus) is -// not an error of the request. Connect uses Timeout and its context too, for -// its lookup of Target and its dial together. +// and that reason's text. A Response whose error-status is not NoError ends +// the request with a [*StatusError] (see Errors). Connect uses Timeout and its +// context too, for its lookup of Target and its dial together. // // # Panics // @@ -215,7 +215,12 @@ // # Walks // // [Client.WalkAll] and [Client.BulkWalkAll] return the values of a subtree, in -// order. BulkWalkAll takes the max-repetitions of its GETBULK requests as an +// order, and how the walk ended ([WalkEnd]): the reason, and the varbind that +// ended it, such as the first name outside the subtree, an EndOfMibView, or +// the answer to the Get of a root that is a leaf, whose NoSuchObject or +// NoSuchInstance tells an object the agent does not have from an instance it +// does not have. A walk that fails returns the values before its error. +// BulkWalkAll takes the max-repetitions of its GETBULK requests as an // argument. Their docs say how a walk starts and ends. // // # Values @@ -508,14 +513,21 @@ // matches none of them, nor does a transport error or a timeout without a // discarded reply. // +// A request answered by a Response whose error-status is not NoError fails +// with a [*StatusError], whose fields give the status and the error-index +// ([errors.As]); it matches no category, since the answer is a valid message. +// So does a walk, but for the statuses its end rule takes for an end (see +// [Client.BulkWalkAll]). +// // [ErrAnswered] says whether the agent's answer ended a request: the error of -// a request (a walk's included) that a Report or a discovery answer without an -// engine ID ended also matches it, besides the answer's category, while the -// library marks no error of a request that ended without an answer, a timeout -// after discarded replies included; errors of the caller's own code (a context -// it gave, and a panic in one) pass through unchanged. A failed request returns -// no message, so this is how a caller tells an agent that answered from one -// that did not. +// a request (a walk's included) that a Report, a discovery answer without an +// engine ID or a Response with an error-status (a [*StatusError]) ended also +// matches it, besides the answer's category when it has one, while the library +// marks no error of a request that ended without an answer, a timeout after +// discarded replies included; errors of the caller's own code (a context it +// gave, and a panic in one) pass through unchanged. A failed request returns no +// message, so this is how a caller tells an agent that answered from one that +// did not. // // The categories are variables only so that errors.Is can match them; do not // assign to them. The library makes some of its errors with their values when diff --git a/engine.go b/engine.go index e297ba0..a6c5531 100644 --- a/engine.go +++ b/engine.go @@ -16,15 +16,17 @@ import ( ) // send runs the request packetOut (request) within ctx and returns its -// answer, or no message with the error of a request that fails, marked with -// ErrAnswered when an answer ended it (the message request returns beside its -// error), and with requestPanicked's error for one that panics. A nil ctx -// fails the request before it starts. The library prevents panics of its -// own: the recover is for the caller's code the request runs on its -// goroutine (the methods of the context it calls) and for bugs, which would -// otherwise end the process of a caller that runs requests on a goroutine -// without a recover. A flag, not recover's value, tells that the request -// panicked: under GODEBUG=panicnil=1, recover returns nil for panic(nil). +// answer, a Response whose error-status is NoError, or no message with the +// error of a request that fails: marked with ErrAnswered when an answer ended +// it (the message request returns beside its error), requestPanicked's error +// for one that panics, and a *StatusError for a Response with another +// error-status. A nil ctx fails the request before it starts. The library +// prevents panics of its own: the recover is for the caller's code the request +// runs on its goroutine (the methods of the context it calls) and for bugs, +// which would otherwise end the process of a caller that runs requests on a +// goroutine without a recover. A flag, not recover's value, tells that the +// request panicked: under GODEBUG=panicnil=1, recover returns nil for +// panic(nil). // // packetOut is a request of the Read Class (RFC 3411 section 2.8.1), a Get, // GetNext or GetBulk, as every request the client sends: the SNMPv3 engine @@ -48,6 +50,9 @@ func (x *Client) send(ctx context.Context, packetOut *Message) (result *Message, } return nil, err } + if result.ErrorStatus != NoError { + return nil, &StatusError{Status: result.ErrorStatus, Index: result.ErrorIndex} + } return result, nil } diff --git a/engine_requests_test.go b/engine_requests_test.go index 7a08638..6e6cc64 100644 --- a/engine_requests_test.go +++ b/engine_requests_test.go @@ -423,6 +423,16 @@ func engineScenarios() map[string]engineScenario { }, "answer/empty-with-error-status-wrong-id": {agent: answerWith(func(p *Message) { p.RequestID += 100; p.ErrorStatus = GenErr })}, "answer/error-status": {agent: answerWith(func(p *Message) { p.ErrorStatus, p.ErrorIndex = NoSuchName, 1 }, VarBind{Name: engineOID, Value: NullValue()})}, + // Error statuses as agents send them: the request's varbinds echoed + // (RFC 3416 section 4.2.1), none for tooBig, an index past them, and a + // status RFC 3416 does not name. + "answer/error-status/gen-err": {agent: answerWith(func(p *Message) { p.ErrorStatus, p.ErrorIndex = GenErr, 1 }, VarBind{Name: engineOID, Value: NullValue()})}, + "answer/error-status/too-big": {agent: answerWith(func(p *Message) { p.ErrorStatus = TooBig })}, + "answer/error-status/index-past": {agent: answerWith(func(p *Message) { p.ErrorStatus, p.ErrorIndex = NoAccess, 5 }, VarBind{Name: engineOID, Value: NullValue()})}, + "answer/error-status/unknown": {agent: answerWith(func(p *Message) { p.ErrorStatus, p.ErrorIndex = InconsistentName+1, 1 }, sysDescr)}, + "answer/error-status/v1-no-such-name": { + v1: true, agent: answerWith(func(p *Message) { p.ErrorStatus, p.ErrorIndex = NoSuchName, 1 }, VarBind{Name: engineOID, Value: NullValue()}), + }, "answer/not-a-response": { agent: answerWith(func(p *Message) { p.PDUType = GetRequest }, sysDescr), }, diff --git a/engine_v3_test.go b/engine_v3_test.go index 29f0d53..e099c63 100644 --- a/engine_v3_test.go +++ b/engine_v3_test.go @@ -674,6 +674,17 @@ func v3Scenarios() map[string]v3Scenario { } return agentAnswer{}, false }}, + // A Response with an error status, after discovery. + "answer/error-status": {user: "codec-sha-aes", script: onRequest(2, agentAnswer{ + level: AuthPriv, why: "answers the request with noAccess", + edit: func(p *agentOut) { p.ErrorStatus, p.ErrorIndex = NoAccess, 1 }, + })}, + // A Report with an error status: the Report's counter, not the status, + // is the request's error. + "report/with-error-status": {user: "codec-sha-aes", script: onRequest(2, agentAnswer{ + report: usmStatsUnknownUserNames, why: "rejects the request with a Report carrying genErr", + edit: func(p *agentOut) { p.ErrorStatus, p.ErrorIndex = GenErr, 1 }, + })}, // A Report another agent with the same credentials sent, for another // message or with the request's msgID, followed by two Gets. "answer/other-agent-report/other-message": {user: "codec-md5", run: getThrice(nil), script: onRequest(2, agentAnswer{ diff --git a/engine_walk_test.go b/engine_walk_test.go index a9f6391..054e286 100644 --- a/engine_walk_test.go +++ b/engine_walk_test.go @@ -168,8 +168,7 @@ type walkScenario struct { script func(n int, req, out *Message) bool // context, when set, gives the walk's context; its cancel runs when the // scenario ends. - context func() (context.Context, context.CancelFunc) - knownBug string + context func() (context.Context, context.CancelFunc) } // onWalkRequest changes the answer to request n only. @@ -260,13 +259,13 @@ func walkScenarios() map[string]walkScenario { root: ".1.3.6.1.2.1.1.5.0", script: onWalkRequest(2, errorStatus(GenErr)), }, - // The leaf's Get answered with a status no RFC names. + // The leaf's Get answered with a status no RFC names and the root's + // value: an error-status, so no value. "leaf/get-unknown-status": { root: ".1.3.6.1.2.1.1.5.0", script: onWalkRequest(2, func(_, out *Message) { out.ErrorStatus = InconsistentName + 1 }), - knownBug: "an error status the walk does not name is ignored and the root's value passed on", }, // The leaf's Get answered with another name before the root's value. "leaf/get-other-name-first": { @@ -279,15 +278,10 @@ func walkScenarios() map[string]walkScenario { script: onWalkRequest(2, varbinds()), }, - // Answers a walk does not expect. - "empty-response": { - root: walkSystem, script: onWalkRequest(2, varbinds()), - knownBug: "an empty response ends the walk with a nil error", - }, - "empty-response/bulk": { - root: walkSystem, bulk: true, script: onWalkRequest(1, varbinds()), - knownBug: "an empty response ends the walk with a nil error", - }, + // Answers a walk does not expect. An empty answer ends the walk + // without an error, also as the first answer. + "empty-response": {root: walkSystem, script: onWalkRequest(2, varbinds())}, + "empty-response/bulk": {root: walkSystem, bulk: true, script: onWalkRequest(1, varbinds())}, "not-increasing/equal": {root: walkSystem, script: onWalkRequest(2, varbinds(walkMIB[0]))}, "not-increasing/decreasing": {root: walkIfDescr, script: onWalkRequest(3, varbinds(ifDescr(0)))}, // Each name is compared with the request's, as in net-snmp's @@ -362,29 +356,40 @@ func walkScenarios() map[string]walkScenario { }, } - // Every error status the walk names, and one it does not. NoSuchName - // ends a walk without an error in net-snmp's snmpwalk too. + // Every error status RFC 3416 names, and two it does not: each but + // NoSuchName, which ends a walk without an error as in net-snmp's + // snmpwalk, fails the walk with the values before it. for status := TooBig; status <= InconsistentName; status++ { sc := walkScenario{root: walkSystem, script: onWalkRequest(2, errorStatus(status))} - if status != NoSuchName { - sc.knownBug = "an error status ends the walk with a nil error and the values so far" - } scenarios[fmt.Sprintf("error-status/%d-%v", status, status)] = sc } scenarios["error-status/19-unknown"] = walkScenario{ root: walkSystem, script: onWalkRequest(2, errorStatus(InconsistentName+1)), - knownBug: "an error status the walk does not name is ignored and the varbinds walked", } scenarios["error-status/19-unknown-empty"] = walkScenario{ root: walkSystem, script: onWalkRequest(2, func(_, out *Message) { out.ErrorStatus, out.ErrorIndex, out.VarBinds = InconsistentName+1, 0, nil }), - knownBug: "an empty response ends the walk with a nil error", } scenarios["error-status/bulk-too-big"] = walkScenario{ root: walkSystem, bulk: true, script: onWalkRequest(1, errorStatus(TooBig)), - knownBug: "an error status ends the walk with a nil error and the values so far", + } + // Another error status as a walk's first answer: nothing collected, no + // Get of the root. + scenarios["error-status/first-answer-gen-err"] = walkScenario{ + root: walkSystem, script: onWalkRequest(1, errorStatus(GenErr)), + } + // tooBig answering a later GetBulk, after values. + scenarios["error-status/bulk-too-big-later"] = walkScenario{ + root: walkIfDescr, bulk: true, maxRepetitions: 1, script: onWalkRequest(2, errorStatus(TooBig)), + } + // The leaf's Get answered with tooBig and with noSuchName in SNMPv2c. + scenarios["leaf/get-too-big"] = walkScenario{ + root: ".1.3.6.1.2.1.1.5.0", script: onWalkRequest(2, errorStatus(TooBig)), + } + scenarios["leaf/get-no-such-name"] = walkScenario{ + root: ".1.3.6.1.2.1.1.5.0", script: onWalkRequest(2, errorStatus(NoSuchName)), } // NoSuchName as an SNMPv2c walk's first answer, with and without the // request's varbinds: the status is read first, and the walk gets the root. @@ -401,8 +406,8 @@ func walkScenarios() map[string]walkScenario { } // TestEngineWalkCharacterization pins what WalkAll and BulkWalkAll request -// and return, known bugs included: the subtree walked, the leaf fallback to a -// Get, the end of the MIB, error statuses, exception values and the +// and return, and how each walk ended: the subtree walked, the leaf fallback +// to a Get, the end of the MIB, error statuses, exception values and the // increasing check. // testdata/engine/walk.golden holds the transcripts. func TestEngineWalkCharacterization(t *testing.T) { @@ -476,11 +481,11 @@ func TestEngineWalkRootSpellings(t *testing.T) { root := spelling.root(tc.root) walk := x.WalkAll if api == "BulkWalkAll" { - walk = func(ctx context.Context, rootOid string) ([]VarBind, error) { + walk = func(ctx context.Context, rootOid string) ([]VarBind, WalkEnd, error) { return x.BulkWalkAll(ctx, rootOid, 0) } } - got, err := walk(context.Background(), root) + got, _, err := walk(context.Background(), root) if tc.equal { if err == nil || !strings.Contains(err.Error(), "OID not increasing") { t.Errorf("error = %v, want OID not increasing", err) @@ -522,15 +527,17 @@ func runWalkScenario(t *testing.T, sc walkScenario) string { tr.addf("client: %v root=%q bulk=%t max-repetitions=%d", x.config.Version, sc.root, sc.bulk, sc.maxRepetitions) walk := x.WalkAll if sc.bulk { - walk = func(ctx context.Context, rootOid string) ([]VarBind, error) { + walk = func(ctx context.Context, rootOid string) ([]VarBind, WalkEnd, error) { return x.BulkWalkAll(ctx, rootOid, sc.maxRepetitions) } } var err error run := func() { var vbs []VarBind - vbs, err = walk(ctx, sc.root) + var end WalkEnd + vbs, end, err = walk(ctx, sc.root) tr.addf("values: %s", describeVarbinds(vbs)) + tr.addf("end: %s", describeWalkEnd(end)) } if v, _ := recoverPanic(run); v != nil { tr.addf("panic: %v", v) @@ -539,12 +546,24 @@ func runWalkScenario(t *testing.T, sc walkScenario) string { } else { tr.addf("result: no error") } - if sc.knownBug != "" { - tr.addf("known bug: %s", sc.knownBug) - } return tr.text() } +// describeWalkEnd describes how a walk ended: none for the zero WalkEnd, else +// its reason and its varbind, when it has one. +func describeWalkEnd(end WalkEnd) string { + if end.Reason == 0 { + if end.VarBind.Name != "" || end.VarBind.Value.Type() != UnknownType { + return fmt.Sprintf("none with a varbind %s", describeVarbinds([]VarBind{end.VarBind})) + } + return "none" + } + if end.VarBind.Name == "" && end.VarBind.Value.Type() == UnknownType { + return end.Reason.String() + } + return fmt.Sprintf("%v %s", end.Reason, describeVarbinds([]VarBind{end.VarBind})) +} + // BenchmarkWalk runs WalkAll and BulkWalkAll, as the Netdata Agent's SNMP // collector does, over a 100-row table column on the in-memory agent. func BenchmarkWalk(b *testing.B) { @@ -555,18 +574,18 @@ func BenchmarkWalk(b *testing.B) { } mib = append(mib, VarBind{Name: ".1.3.6.1.4.1.99999.1.1.1.1.2.3.5.1", Value: IntegerValue(1)}) - for name, walkAll := range map[string]func(x *Client) ([]VarBind, error){ - "WalkAll": func(x *Client) ([]VarBind, error) { return x.WalkAll(context.Background(), root) }, - "BulkWalkAll": func(x *Client) ([]VarBind, error) { return x.BulkWalkAll(context.Background(), root, 0) }, + for name, walkAll := range map[string]func(x *Client) ([]VarBind, WalkEnd, error){ + "WalkAll": func(x *Client) ([]VarBind, WalkEnd, error) { return x.WalkAll(context.Background(), root) }, + "BulkWalkAll": func(x *Client) ([]VarBind, WalkEnd, error) { return x.BulkWalkAll(context.Background(), root, 0) }, } { b.Run(name, func(b *testing.B) { agent := &mibAgent{mib: mib} x := newEngineClient(b, Version2c, newFakeTransport(nil, agent.handle)) b.ReportAllocs() for b.Loop() { - vbs, err := walkAll(x) - if err != nil || len(vbs) != 100 { - b.Fatalf("%d values, error %v", len(vbs), err) + vbs, end, err := walkAll(x) + if err != nil || len(vbs) != 100 || end.Reason != WalkOutside { + b.Fatalf("%d values, end %v, error %v", len(vbs), end.Reason, err) } } }) @@ -575,38 +594,48 @@ func BenchmarkWalk(b *testing.B) { // TestWalkEndedByReport pins that a walk an SNMPv3 Report ends returns the // values before it with an error that matches ErrAnswered and the Report's -// category: the agent answers the discovery and the walk's first request, then -// rejects the next with a usmStatsUnknownUserNames Report. +// category, and no end: the agent answers the discovery and the walk's first +// request, then rejects the next with a usmStatsUnknownUserNames Report, the +// walk's second request or the Get of a leaf root. func TestWalkEndedByReport(t *testing.T) { - const root = ".1.3.6.1.2.1.2" - first := VarBind{Name: root + ".1.0", Value: IntegerValue(1)} - walks := map[string]func(*Client, context.Context, string) ([]VarBind, error){ + first := VarBind{Name: ".1.3.6.1.2.1.2.1.0", Value: IntegerValue(1)} + cases := map[string]struct { + root string + want []VarBind + }{ + "second-request": {root: ".1.3.6.1.2.1.2", want: []VarBind{first}}, + "leaf-get": {root: ".1.3.6.1.2.1.1.5.0"}, // the first answer, first, is outside it + } + walks := map[string]func(*Client, context.Context, string) ([]VarBind, WalkEnd, error){ "WalkAll": (*Client).WalkAll, - "BulkWalkAll": func(x *Client, ctx context.Context, rootOid string) ([]VarBind, error) { + "BulkWalkAll": func(x *Client, ctx context.Context, rootOid string) ([]VarBind, WalkEnd, error) { return x.BulkWalkAll(ctx, rootOid, 0) }, } - for name, walk := range walks { - t.Run(name, func(t *testing.T) { - agent := newFakeV3Agent(nil, agentCreds, first) - agent.script = func(n int, _ agentRequest) (agentAnswer, bool) { - if n < 3 { - return agentAnswer{}, false + for name, tc := range cases { + for api, walk := range walks { + t.Run(name+"/"+api, func(t *testing.T) { + agent := newFakeV3Agent(nil, agentCreds, first) + agent.script = func(n int, _ agentRequest) (agentAnswer, bool) { + if n < 3 { + return agentAnswer{}, false + } + return agentAnswer{report: usmStatsUnknownUserNames, why: "rejects the walk's second request"}, true } - return agentAnswer{report: usmStatsUnknownUserNames, why: "rejects the walk's second request"}, true - } - c := newFakeTransport(nil, agent.handle) - x := newEngineClientFrom(t, c, Config{ - Version: Version3, - SecurityLevel: NoAuthNoPriv, - User: agentCreds["codec-noauth"].user("codec-noauth"), - Timeout: time.Second, - Retries: 2, + c := newFakeTransport(nil, agent.handle) + x := newEngineClientFrom(t, c, Config{ + Version: Version3, + SecurityLevel: NoAuthNoPriv, + User: agentCreds["codec-noauth"].user("codec-noauth"), + Timeout: time.Second, + Retries: 2, + }) + values, end, err := walk(x, context.Background(), tc.root) + require.ErrorIs(t, err, ErrAnswered) + require.ErrorIs(t, err, ErrUnknownUsername) + require.Equal(t, tc.want, values) + require.Equal(t, WalkEnd{}, end) }) - values, err := walk(x, context.Background(), root) - require.ErrorIs(t, err, ErrAnswered) - require.ErrorIs(t, err, ErrUnknownUsername) - require.Equal(t, []VarBind{first}, values) - }) + } } } diff --git a/errors.go b/errors.go index 1af4946..8fb37bf 100644 --- a/errors.go +++ b/errors.go @@ -4,7 +4,10 @@ package gosnmp -import "errors" +import ( + "errors" + "fmt" +) // The categories of the errors of incoming messages. Each error of decoding a // message, of its security processing and of matching a reply to a request @@ -73,8 +76,9 @@ var ( // ErrAnswered marks the error of a request that the agent's answer ended: an // SNMPv3 Report that answers it, whose error also matches the category of the -// Report's counter, or a discovery answer that names no engine ID (an error that -// also matches ErrUnknownEngineID). It is not a category: the error keeps its +// Report's counter, a discovery answer that names no engine ID (an error that +// also matches ErrUnknownEngineID), or a Response whose error-status is not +// NoError (a *StatusError). It is not a category: the error keeps its // own, and its text. The library marks no error of a request that ended // without an answer: a timeout or the context's deadline, also after discarded // replies, whose category the error then matches; the context, Close, a @@ -109,3 +113,27 @@ func inCategory(category, err error) error { func answered(err error) error { return &categorized{errs: [2]error{err, ErrAnswered}} } + +// StatusError is the error of a request answered by a Response whose +// error-status (RFC 3416 section 3) is not NoError: a Get's, and a walk's but +// for the statuses its end rule takes for an end (see BulkWalkAll). A request +// that fails with it returns no message. It matches ErrAnswered and no +// category: the answer is a valid message. errors.As gives the status and the +// index. +type StatusError struct { + // Status is the error-status the answer carried, a value RFC 3416 does + // not name included. + Status ErrorStatus + // Index is the error-index: the position, from 1, of the varbind the + // status is about in the request, 0 for none, as decoded (255 for a + // larger one). + Index uint8 +} + +func (e *StatusError) Error() string { + return fmt.Sprintf("the agent answered with error-status %v, error-index %d", e.Status, e.Index) +} + +// Is reports whether target is ErrAnswered: the agent's answer ended the +// request. +func (e *StatusError) Is(target error) bool { return target == ErrAnswered } diff --git a/errors_test.go b/errors_test.go index 1dc3866..5a7b06f 100644 --- a/errors_test.go +++ b/errors_test.go @@ -5,8 +5,10 @@ package gosnmp import ( + "context" "errors" "testing" + "testing/synctest" "github.com/netdata/gosnmp/internal/ber" ) @@ -139,3 +141,67 @@ func TestUnmatchedReplyReasons(t *testing.T) { } } } + +// TestStatusError pins the error of a Response with an error-status as a +// caller reads it: Get returns no message and a *StatusError whose fields are +// the answer's status and index, which matches ErrAnswered and no category; a +// walk returns the values before it with it and no end. +func TestStatusError(t *testing.T) { + gets := map[string]struct { + version Version + status ErrorStatus + index uint8 + }{ + "v1 noSuchName": {version: Version1, status: NoSuchName, index: 2}, + "v2c genErr": {version: Version2c, status: GenErr, index: 1}, + "v2c tooBig": {version: Version2c, status: TooBig}, + "v2c unnamed": {version: Version2c, status: InconsistentName + 1, index: 3}, + "v2c index past it": {version: Version2c, status: NoAccess, index: 255}, + } + for name, tc := range gets { + t.Run("Get/"+name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + agent := answerWith(func(p *Message) { p.ErrorStatus, p.ErrorIndex = tc.status, tc.index }) + x := newEngineClient(t, tc.version, newFakeTransport(nil, agent)) + result, err := x.Get(context.Background(), []string{engineOID, ".1.3.6.1.2.1.1.5.0"}) + var got *StatusError + if !errors.As(err, &got) { + t.Fatalf("error = %v, want a *StatusError", err) + } + if *got != (StatusError{Status: tc.status, Index: tc.index}) { + t.Errorf("StatusError = %+v, want status %v index %d", *got, tc.status, tc.index) + } + if result != nil { + t.Errorf("result = %v, want none", result) + } + if !errors.Is(err, ErrAnswered) { + t.Error("error does not match ErrAnswered") + } + for _, c := range errorCategories { + if errors.Is(err, c.err) { + t.Errorf("error matches the category %s", c.name) + } + } + }) + }) + } + t.Run("BulkWalkAll", func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + agent := &mibAgent{mib: walkMIB, script: onWalkRequest(2, func(_, out *Message) { + out.ErrorStatus, out.ErrorIndex, out.VarBinds = TooBig, 0, nil + })} + x := newEngineClient(t, Version2c, newFakeTransport(nil, agent.handle)) + values, end, err := x.BulkWalkAll(context.Background(), walkIfDescr, 1) + var got *StatusError + if !errors.As(err, &got) || *got != (StatusError{Status: TooBig}) { + t.Fatalf("error = %v, want the *StatusError of tooBig", err) + } + if len(values) != 1 || values[0].Name != walkMIB[3].Name { + t.Errorf("values = %v, want the first answer's", values) + } + if end.Reason != 0 { + t.Errorf("end = %v, want none", end.Reason) + } + }) + }) +} diff --git a/examples/walkexample/main.go b/examples/walkexample/main.go index e78fb72..5fbb258 100644 --- a/examples/walkexample/main.go +++ b/examples/walkexample/main.go @@ -63,7 +63,7 @@ func main() { // maxRepetitions is how many objects each GETBULK request asks for: net-snmp // snmpbulkwalk's default; 0 asks for the library's, 50. const maxRepetitions = 10 - values, err := client.BulkWalkAll(ctx, oid, maxRepetitions) + values, end, err := client.BulkWalkAll(ctx, oid, maxRepetitions) for _, vb := range values { printValue(vb) } @@ -71,6 +71,11 @@ func main() { fmt.Printf("Walk Error: %v\n", err) os.Exit(1) } + // end says how the walk ended: past the subtree, at an exception value + // such as EndOfMibView, or with the Get of a leaf root, whose + // NoSuchObject or NoSuchInstance tells a missing object from a missing + // instance. + fmt.Printf("End of walk: %v %s\n", end.Reason, end.VarBind.Name) } func printValue(vb gosnmp.VarBind) { diff --git a/generic_e2e_test.go b/generic_e2e_test.go index 8e48276..c5686d8 100644 --- a/generic_e2e_test.go +++ b/generic_e2e_test.go @@ -165,26 +165,32 @@ func TestGenericWalk(t *testing.T) { gs := connectToTarget(t, testConfig()) defer gs.Close() - result, err := gs.WalkAll(context.Background(), "") + result, end, err := gs.WalkAll(context.Background(), "") if err != nil { t.Fatalf("WalkAll() Failed with error => %v", err) } if len(result) <= 1 { t.Fatalf("Expected multiple values, got %d", len(result)) } + if end.Reason != WalkException || end.VarBind.Value.Type() != EndOfMibView { + t.Fatalf("Expected the walk to end at EndOfMibView, got %v %v", end.Reason, end.VarBind) + } } func TestGenericBulkWalk(t *testing.T) { gs := connectToTarget(t, testConfig()) defer gs.Close() - result, err := gs.BulkWalkAll(context.Background(), "", 0) + result, end, err := gs.BulkWalkAll(context.Background(), "", 0) if err != nil { t.Fatalf("BulkWalkAll() Failed with error => %v", err) } if len(result) <= 1 { t.Fatalf("Expected multiple values, got %d", len(result)) } + if end.Reason != WalkException || end.VarBind.Value.Type() != EndOfMibView { + t.Fatalf("Expected the walk to end at EndOfMibView, got %v %v", end.Reason, end.VarBind) + } } // TestGenericBulkWalkMaxRepetitions walks the system group with the default, @@ -196,7 +202,7 @@ func TestGenericBulkWalkMaxRepetitions(t *testing.T) { walkNames := func(t *testing.T, maxRepetitions uint32) []string { t.Helper() - values, err := gs.BulkWalkAll(context.Background(), ".1.3.6.1.2.1.1", maxRepetitions) // SNMP MIB-2 system + values, _, err := gs.BulkWalkAll(context.Background(), ".1.3.6.1.2.1.1", maxRepetitions) // SNMP MIB-2 system if err != nil { t.Fatalf("BulkWalkAll() failed with error => %v", err) } @@ -226,12 +232,85 @@ func TestV1BulkWalkError(t *testing.T) { g.Close() - _, err := g.BulkWalkAll(context.Background(), "", 0) + _, _, err := g.BulkWalkAll(context.Background(), "", 0) if err == nil { t.Fatalf("BulkWalkAll() should fail in SNMPv1 but returned nil") } } +// TestGenericWalkEnd walks roots that end a walk in each way an agent ends +// one, with GETNEXT and GETBULK, and checks how each walk ended: the system +// group past its last object, a leaf scalar got with one Get, an instance the +// agent does not have (NoSuchInstance) and an object it does not have +// (NoSuchObject); and over SNMPv1, the noSuchName that ends the agent's MIB, +// and a root past it. +func TestGenericWalkEnd(t *testing.T) { + const ( + system = ".1.3.6.1.2.1.1" + sysName = ".1.3.6.1.2.1.1.5.0" + ) + type want struct { + reason WalkReason + name string // the end's varbind name, "" for any + typ ValueType // the end's varbind type + values int // -1 for more than one + outside bool // the end's varbind is outside the root + } + cases := map[string]struct { + version Version + root string + want want + }{ + "subtree": {version: Version2c, root: system, want: want{reason: WalkOutside, values: -1, outside: true}}, + "leaf": {version: Version2c, root: sysName, want: want{reason: WalkLeaf, name: sysName, typ: OctetString, values: 1}}, + "no-such-instance": {version: Version2c, root: ".1.3.6.1.2.1.1.5.1", want: want{reason: WalkLeaf, name: ".1.3.6.1.2.1.1.5.1", typ: NoSuchInstance}}, + "no-such-object": {version: Version2c, root: ".1.3.6.1.2.1.1.10", want: want{reason: WalkLeaf, name: ".1.3.6.1.2.1.1.10", typ: NoSuchObject}}, + "v1/subtree": {version: Version1, root: system, want: want{reason: WalkOutside, values: -1, outside: true}}, + "v1/whole-mib": {version: Version1, root: "", want: want{reason: WalkNoSuchName, values: -1}}, + // A root past the agent's last object: the walk gets it, which the + // agent answers with noSuchName. + "v1/past-the-mib": {version: Version1, root: ".1.3.6.1.6.3.99", want: want{reason: WalkLeaf}}, + } + for name, tc := range cases { + config := testConfig() + config.Version = tc.version + gs := connectToTarget(t, config) + walks := map[string]func() ([]VarBind, WalkEnd, error){ + "WalkAll": func() ([]VarBind, WalkEnd, error) { return gs.WalkAll(context.Background(), tc.root) }, + } + if tc.version != Version1 { + walks["BulkWalkAll"] = func() ([]VarBind, WalkEnd, error) { return gs.BulkWalkAll(context.Background(), tc.root, 0) } + } + for api, walk := range walks { + t.Run(name+"/"+api, func(t *testing.T) { + values, end, err := walk() + if err != nil { + t.Fatalf("walk: %v", err) + } + switch { + case tc.want.values < 0 && len(values) <= 1: + t.Errorf("values = %d, want more than one", len(values)) + case tc.want.values >= 0 && len(values) != tc.want.values: + t.Errorf("values = %v, want %d", values, tc.want.values) + } + if end.Reason != tc.want.reason { + t.Errorf("end = %v, want %v", end.Reason, tc.want.reason) + } + if tc.want.name != "" && end.VarBind.Name != tc.want.name { + t.Errorf("end varbind = %q, want %q", end.VarBind.Name, tc.want.name) + } + if tc.want.typ != UnknownType && end.VarBind.Value.Type() != tc.want.typ { + t.Errorf("end varbind type = %v, want %v", end.VarBind.Value.Type(), tc.want.typ) + } + if tc.want.outside && (end.VarBind.Name == "" || strings.HasPrefix(end.VarBind.Name, tc.root+".")) { + t.Errorf("end varbind %q is not outside %s", end.VarBind.Name, tc.root) + } + }) + } + gs.Close() + } +} + // Standard exception/error tests func TestMaxOids(t *testing.T) { @@ -337,7 +416,7 @@ func TestSnmpV3Users(t *testing.T) { t.Fatalf("Got a zero length sysDescr") } - values, err := gs.BulkWalkAll(context.Background(), ".1.3.6.1.2.1.1", 0) // SNMP MIB-2 system + values, _, err := gs.BulkWalkAll(context.Background(), ".1.3.6.1.2.1.1", 0) // SNMP MIB-2 system if err != nil { t.Fatalf("BulkWalkAll() failed with error => %v", err) } diff --git a/gosnmp_api_test.go b/gosnmp_api_test.go index a0df3c8..78a758c 100644 --- a/gosnmp_api_test.go +++ b/gosnmp_api_test.go @@ -45,11 +45,11 @@ func TestAPIGetMethodSignature(_ *testing.T) { } func TestAPIBulkWalkAllMethodSignature(_ *testing.T) { - var f func(context.Context, string, uint32) ([]gosnmp.VarBind, error) = (&gosnmp.Client{}).BulkWalkAll + var f func(context.Context, string, uint32) ([]gosnmp.VarBind, gosnmp.WalkEnd, error) = (&gosnmp.Client{}).BulkWalkAll _ = f } func TestAPIWalkAllMethodSignature(_ *testing.T) { - var f func(context.Context, string) ([]gosnmp.VarBind, error) = (&gosnmp.Client{}).WalkAll + var f func(context.Context, string) ([]gosnmp.VarBind, gosnmp.WalkEnd, error) = (&gosnmp.Client{}).WalkAll _ = f } diff --git a/request.go b/request.go index 6e2c999..5094708 100644 --- a/request.go +++ b/request.go @@ -33,9 +33,10 @@ func (x *Client) NewMessage(pduType PDUType, varBinds []VarBind, nonRepeaters ui } // Get sends a GET request for oids, at most MaxOids of them, within ctx, and -// returns the answer, a Response whatever its error-status; on an error it +// returns the answer, a Response whose error-status is NoError; on an error it // returns no message, also when the agent's answer ended the request (an -// SNMPv3 Report), whose error then matches ErrAnswered (see the package +// SNMPv3 Report, or a Response with another error-status, whose error is a +// *StatusError), whose error then matches ErrAnswered (see the package // documentation, Errors). A done ctx ends the request with its error (see // the package documentation, Attempts and timeouts); a nil ctx fails it // before it is sent. diff --git a/request_test.go b/request_test.go index 89c0143..40229ac 100644 --- a/request_test.go +++ b/request_test.go @@ -63,7 +63,7 @@ func TestWalkRequestChecks(t *testing.T) { x := mustClient(Config{Version: tc.version, MaxOids: tc.maxOids}) walk := x.WalkAll if tc.bulk { - walk = func(ctx context.Context, rootOid string) ([]VarBind, error) { + walk = func(ctx context.Context, rootOid string) ([]VarBind, WalkEnd, error) { return x.BulkWalkAll(ctx, rootOid, tc.maxRepetitions) } } @@ -71,9 +71,10 @@ func TestWalkRequestChecks(t *testing.T) { if tc.nilContext { ctx = nil } - values, err := walk(ctx, tc.root) + values, end, err := walk(ctx, tc.root) assert.EqualError(t, err, tc.wantErr) assert.Empty(t, values) + assert.Equal(t, WalkEnd{}, end) }) } } diff --git a/testdata/engine/requests.golden b/testdata/engine/requests.golden index 655ef1b..27cd840 100644 --- a/testdata/engine/requests.golden +++ b/testdata/engine/requests.golden @@ -82,7 +82,52 @@ 0s write #1: 2c "public" GetRequest id=1001 [.1.3.6.1.2.1.1.1.0] 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 error=NoSuchName index=1 [.1.3.6.1.2.1.1.1.0] -0s result: packet 2c "public" Response id=1001 error=NoSuchName index=1 [.1.3.6.1.2.1.1.1.0], no error +0s result: packet nil, error "the agent answered with error-status NoSuchName, error-index 1" *gosnmp.StatusError (is ErrAnswered) +0s client after: next request id=1002 + +== requests/answer/error-status/gen-err +0s client: 2c timeout=1s retries=2 +0s deadline +1s +0s write #1: 2c "public" GetRequest id=1001 [.1.3.6.1.2.1.1.1.0] +0s agent answers #1 after 0s +0s read from 127.0.0.2:161: 2c "public" Response id=1001 error=GenErr index=1 [.1.3.6.1.2.1.1.1.0] +0s result: packet nil, error "the agent answered with error-status GenErr, error-index 1" *gosnmp.StatusError (is ErrAnswered) +0s client after: next request id=1002 + +== requests/answer/error-status/index-past +0s client: 2c timeout=1s retries=2 +0s deadline +1s +0s write #1: 2c "public" GetRequest id=1001 [.1.3.6.1.2.1.1.1.0] +0s agent answers #1 after 0s +0s read from 127.0.0.2:161: 2c "public" Response id=1001 error=NoAccess index=5 [.1.3.6.1.2.1.1.1.0] +0s result: packet nil, error "the agent answered with error-status NoAccess, error-index 5" *gosnmp.StatusError (is ErrAnswered) +0s client after: next request id=1002 + +== requests/answer/error-status/too-big +0s client: 2c timeout=1s retries=2 +0s deadline +1s +0s write #1: 2c "public" GetRequest id=1001 [.1.3.6.1.2.1.1.1.0] +0s agent answers #1 after 0s +0s read from 127.0.0.2:161: 2c "public" Response id=1001 error=TooBig index=0 [] +0s result: packet nil, error "the agent answered with error-status TooBig, error-index 0" *gosnmp.StatusError (is ErrAnswered) +0s client after: next request id=1002 + +== requests/answer/error-status/unknown +0s client: 2c timeout=1s retries=2 +0s deadline +1s +0s write #1: 2c "public" GetRequest id=1001 [.1.3.6.1.2.1.1.1.0] +0s agent answers #1 after 0s +0s read from 127.0.0.2:161: 2c "public" Response id=1001 error=ErrorStatus(19) index=1 [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] +0s result: packet nil, error "the agent answered with error-status ErrorStatus(19), error-index 1" *gosnmp.StatusError (is ErrAnswered) +0s client after: next request id=1002 + +== requests/answer/error-status/v1-no-such-name +0s client: 1 timeout=1s retries=2 +0s deadline +1s +0s write #1: 1 "public" GetRequest id=1001 [.1.3.6.1.2.1.1.1.0] +0s agent answers #1 after 0s +0s read from 127.0.0.2:161: 1 "public" Response id=1001 error=NoSuchName index=1 [.1.3.6.1.2.1.1.1.0] +0s result: packet nil, error "the agent answered with error-status NoSuchName, error-index 1" *gosnmp.StatusError (is ErrAnswered) 0s client after: next request id=1002 == requests/answer/first-result-after-second-get diff --git a/testdata/engine/v3.golden b/testdata/engine/v3.golden index 91ac3d6..711b862 100644 --- a/testdata/engine/v3.golden +++ b/testdata/engine/v3.golden @@ -177,6 +177,22 @@ 1s result: packet 3 msgID=2003 authNoPriv Response id=1003 context="\x80\x00\x1f\x88\x04codec-agent"/"" usm="codec-md5"/"\x80\x00\x1f\x88\x04codec-agent"/7/1001 [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"], no error 1s client after: engine="\x80\x00\x1f\x88\x04codec-agent" boots=7 time=1001 keys=current context engine="\x80\x00\x1f\x88\x04codec-agent" +== v3/answer/error-status +0s client: user="codec-sha-aes" authPriv timeout=1s retries=2 +0s deadline +1s +0s write #1: 3 msgID=2001 noAuthNoPriv+reportable user="" engine="" boots=0 time=0 context=""/"" GetRequest id=1001 [] +0s agent: answers #1 with Report id=1001 msgID=2001 [.1.3.6.1.6.3.15.1.1.4.0=Counter32:1] at noAuthNoPriv (unknown engine ID) +0s agent answers #1 after 0s +0s read from 127.0.0.2:161: 3 msgID=2001 noAuthNoPriv user="" engine="\x80\x00\x1f\x88\x04codec-agent" boots=7 time=1000 context="\x80\x00\x1f\x88\x04codec-agent"/"" Report id=1001 [.1.3.6.1.6.3.15.1.1.4.0=Counter32:1] +0s deadline +1s +0s write #2: 3 msgID=2002 authPriv+reportable user="codec-sha-aes" engine="\x80\x00\x1f\x88\x04codec-agent" boots=7 time=1000 salt=0000000000000101 encrypted +0s agent: #2 decrypts to context="\x80\x00\x1f\x88\x04codec-agent"/"" GetRequest id=1002 [.1.3.6.1.2.1.1.1.0] +0s agent: answers #2 with Response id=1002 msgID=2002 [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] at authPriv (answers the request with noAccess) +0s agent answers #2 after 0s +0s read from 127.0.0.2:161: 3 msgID=2002 authPriv user="codec-sha-aes" engine="\x80\x00\x1f\x88\x04codec-agent" boots=7 time=1000 salt=0000000000000102 encrypted +0s result: packet nil, error "the agent answered with error-status NoAccess, error-index 1" *gosnmp.StatusError (is ErrAnswered) +0s client after: engine="\x80\x00\x1f\x88\x04codec-agent" boots=7 time=1000 keys=current context engine="\x80\x00\x1f\x88\x04codec-agent" + == v3/answer/forged-report/empty-identity 0s client: user="codec-md5" authNoPriv timeout=1s retries=2 0s deadline +1s @@ -3457,6 +3473,22 @@ 0s result: packet nil, error "wrong digest" *gosnmp.categorized (is ErrAnswered, ErrWrongDigest) 0s client after: engine="\x80\x00\x1f\x88\x04codec-agent" boots=7 time=1000 keys=none context engine="\x80\x00\x1f\x88\x04codec-agent" +== v3/report/with-error-status +0s client: user="codec-sha-aes" authPriv timeout=1s retries=2 +0s deadline +1s +0s write #1: 3 msgID=2001 noAuthNoPriv+reportable user="" engine="" boots=0 time=0 context=""/"" GetRequest id=1001 [] +0s agent: answers #1 with Report id=1001 msgID=2001 [.1.3.6.1.6.3.15.1.1.4.0=Counter32:1] at noAuthNoPriv (unknown engine ID) +0s agent answers #1 after 0s +0s read from 127.0.0.2:161: 3 msgID=2001 noAuthNoPriv user="" engine="\x80\x00\x1f\x88\x04codec-agent" boots=7 time=1000 context="\x80\x00\x1f\x88\x04codec-agent"/"" Report id=1001 [.1.3.6.1.6.3.15.1.1.4.0=Counter32:1] +0s deadline +1s +0s write #2: 3 msgID=2002 authPriv+reportable user="codec-sha-aes" engine="\x80\x00\x1f\x88\x04codec-agent" boots=7 time=1000 salt=0000000000000101 encrypted +0s agent: #2 decrypts to context="\x80\x00\x1f\x88\x04codec-agent"/"" GetRequest id=1002 [.1.3.6.1.2.1.1.1.0] +0s agent: answers #2 with Report id=1002 msgID=2002 [.1.3.6.1.6.3.15.1.1.3.0=Counter32:1] at noAuthNoPriv (rejects the request with a Report carrying genErr) +0s agent answers #2 after 0s +0s read from 127.0.0.2:161: 3 msgID=2002 noAuthNoPriv user="codec-sha-aes" engine="\x80\x00\x1f\x88\x04codec-agent" boots=7 time=1000 context="\x80\x00\x1f\x88\x04codec-agent"/"" Report id=1002 [.1.3.6.1.6.3.15.1.1.3.0=Counter32:1] +0s result: packet nil, error "unknown username" *gosnmp.categorized (is ErrAnswered, ErrUnknownUsername) +0s client after: engine="\x80\x00\x1f\x88\x04codec-agent" boots=7 time=1000 keys=current context engine="\x80\x00\x1f\x88\x04codec-agent" + == v3/resync/panic-in-retransmission 0s client: user="codec-md5" authNoPriv timeout=1s retries=2 0s deadline +1s diff --git a/testdata/engine/walk.golden b/testdata/engine/walk.golden index 3e56301..c683ea9 100644 --- a/testdata/engine/walk.golden +++ b/testdata/engine/walk.golden @@ -11,6 +11,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.2.2.1.2=NoSuchInstance:] 0s values: [] +0s end: Leaf [.1.3.6.1.2.1.2.2.1.2=NoSuchInstance:] 0s result: no error == walk/below-root/later @@ -24,11 +25,13 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] 0s values: [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] +0s end: Outside [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] 0s result: no error == walk/context/canceled-before 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 0s values: [] +0s end: none 0s result: error "context canceled" *errors.errorString (equals context.Canceled) == walk/context/canceled-in-leaf-get @@ -43,6 +46,7 @@ 300ms deadline in the past 300ms read from 127.0.0.2:161: deadline 300ms values: [] +300ms end: none 300ms result: error "context canceled" *errors.errorString (equals context.Canceled) == walk/context/canceled-mid-walk @@ -57,6 +61,7 @@ 300ms deadline in the past 300ms read from 127.0.0.2:161: deadline 300ms values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] +300ms end: none 300ms result: error "context canceled" *errors.errorString (equals context.Canceled) == walk/context/canceled-mid-walk/bulk @@ -71,6 +76,7 @@ 300ms deadline in the past 300ms read from 127.0.0.2:161: deadline 300ms values: [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] +300ms end: none 300ms result: error "context canceled" *errors.errorString (equals context.Canceled) == walk/context/deadline-mid-walk @@ -88,16 +94,19 @@ 1s agent: drops #3 1.5s read from 127.0.0.2:161: deadline 1.5s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] +1.5s end: none 1.5s result: error "context deadline exceeded" context.deadlineExceededError (equals context.DeadlineExceeded; is net.Error timeout) == walk/context/nil 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 0s values: [] +0s end: none 0s result: error "nil context" *errors.errorString == walk/context/nil/bulk 0s client: 2c root=".1.3.6.1.2.1.1" bulk=true max-repetitions=0 0s values: [] +0s end: none 0s result: error "nil context" *errors.errorString == walk/default-root @@ -135,6 +144,7 @@ 0s agent answers #8 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1008 [.1.3.6.1.4.1.99.1.0=EndOfMibView:] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1.3.0=TimeTicks:4200 .1.3.6.1.2.1.1.5.0=OctetString:"codec-host" .1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1" .1.3.6.1.4.1.99.1.0=Integer:7] +0s end: Exception [.1.3.6.1.4.1.99.1.0=EndOfMibView:] 0s result: no error == walk/default-root/dot @@ -144,6 +154,7 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1.3.0=TimeTicks:4200 .1.3.6.1.2.1.1.5.0=OctetString:"codec-host" .1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1" .1.3.6.1.4.1.99.1.0=Integer:7 .1.3.6.1.4.1.99.1.0=EndOfMibView:] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1.3.0=TimeTicks:4200 .1.3.6.1.2.1.1.5.0=OctetString:"codec-host" .1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1" .1.3.6.1.4.1.99.1.0=Integer:7] +0s end: Exception [.1.3.6.1.4.1.99.1.0=EndOfMibView:] 0s result: no error == walk/default-root/v1 @@ -181,6 +192,7 @@ 0s agent answers #8 after 0s 0s read from 127.0.0.2:161: 1 "public" Response id=1008 error=NoSuchName index=1 [.1.3.6.1.4.1.99.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1.3.0=TimeTicks:4200 .1.3.6.1.2.1.1.5.0=OctetString:"codec-host" .1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1" .1.3.6.1.4.1.99.1.0=Integer:7] +0s end: NoSuchName 0s result: no error == walk/empty-response @@ -194,8 +206,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] +0s end: Empty 0s result: no error -0s known bug: an empty response ends the walk with a nil error == walk/empty-response/bulk 0s client: 2c root=".1.3.6.1.2.1.1" bulk=true max-repetitions=0 @@ -204,8 +216,8 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 [] 0s values: [] +0s end: Empty 0s result: no error -0s known bug: an empty response ends the walk with a nil error == walk/empty-subtree 0s client: 2c root=".1.3.6.1.2.1.1.4" bulk=false max-repetitions=0 @@ -218,6 +230,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.4=NoSuchInstance:] 0s values: [] +0s end: Leaf [.1.3.6.1.2.1.1.4=NoSuchInstance:] 0s result: no error == walk/empty-subtree/bulk @@ -231,6 +244,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.4=NoSuchInstance:] 0s values: [] +0s end: Leaf [.1.3.6.1.2.1.1.4=NoSuchInstance:] 0s result: no error == walk/error-status/1-TooBig @@ -244,8 +258,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=TooBig index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status TooBig, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/10-WrongValue 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -258,8 +272,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=WrongValue index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status WrongValue, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/11-NoCreation 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -272,8 +286,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=NoCreation index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status NoCreation, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/12-InconsistentValue 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -286,8 +300,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=InconsistentValue index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status InconsistentValue, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/13-ResourceUnavailable 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -300,8 +314,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=ResourceUnavailable index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status ResourceUnavailable, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/14-CommitFailed 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -314,8 +328,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=CommitFailed index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status CommitFailed, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/15-UndoFailed 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -328,8 +342,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=UndoFailed index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status UndoFailed, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/16-AuthorizationError 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -342,8 +356,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=AuthorizationError index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status AuthorizationError, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/17-NotWritable 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -356,8 +370,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=NotWritable index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status NotWritable, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/18-InconsistentName 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -370,8 +384,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=InconsistentName index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status InconsistentName, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/19-unknown 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -384,8 +398,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=ErrorStatus(19) index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: error "OID not increasing: .1.3.6.1.2.1.1.1.0 >= .1.3.6.1.2.1.1.1.0" *errors.errorString -0s known bug: an error status the walk does not name is ignored and the varbinds walked +0s end: none +0s result: error "the agent answered with error-status ErrorStatus(19), error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/19-unknown-empty 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -398,8 +412,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=ErrorStatus(19) index=0 [] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an empty response ends the walk with a nil error +0s end: none +0s result: error "the agent answered with error-status ErrorStatus(19), error-index 0" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/2-NoSuchName 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -412,6 +426,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=NoSuchName index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] +0s end: NoSuchName 0s result: no error == walk/error-status/3-BadValue @@ -425,8 +440,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=BadValue index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status BadValue, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/4-ReadOnly 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -439,8 +454,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=ReadOnly index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status ReadOnly, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/5-GenErr 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -453,8 +468,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=GenErr index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status GenErr, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/6-NoAccess 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -467,8 +482,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=NoAccess index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status NoAccess, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/7-WrongType 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -481,8 +496,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=WrongType index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status WrongType, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/8-WrongLength 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -495,8 +510,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=WrongLength index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status WrongLength, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/9-WrongEncoding 0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 @@ -509,8 +524,8 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=WrongEncoding index=1 [.1.3.6.1.2.1.1.1.0] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status WrongEncoding, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/bulk-too-big 0s client: 2c root=".1.3.6.1.2.1.1" bulk=true max-repetitions=0 @@ -519,8 +534,32 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 error=TooBig index=1 [.1.3.6.1.2.1.1] 0s values: [] -0s result: no error -0s known bug: an error status ends the walk with a nil error and the values so far +0s end: none +0s result: error "the agent answered with error-status TooBig, error-index 1" *gosnmp.StatusError (is ErrAnswered) + +== walk/error-status/bulk-too-big-later +0s client: 2c root=".1.3.6.1.2.1.2.2.1.2" bulk=true max-repetitions=1 +0s deadline +1s +0s write #1: 2c "public" GetBulkRequest id=1001 nonRepeaters=0 maxRepetitions=1 [.1.3.6.1.2.1.2.2.1.2] +0s agent answers #1 after 0s +0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] +0s deadline +1s +0s write #2: 2c "public" GetBulkRequest id=1002 nonRepeaters=0 maxRepetitions=1 [.1.3.6.1.2.1.2.2.1.2.1] +0s agent answers #2 after 0s +0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=TooBig index=1 [.1.3.6.1.2.1.2.2.1.2.1] +0s values: [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] +0s end: none +0s result: error "the agent answered with error-status TooBig, error-index 1" *gosnmp.StatusError (is ErrAnswered) + +== walk/error-status/first-answer-gen-err +0s client: 2c root=".1.3.6.1.2.1.1" bulk=false max-repetitions=0 +0s deadline +1s +0s write #1: 2c "public" GetNextRequest id=1001 [.1.3.6.1.2.1.1] +0s agent answers #1 after 0s +0s read from 127.0.0.2:161: 2c "public" Response id=1001 error=GenErr index=1 [.1.3.6.1.2.1.1] +0s values: [] +0s end: none +0s result: error "the agent answered with error-status GenErr, error-index 1" *gosnmp.StatusError (is ErrAnswered) == walk/error-status/no-such-name-empty-first 0s client: 2c root=".1.3.6.1.2.1.1.5.0" bulk=false max-repetitions=0 @@ -533,6 +572,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s values: [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] +0s end: Leaf [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s result: no error == walk/error-status/no-such-name-first @@ -546,6 +586,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s values: [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] +0s end: Leaf [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s result: no error == walk/exception/no-such-instance-bulk @@ -555,6 +596,7 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1.3.0=NoSuchInstance: .1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] +0s end: Exception [.1.3.6.1.2.1.1.3.0=NoSuchInstance:] 0s result: no error == walk/exception/no-such-object @@ -568,6 +610,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.3.0=NoSuchObject:] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] +0s end: Exception [.1.3.6.1.2.1.1.3.0=NoSuchObject:] 0s result: no error == walk/exception/outside-first-value @@ -581,6 +624,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s values: [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] +0s end: Leaf [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s result: no error == walk/leaf @@ -594,6 +638,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s values: [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] +0s end: Leaf [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s result: no error == walk/leaf/bulk @@ -607,6 +652,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s values: [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] +0s end: Leaf [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s result: no error == walk/leaf/get-answer-in-subtree @@ -620,6 +666,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.5.0.1=OctetString:"inside"] 0s values: [] +0s end: Leaf [.1.3.6.1.2.1.1.5.0.1=OctetString:"inside"] 0s result: no error == walk/leaf/get-empty @@ -633,6 +680,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [] 0s values: [] +0s end: Leaf 0s result: no error == walk/leaf/get-error @@ -654,6 +702,7 @@ 2s agent: drops #4 3s read from 127.0.0.2:161: deadline 3s values: [] +3s end: none 3s result: error "request timeout (after 2 retries)" *gosnmp.attemptsError (is os.ErrDeadlineExceeded, net.Error timeout) == walk/leaf/get-error-status @@ -667,6 +716,21 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=GenErr index=1 [.1.3.6.1.2.1.1.5.0] 0s values: [] +0s end: Leaf +0s result: no error + +== walk/leaf/get-no-such-name +0s client: 2c root=".1.3.6.1.2.1.1.5.0" bulk=false max-repetitions=0 +0s deadline +1s +0s write #1: 2c "public" GetNextRequest id=1001 [.1.3.6.1.2.1.1.5.0] +0s agent answers #1 after 0s +0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] +0s deadline +1s +0s write #2: 2c "public" GetRequest id=1002 [.1.3.6.1.2.1.1.5.0] +0s agent answers #2 after 0s +0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=NoSuchName index=1 [.1.3.6.1.2.1.1.5.0] +0s values: [] +0s end: Leaf 0s result: no error == walk/leaf/get-other-name-first @@ -680,6 +744,21 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s values: [] +0s end: Leaf [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] +0s result: no error + +== walk/leaf/get-too-big +0s client: 2c root=".1.3.6.1.2.1.1.5.0" bulk=false max-repetitions=0 +0s deadline +1s +0s write #1: 2c "public" GetNextRequest id=1001 [.1.3.6.1.2.1.1.5.0] +0s agent answers #1 after 0s +0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] +0s deadline +1s +0s write #2: 2c "public" GetRequest id=1002 [.1.3.6.1.2.1.1.5.0] +0s agent answers #2 after 0s +0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=TooBig index=1 [.1.3.6.1.2.1.1.5.0] +0s values: [] +0s end: Leaf 0s result: no error == walk/leaf/get-unknown-status @@ -692,9 +771,9 @@ 0s write #2: 2c "public" GetRequest id=1002 [.1.3.6.1.2.1.1.5.0] 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 error=ErrorStatus(19) index=0 [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] -0s values: [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] +0s values: [] +0s end: Leaf 0s result: no error -0s known bug: an error status the walk does not name is ignored and the root's value passed on == walk/leaf/last-object 0s client: 2c root=".1.3.6.1.4.1.99.1.0" bulk=false max-repetitions=0 @@ -703,6 +782,7 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.4.1.99.1.0=EndOfMibView:] 0s values: [] +0s end: Exception [.1.3.6.1.4.1.99.1.0=EndOfMibView:] 0s result: no error == walk/leaf/last-object-v1 @@ -716,6 +796,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 1 "public" Response id=1002 [.1.3.6.1.4.1.99.1.0=Integer:7] 0s values: [.1.3.6.1.4.1.99.1.0=Integer:7] +0s end: Leaf [.1.3.6.1.4.1.99.1.0=Integer:7] 0s result: no error == walk/leaf/no-such-instance @@ -729,6 +810,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.5.1=NoSuchInstance:] 0s values: [] +0s end: Leaf [.1.3.6.1.2.1.1.5.1=NoSuchInstance:] 0s result: no error == walk/leaf/no-such-object @@ -742,6 +824,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.7.0=NoSuchObject:] 0s values: [] +0s end: Leaf [.1.3.6.1.2.1.1.7.0=NoSuchObject:] 0s result: no error == walk/leaf/root-echo @@ -751,6 +834,7 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.2.1.1=Integer:1] 0s values: [] +0s end: none 0s result: error "OID not increasing: .1.3.6.1.2.1.1 >= .1.3.6.1.2.1.1" *errors.errorString == walk/leaf/v1 @@ -764,6 +848,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 1 "public" Response id=1002 [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s values: [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] +0s end: Leaf [.1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] 0s result: no error == walk/leaf/v1-no-such-name @@ -777,6 +862,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 1 "public" Response id=1002 error=NoSuchName index=1 [.1.3.6.1.2.1.1.5.1] 0s values: [] +0s end: Leaf 0s result: no error == walk/max-repetitions/max-int32 @@ -786,11 +872,13 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1" .1.3.6.1.4.1.99.1.0=Integer:7 .1.3.6.1.4.1.99.1.0=EndOfMibView:] 0s values: [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1"] +0s end: Outside [.1.3.6.1.4.1.99.1.0=Integer:7] 0s result: no error == walk/max-repetitions/max-uint32 0s client: 2c root=".1.3.6.1.2.1.2.2.1.2" bulk=true max-repetitions=4294967295 0s values: [] +0s end: none 0s result: error "max-repetitions 4294967295 is more than 2147483647" *errors.errorString == walk/max-repetitions/one @@ -812,11 +900,13 @@ 0s agent answers #4 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1004 [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1.3.0=TimeTicks:4200 .1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] +0s end: Outside [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] 0s result: no error == walk/max-repetitions/top-bit 0s client: 2c root=".1.3.6.1.2.1.2.2.1.2" bulk=true max-repetitions=2147483648 0s values: [] +0s end: none 0s result: error "max-repetitions 2147483648 is more than 2147483647" *errors.errorString == walk/max-repetitions/two @@ -830,6 +920,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1" .1.3.6.1.4.1.99.1.0=Integer:7] 0s values: [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1"] +0s end: Outside [.1.3.6.1.4.1.99.1.0=Integer:7] 0s result: no error == walk/not-increasing/bulk-inside-response @@ -843,6 +934,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.4.1.99.1.0=Integer:7 .1.3.6.1.4.1.99.1.0=EndOfMibView:] 0s values: [.1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1"] +0s end: Outside [.1.3.6.1.4.1.99.1.0=Integer:7] 0s result: no error == walk/not-increasing/decreasing @@ -860,6 +952,7 @@ 0s agent answers #3 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1003 [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] 0s values: [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0"] +0s end: none 0s result: error "OID not increasing: .1.3.6.1.2.1.2.2.1.2.2 >= .1.3.6.1.2.1.2.2.1.2.1" *errors.errorString == walk/not-increasing/equal @@ -873,6 +966,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] +0s end: none 0s result: error "OID not increasing: .1.3.6.1.2.1.1.1.0 >= .1.3.6.1.2.1.1.1.0" *errors.errorString == walk/request-error @@ -898,6 +992,7 @@ 2s agent: drops #5 3s read from 127.0.0.2:161: deadline 3s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1.3.0=TimeTicks:4200] +3s end: none 3s result: error "request timeout (after 2 retries)" *gosnmp.attemptsError (is os.ErrDeadlineExceeded, net.Error timeout) == walk/request-error/bulk @@ -923,6 +1018,7 @@ 2s agent: drops #5 3s read from 127.0.0.2:161: deadline 3s values: [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0"] +3s end: none 3s result: error "request timeout (after 2 retries)" *gosnmp.attemptsError (is os.ErrDeadlineExceeded, net.Error timeout) == walk/root-in-bulk-response @@ -932,6 +1028,7 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1=Integer:1 .1.3.6.1.2.1.1.3.0=TimeTicks:4200] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] +0s end: none 0s result: error "OID not increasing: .1.3.6.1.2.1.1 >= .1.3.6.1.2.1.1" *errors.errorString == walk/root-in-response @@ -945,11 +1042,13 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1002 [.1.3.6.1.2.1.1=Integer:1] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent"] +0s end: none 0s result: error "OID not increasing: .1.3.6.1.2.1.1.1.0 >= .1.3.6.1.2.1.1" *errors.errorString == walk/root-invalid 0s client: 2c root="1.3.6.x" bulk=false max-repetitions=0 0s values: [] +0s end: none 0s result: error "unable to marshal OID \"1.3.6.x\": invalid object identifier" *fmt.wrapError == walk/root-past-mib @@ -959,6 +1058,7 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.4.1.99.2=EndOfMibView:] 0s values: [] +0s end: Exception [.1.3.6.1.4.1.99.2=EndOfMibView:] 0s result: no error == walk/root-past-mib/bulk @@ -968,6 +1068,7 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.4.1.99.2=EndOfMibView:] 0s values: [] +0s end: Exception [.1.3.6.1.4.1.99.2=EndOfMibView:] 0s result: no error == walk/root-past-mib/v1 @@ -981,6 +1082,7 @@ 0s agent answers #2 after 0s 0s read from 127.0.0.2:161: 1 "public" Response id=1002 error=NoSuchName index=1 [.1.3.6.1.4.1.99.2] 0s values: [] +0s end: Leaf 0s result: no error == walk/root-without-dot @@ -1002,6 +1104,7 @@ 0s agent answers #4 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1004 [.1.3.6.1.4.1.99.1.0=Integer:7] 0s values: [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1"] +0s end: Outside [.1.3.6.1.4.1.99.1.0=Integer:7] 0s result: no error == walk/root-without-dot/bulk @@ -1011,6 +1114,7 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1" .1.3.6.1.4.1.99.1.0=Integer:7 .1.3.6.1.4.1.99.1.0=EndOfMibView:] 0s values: [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1"] +0s end: Outside [.1.3.6.1.4.1.99.1.0=Integer:7] 0s result: no error == walk/sibling-arc @@ -1032,6 +1136,7 @@ 0s agent answers #4 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1004 [.1.3.6.1.2.1.2.2.1.20.1=OctetString:"sibling"] 0s values: [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1"] +0s end: Outside [.1.3.6.1.2.1.2.2.1.20.1=OctetString:"sibling"] 0s result: no error == walk/subtree @@ -1053,6 +1158,7 @@ 0s agent answers #4 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1004 [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1.3.0=TimeTicks:4200 .1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] +0s end: Outside [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] 0s result: no error == walk/subtree/bulk @@ -1062,6 +1168,7 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1.3.0=TimeTicks:4200 .1.3.6.1.2.1.1.5.0=OctetString:"codec-host" .1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1" .1.3.6.1.4.1.99.1.0=Integer:7 .1.3.6.1.4.1.99.1.0=EndOfMibView:] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1.3.0=TimeTicks:4200 .1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] +0s end: Outside [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] 0s result: no error == walk/subtree/v1 @@ -1083,6 +1190,7 @@ 0s agent answers #4 after 0s 0s read from 127.0.0.2:161: 1 "public" Response id=1004 [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] 0s values: [.1.3.6.1.2.1.1.1.0=OctetString:"codec agent" .1.3.6.1.2.1.1.3.0=TimeTicks:4200 .1.3.6.1.2.1.1.5.0=OctetString:"codec-host"] +0s end: Outside [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo"] 0s result: no error == walk/table-column @@ -1104,6 +1212,7 @@ 0s agent answers #4 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1004 [.1.3.6.1.4.1.99.1.0=Integer:7] 0s values: [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1"] +0s end: Outside [.1.3.6.1.4.1.99.1.0=Integer:7] 0s result: no error == walk/table-column/bulk @@ -1113,9 +1222,11 @@ 0s agent answers #1 after 0s 0s read from 127.0.0.2:161: 2c "public" Response id=1001 [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1" .1.3.6.1.4.1.99.1.0=Integer:7 .1.3.6.1.4.1.99.1.0=EndOfMibView:] 0s values: [.1.3.6.1.2.1.2.2.1.2.1=OctetString:"lo" .1.3.6.1.2.1.2.2.1.2.2=OctetString:"eth0" .1.3.6.1.2.1.2.2.1.2.10=OctetString:"eth1"] +0s end: Outside [.1.3.6.1.4.1.99.1.0=Integer:7] 0s result: no error == walk/v1-bulk 0s client: 1 root=".1.3.6.1.2.1.1" bulk=true max-repetitions=0 0s values: [] +0s end: none 0s result: error "GETBULK not supported in SNMPv1" *errors.errorString diff --git a/transport_test.go b/transport_test.go index 272be31..d4e2457 100644 --- a/transport_test.go +++ b/transport_test.go @@ -529,7 +529,7 @@ func TestCloseDuringWalk(t *testing.T) { } }) start := time.Now() - _, err := x.BulkWalkAll(context.Background(), root, 0) + _, _, err := x.BulkWalkAll(context.Background(), root, 0) require.ErrorIs(t, err, net.ErrClosed) assert.Less(t, time.Since(start), 5*time.Second, "the walk ends before its request's timeout") closers.Wait() diff --git a/walk.go b/walk.go index e5e2809..818df94 100644 --- a/walk.go +++ b/walk.go @@ -21,61 +21,109 @@ import ( // BulkWalkAll walks the subtree under rootOid with GETBULK requests (SNMPv2c // and SNMPv3: a request on SNMPv1 fails before it is sent) and returns its -// values, in the order the agent returns them. An empty or "." rootOid walks -// .1.3.6.1. The subtree is rootOid and the names under it. The walk ends as -// net-snmp's snmpbulkwalk ends its walk: at the first name outside the -// subtree or exception value (NoSuchObject, NoSuchInstance, EndOfMibView), -// and at an answer whose Error is NoSuchName, the end of an SNMPv1 MIB, -// without an error. When the walk's first answer ends it with NoSuchName or -// with a first name outside the subtree, rootOid is taken for a leaf and -// fetched with one Get, whose first value is returned when it names rootOid -// and is not an exception value. An exception value in the subtree ends the -// walk without that Get, also in the first answer: a leaf rootOid that is the -// agent's last object answers EndOfMibView and is not fetched. The walk ends -// at once with the error of a failed request, and with an error for a name in -// the subtree, rootOid included, that is not after the requested one (each -// name of an answer is compared with the requested one); it returns the values -// before the error with it. ctx bounds the walk, each of its requests and the -// Get of a leaf root: a done ctx ends the walk with its error and the values -// before it, and a nil ctx fails it before its first request is sent. -// maxRepetitions is the max-repetitions of each GETBULK request, the most -// objects the agent returns after the one name the request carries: 0 sends -// 50, and a value above 2147483647, the most RFC 3416 allows, fails the walk -// before its first request is sent. Some devices fail with large values; see -// https://github.com/gosnmp/gosnmp/issues/100. Known bugs: an empty answer, or -// one whose Error is another of TooBig through InconsistentName, ends the walk -// without an error; another Error is ignored. -func (x *Client) BulkWalkAll(ctx context.Context, rootOid string, maxRepetitions uint32) (results []VarBind, err error) { +// values, in the order the agent returns them, and how the walk ended. An +// empty or "." rootOid walks .1.3.6.1. The subtree is rootOid and the names +// under it. The walk ends as net-snmp's snmpbulkwalk ends its walk: at the +// first name outside the subtree or exception value (NoSuchObject, +// NoSuchInstance, EndOfMibView), and at an answer whose error-status is +// NoSuchName, the end of an SNMPv1 MIB; it also ends at an answer without +// varbinds, which net-snmp sends again. When the walk's first answer ends it +// with NoSuchName or with a first name outside the subtree, rootOid is taken +// for a leaf and fetched with one Get, whose first value is returned when it +// names rootOid and is not an exception value; that Get answered with an +// error-status means rootOid has no value, as in net-snmp. An exception value +// in the subtree ends the walk without that Get, also in the first answer: a +// leaf rootOid that is the agent's last object answers EndOfMibView and is not +// fetched. WalkEnd says which of these ended the walk, with the varbind that +// did. The walk ends at once with an error, which it returns with the values +// before it and a zero WalkEnd: the error of a failed request; a *StatusError +// for an answer with another error-status than NoSuchName, which net-snmp's +// walk fails with too (it also gets rootOid when it printed nothing); and an +// error for a name in the subtree, rootOid included, that is not after the +// requested one (each name of an answer is compared with the requested one). +// It also differs from net-snmp where a failed Get of a leaf rootOid (a +// timeout, a Report) fails the walk, which net-snmp ignores, and where the +// walk stops at the first value that ends it, while net-snmp goes on printing +// the values of the same answer that lie in the subtree. +// ctx bounds the walk, each of its requests and the Get of a leaf root: a done +// ctx ends the walk with its error and the values before it, and a nil ctx +// fails it before its first request is sent. maxRepetitions is the +// max-repetitions of each GETBULK request, the most objects the agent returns +// after the one name the request carries: 0 sends 50, and a value above +// 2147483647, the most RFC 3416 allows, fails the walk before its first +// request is sent. Some devices fail with large values; see +// https://github.com/gosnmp/gosnmp/issues/100. +func (x *Client) BulkWalkAll(ctx context.Context, rootOid string, maxRepetitions uint32) (results []VarBind, end WalkEnd, err error) { return x.walk(ctx, GetBulkRequest, rootOid, maxRepetitions) } // WalkAll walks the subtree under rootOid with GETNEXT requests, one per -// value, where BulkWalkAll asks for many at a time, and returns its values. It -// walks SNMPv1 agents too, and otherwise starts, ends and fails as BulkWalkAll -// does; net-snmp's snmpwalk differs only for a name before rootOid, which fails -// its increasing check where WalkAll ends (or, as the first answer, gets -// rootOid). -func (x *Client) WalkAll(ctx context.Context, rootOid string) (results []VarBind, err error) { +// value, where BulkWalkAll asks for many at a time, and returns its values and +// how the walk ended. It walks SNMPv1 agents too, and otherwise starts, ends +// and fails as BulkWalkAll does; net-snmp's snmpwalk differs only for a name +// before rootOid, which fails its increasing check where WalkAll ends (or, as +// the first answer, gets rootOid). +func (x *Client) WalkAll(ctx context.Context, rootOid string) (results []VarBind, end WalkEnd, err error) { return x.walk(ctx, GetNextRequest, rootOid, 0) } -// walk returns the values of the subtree under rootOid: it asks the agent for -// the objects after the root, then after the last name it received, with -// requestType (GetNext or GetBulk), and collects the objects in the subtree -// until an answer ends the walk (visit's rule). When that answer shows the -// root to be a leaf, the walk gets it once (getRoot). maxRepetitions is the -// GetBulk's max-repetitions, zero for the default; it is checked after the -// root, before the first request. Each request runs within ctx. An error ends -// the walk with the values collected before it. -func (x *Client) walk(ctx context.Context, requestType PDUType, rootOid string, maxRepetitions uint32) (results []VarBind, err error) { +// WalkEnd is how a walk ended without an error: what ended it, and the +// varbind that did when there is one. A walk that fails returns the zero +// WalkEnd. +type WalkEnd struct { + // Reason is what ended the walk. + Reason WalkReason + // VarBind is the varbind that ended the walk: for WalkOutside, the name + // outside the subtree; for WalkException, the exception value; for + // WalkLeaf, the first varbind of the Get's answer when the answer has one + // and no error-status (rootOid's value, also the walk's last value; an + // exception value, NoSuchObject when the agent has no such object, + // NoSuchInstance when it has one but not this instance, RFC 3416 section + // 4.2.1; or another name). Otherwise the zero VarBind. + VarBind VarBind +} + +// WalkReason is what ended a walk (WalkEnd). +type WalkReason uint8 + +// The reasons a walk ends without an error. +const ( + // WalkOutside is a name outside the subtree, after the walk's first + // value. + WalkOutside WalkReason = iota + 1 + // WalkException is an exception value (RFC 3416 section 4.2: + // NoSuchObject, NoSuchInstance or EndOfMibView) in the subtree. + WalkException + // WalkNoSuchName is an answer whose error-status is NoSuchName, the end + // of an SNMPv1 MIB, after the walk's first answer. + WalkNoSuchName + // WalkEmpty is an answer without varbinds. + WalkEmpty + // WalkLeaf is the Get of rootOid, taken for a leaf: the walk's first + // answer was NoSuchName or its first name was outside the subtree. + WalkLeaf +) + +//go:generate go tool -modfile=tools/go.mod stringer -type=WalkReason -trimprefix=Walk + +// walk returns the values of the subtree under rootOid and how the walk +// ended: it asks the agent for the objects after the root, then after the +// last name it received, with requestType (GetNext or GetBulk), and collects +// the objects in the subtree until an answer ends the walk (visit's rule). +// When that answer shows the root to be a leaf, the walk gets it once +// (getRoot). maxRepetitions is the GetBulk's max-repetitions, zero for the +// default; it is checked after the root, before the first request. Each +// request runs within ctx. An error ends the walk with the values collected +// before it. +func (x *Client) walk(ctx context.Context, requestType PDUType, rootOid string, maxRepetitions uint32) (results []VarBind, end WalkEnd, err error) { root, err := walkRoot(rootOid) if err != nil { - return nil, err + return nil, WalkEnd{}, err } // RFC 3416 section 3: max-repetitions INTEGER (0..max-bindings), where // max-bindings is 2147483647. if maxRepetitions > math.MaxInt32 { - return nil, fmt.Errorf("max-repetitions %d is more than %d", maxRepetitions, math.MaxInt32) + return nil, WalkEnd{}, fmt.Errorf("max-repetitions %d is more than %d", maxRepetitions, math.MaxInt32) } w := walker{ x: x, @@ -85,26 +133,34 @@ func (x *Client) walk(ctx context.Context, requestType PDUType, rootOid string, } oid := w.root - for requests := 1; ; requests++ { + for firstAnswer := true; ; firstAnswer = false { response, err := w.request(ctx, oid) - if err != nil { - return results, err - } var next walkStep - results, next, err = w.visit(response, oid, requests == 1, results) - if err != nil { - return results, err + switch { + case err == nil: + if results, end, next, err = w.visit(response, oid, firstAnswer, results); err != nil { + return results, WalkEnd{}, err + } + case isStatus(err, NoSuchName): + // noSuchName is the end of an SNMPv1 MIB: as the first answer, + // the root may still be a leaf, the MIB's last object. + next, end = walkDone, WalkEnd{Reason: WalkNoSuchName} + if firstAnswer { + next = walkGetRoot + } + default: + // Any other error, another error-status included, ends the walk + // with it. + return results, WalkEnd{}, err } switch next { case walkContinue: oid = response.VarBinds[len(response.VarBinds)-1].Name continue case walkGetRoot: - if results, err = w.getRoot(ctx, results); err != nil { - return results, err - } + return w.getRoot(ctx, results) } - return results, nil + return results, end, nil } } @@ -151,7 +207,8 @@ func walkRoot(rootOid string) (string, error) { } // request asks the agent for the objects after oid, within ctx, with the -// walk's max-repetitions for a GetBulk, which SNMPv1 does not have. +// walk's max-repetitions for a GetBulk, which SNMPv1 does not have. An answer +// with an error-status fails it with a *StatusError (send). func (w *walker) request(ctx context.Context, oid string) (*Message, error) { var maxReps uint32 if w.requestType == GetBulkRequest { @@ -168,27 +225,17 @@ func (w *walker) request(ctx context.Context, oid string) (*Message, error) { } // visit applies the walk's end rule, net-snmp's (apps/snmpwalk.c and -// apps/snmpbulkwalk.c at v5.9.4), to response: it appends the values of -// response that lie in the subtree to results, in order, and returns them and -// what the walk does next. oid is the name the request asked for; firstAnswer -// marks the walk's first answer: a walk goes on only after an answer whose -// values it all collected, so only the first answer can end a walk that found -// nothing, the case in which net-snmp gets the root. -func (w *walker) visit(response *Message, oid string, firstAnswer bool, results []VarBind) ([]VarBind, walkStep, error) { - // The error statuses tooBig to inconsistentName (RFC 3416) end the walk. - // noSuchName is the end of an SNMPv1 MIB: as the first answer, the root may - // still be a leaf, the MIB's last object. Known bugs: the others end the - // walk with a nil error and the values so far, where net-snmp fails; a - // status the RFC does not name is ignored and the answer walked. - if status := response.ErrorStatus; rfcError(status) { - if status == NoSuchName && firstAnswer { - return results, walkGetRoot, nil - } - return results, walkDone, nil - } - // Known bug: an empty answer ends the walk with a nil error. +// apps/snmpbulkwalk.c at v5.9.4), to response, an answer without an +// error-status (walk reads those): it appends the values of response that lie +// in the subtree to results, in order, and returns them, how the walk ended +// when the answer ends it, and what the walk does next. oid is the name the +// request asked for; firstAnswer marks the walk's first answer: a walk goes on +// only after an answer whose values it all collected, so only the first answer +// can end a walk that found nothing, the case in which net-snmp gets the root. +func (w *walker) visit(response *Message, oid string, firstAnswer bool, results []VarBind) ([]VarBind, WalkEnd, walkStep, error) { + // An empty answer ends the walk, where net-snmp sends the request again. if len(response.VarBinds) == 0 { - return results, walkDone, nil + return results, WalkEnd{Reason: WalkEmpty}, walkDone, nil } for i, pdu := range response.VarBinds { @@ -196,45 +243,52 @@ func (w *walker) visit(response *Message, oid string, firstAnswer bool, results // it shows the root to be a leaf (gosnmp/gosnmp#78). if !inSubtree(pdu.Name, w.root) { if firstAnswer && i == 0 { - return results, walkGetRoot, nil + return results, WalkEnd{}, walkGetRoot, nil } - return results, walkDone, nil + return results, WalkEnd{Reason: WalkOutside, VarBind: pdu}, walkDone, nil } // An exception value (RFC 3416 section 4.2) ends the walk, also as its // first value, without the leaf Get, as in net-snmp's snmpwalk: a leaf // root that is the MIB's last object answers endOfMibView and is not // fetched. if isException(pdu.Value.Type()) { - return results, walkDone, nil + return results, WalkEnd{Reason: WalkException, VarBind: pdu}, walkDone, nil } // Each name is compared with the request's, as net-snmp's // snmpbulkwalk does: a name equal to the root fails too. if oidCompare(oid, pdu.Name) >= 0 { - return results, walkDone, fmt.Errorf("OID not increasing: %s >= %s", oid, pdu.Name) + return results, WalkEnd{}, walkDone, fmt.Errorf("OID not increasing: %s >= %s", oid, pdu.Name) } results = append(results, pdu) } - return results, walkContinue, nil + return results, WalkEnd{}, walkContinue, nil } // getRoot gets the root, which the walk found to be a leaf, within ctx, as -// net-snmp's snmpwalk does after a walk that printed nothing, and appends the -// answer's first value to results when it names the root and is not an -// exception value (a Get of one name is answered by its first varbind). -// Statuses read as in visit: an error status adds nothing (known bug: one the -// RFC does not name is ignored). -func (w *walker) getRoot(ctx context.Context, results []VarBind) ([]VarBind, error) { +// net-snmp's snmpwalk does after a walk that printed nothing, and returns +// results with the answer's first value appended when it names the root and is +// not an exception value (a Get of one name is answered by its first varbind), +// and the end WalkLeaf with that varbind. An answer with an error-status adds +// nothing and ends the walk without an error, as net-snmp ignores it; a failed +// Get fails the walk. +func (w *walker) getRoot(ctx context.Context, results []VarBind) ([]VarBind, WalkEnd, error) { + end := WalkEnd{Reason: WalkLeaf} response, err := w.x.Get(ctx, []string{w.root}) - if err != nil { - return results, err + switch { + case err == nil: + case errors.As(err, new(*StatusError)): + return results, end, nil + default: + return results, WalkEnd{}, err } - if rfcError(response.ErrorStatus) || len(response.VarBinds) == 0 { - return results, nil + if len(response.VarBinds) == 0 { + return results, end, nil } - if pdu := response.VarBinds[0]; pdu.Name == w.root && !isException(pdu.Value.Type()) { - return append(results, pdu), nil + end.VarBind = response.VarBinds[0] + if pdu := end.VarBind; pdu.Name == w.root && !isException(pdu.Value.Type()) { + results = append(results, pdu) } - return results, nil + return results, end, nil } // inSubtree reports whether name is root or one of the names under it: it @@ -247,10 +301,12 @@ func inSubtree(name, root string) bool { return strings.HasPrefix(name, root) && (len(name) == len(root) || name[len(root)] == '.') } -// rfcError reports whether status is one of the error statuses RFC 3416 -// names, tooBig to inconsistentName. -func rfcError(status ErrorStatus) bool { - return status >= TooBig && status <= InconsistentName +// isStatus reports whether err is a *StatusError of status. Its target is a +// local of its own: one that escapes to errors.As in walk's loop would be +// allocated for every request. +func isStatus(err error, status ErrorStatus) bool { + var s *StatusError + return errors.As(err, &s) && s.Status == status } // isException reports whether t is an exception value of RFC 3416 section diff --git a/walkreason_string.go b/walkreason_string.go new file mode 100644 index 0000000..df92178 --- /dev/null +++ b/walkreason_string.go @@ -0,0 +1,28 @@ +// Code generated by "stringer -type=WalkReason -trimprefix=Walk"; DO NOT EDIT. + +package gosnmp + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[WalkOutside-1] + _ = x[WalkException-2] + _ = x[WalkNoSuchName-3] + _ = x[WalkEmpty-4] + _ = x[WalkLeaf-5] +} + +const _WalkReason_name = "OutsideExceptionNoSuchNameEmptyLeaf" + +var _WalkReason_index = [...]uint8{0, 7, 16, 26, 31, 35} + +func (i WalkReason) String() string { + idx := int(i) - 1 + if i < 1 || idx >= len(_WalkReason_index)-1 { + return "WalkReason(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _WalkReason_name[_WalkReason_index[idx]:_WalkReason_index[idx+1]] +}