Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
34 changes: 23 additions & 11 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
10 changes: 10 additions & 0 deletions engine_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down
11 changes: 11 additions & 0 deletions engine_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading
Loading