This repository has been archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
341 lines (289 loc) · 9.11 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package snmp
import (
"context"
"net"
"time"
"github.com/k-sone/snmpgo"
)
const DefaultMaxRepetitions = 10
type TableRequest struct {
// Context for cancellation. If nil, uses the context of the MessageSender. Cancelling
// this context will prevent messages being sent using the channel C, but the Response
// function will be called regardless
Context context.Context
Addr net.Addr // Destination address. Should be *net.UDPAddr unless testing
Community []byte // Host's community string
SnmpV1 bool // SNMP version, only v1 and v2c are supported
Oids snmpgo.Oids // Table base OIDs to fetch
TimeoutAfter time.Duration // Timeout for each request
StopAfter time.Time // Stop request resends after this time
MaxRep int // defaults to DefaultMaxRepetitions when using GetBulk
Slow bool // when true, uses GetNext instead of GetBulk
// Update (when non-nil) will be called after each SNMP request/response,
// and allow the caller to update MaxRep if requested. This is useful when
// experimenting with increasing the MaxRep field to decrease roundtrip times, and
// compensating for that if packets are being dropped
//
// In the Update call, you can alter MaxRep and TimeoutAfter and it will take effect in
// the next SNMP packet. Return true to stop further processing.
Update func(tr *TableRequest, failure bool, took time.Duration) (stop bool)
// Response is called when a response comes in. If not nil, it will be
// called on the main thread. If it blocks, then the SNMP operations will
// block (so be fast!).
Response func(response TableResponse)
// C is a channel created by the user. If it is not nil, it messages will be
// sent to it. If it blocks then SNMP operations will block (so be fast!)
C chan TableResponse
}
type TableResponse struct {
Request *TableRequest
VarBinds []snmpgo.VarBinds // Responses corresponding to the indices of the OIDs in the request
// Err may be one of the following:
// ErrTimedOut
// ErrWalkSingleOid
// context.Canceled
// context.DeadlineExceeded
//
// This differs in behavior to MessageResponse.Err in that a new context can be
// passed into GetTableSlow or GetTable, allowing cancellation or deadlines
// to occur which will be passed back. This increase in functionality is allowed
// as this is a higher level API which builds on the MessageRequest primitives
Err error
Timeouts int // number of sub-requests that timed out
ResponseTimes []time.Duration // times of responses that did not time out
}
func (tr *TableResponse) TimedOut() bool {
return tr.Err == ErrTimedOut || tr.Err == context.DeadlineExceeded
}
func (tr *TableRequest) send(ctx context.Context, varbinds []snmpgo.VarBinds, err error,
timeouts int, responseTimes []time.Duration,
) {
tRes := TableResponse{
Request: tr,
VarBinds: varbinds,
Err: err,
Timeouts: timeouts,
ResponseTimes: responseTimes,
}
if tr.Response != nil {
tr.Response(tRes)
}
if tr.C != nil {
select {
case tr.C <- tRes:
case <-ctx.Done():
}
}
}
func (ms *MessageSender) tableChanListener() {
defer ms.wg.Done()
for {
select {
case <-ms.ctx.Done():
return
case req := <-ms.TC:
if req.Slow || req.SnmpV1 {
ms.tableChanNewSlowMessage(req)
} else {
ms.tableChanNewFastMessage(req)
}
}
}
}
type tableMessageSender struct {
ctx context.Context
ms *MessageSender
tr *TableRequest
vbs []snmpgo.VarBinds
off []int
timeouts int // each timeout increments this
took []time.Duration // duration of all successful requests
request MessageRequest // current sub-request
requestStart time.Time // current sub request start time
}
type slowTableMessageSender struct {
tableMessageSender
version snmpgo.SNMPVersion
}
func (ms *MessageSender) newTableMessageSender(tr *TableRequest) (tms tableMessageSender) {
tms.ms = ms
tms.tr = tr
tms.ctx = tr.Context
if tms.ctx == nil {
tms.ctx = ms.ctx
}
tms.vbs = make([]snmpgo.VarBinds, len(tr.Oids))
tms.off = make([]int, len(tr.Oids))
for i := range tr.Oids {
tms.off[i] = i
}
tms.request.Addr = tr.Addr
return tms
}
func (ms *MessageSender) tableChanNewSlowMessage(tr *TableRequest) {
stms := slowTableMessageSender{tableMessageSender: ms.newTableMessageSender(tr)}
if tr.SnmpV1 {
stms.version = snmpgo.V1
} else {
stms.version = snmpgo.V2c
}
stms.request.Message = NewMessageWithOids(stms.version, snmpgo.GetNextRequest, tr.Community, tr.Oids)
stms.request.Response = stms.onResponse
stms.sendMessageRequest()
}
func (tms *tableMessageSender) sendMessageRequest() {
if tms.tr.TimeoutAfter > 0 {
after := tms.tr.TimeoutAfter
if !tms.tr.StopAfter.IsZero() && time.Now().Add(after).After(tms.tr.StopAfter) {
after = tms.tr.StopAfter.Sub(time.Now())
}
tms.request.Retries(0, after)
} else if !tms.tr.StopAfter.IsZero() {
tms.request.Retries(0, tms.tr.StopAfter.Sub(time.Now()))
}
tms.requestStart = time.Now()
select {
case <-tms.ctx.Done():
tms.tr.send(tms.ctx, nil, tms.ctx.Err(), tms.timeouts, tms.took)
case tms.ms.MC <- &tms.request:
}
}
// return true to not continue
func (tms *tableMessageSender) onResponseErrCheck(r MessageResponse) bool {
took := time.Now().Sub(tms.requestStart)
var cancel bool
if tms.tr.Update != nil {
if tms.tr.Update(tms.tr, r.Err != nil, took) {
cancel = true
}
}
ctxErr := tms.ctx.Err()
if r.Err == nil && ctxErr != nil {
r.Err = ctxErr
}
if r.Err == nil {
tms.took = append(tms.took, took)
return false
}
tms.timeouts++
if r.Err == ErrTimedOut && tms.tr.TimeoutAfter > 0 {
if cancel {
r.Err = ErrCancelViaUpdate
} else if ctxErr != nil {
r.Err = ctxErr
} else if time.Now().Before(tms.tr.StopAfter) {
tms.sendMessageRequest()
return true
}
}
tms.tr.send(tms.ctx, nil, r.Err, tms.timeouts, tms.took)
return true
}
func (stms *slowTableMessageSender) onResponse(r MessageResponse) {
if stms.onResponseErrCheck(r) {
return
}
next := make(snmpgo.Oids, 0, len(stms.off))
nextOff := make([]int, 0, len(stms.off))
pdu := r.Response.Pdu
pvbs := pdu.VarBinds()
if len(pvbs) != len(stms.off) {
stms.tr.send(stms.ctx, nil, &ErrMalformedResponse{
ExpOIDs: len(stms.off),
GotOIDs: len(pvbs),
}, stms.timeouts, stms.took)
return
}
for i, vb := range pvbs {
idx := stms.off[i]
switch vb.Variable.(type) {
case *snmpgo.NoSucheObject: // legal if base OID not implemented
continue
case *snmpgo.NoSucheInstance: // shouldn't be possible?
continue
case *snmpgo.EndOfMibView: // legal
continue
case *snmpgo.Null: // SNMP v1 behavior?
continue
}
if !vb.Oid.Contains(stms.tr.Oids[idx]) {
continue
}
next = append(next, vb.Oid)
stms.vbs[idx] = append(stms.vbs[idx], vb)
nextOff = append(nextOff, stms.off[i])
}
stms.off = nextOff
if len(stms.off) == 0 {
stms.tr.send(stms.ctx, stms.vbs, nil, stms.timeouts, stms.took)
return
}
stms.request.Message = NewMessageWithOids(stms.version, snmpgo.GetNextRequest, stms.tr.Community, next)
stms.sendMessageRequest()
}
type fastTableMessageSender struct {
tableMessageSender
last snmpgo.Oids
}
func (ms *MessageSender) tableChanNewFastMessage(tr *TableRequest) {
ftms := fastTableMessageSender{tableMessageSender: ms.newTableMessageSender(tr)}
ftms.last = make(snmpgo.Oids, len(tr.Oids))
ftms.request.Message = NewMessageWithOids(snmpgo.V2c, snmpgo.GetBulkRequest, tr.Community, tr.Oids)
ftms.request.Message.Pdu.SetMaxRepetitions(tr.MaxRep)
ftms.request.Response = ftms.onResponse
ftms.sendMessageRequest()
}
func (ftms *fastTableMessageSender) onResponse(r MessageResponse) {
if ftms.onResponseErrCheck(r) {
return
}
pdu := r.Response.Pdu
pvbs := pdu.VarBinds()
unhealthy := make([]bool, len(ftms.off))
for i, vb := range pvbs {
mI := i % len(ftms.off)
idx := ftms.off[mI]
// fmt.Printf("i=%d mI=%d idx=%d vb=%s\n", i, mI, idx, vb)
if unhealthy[mI] {
continue
}
var bad bool
switch vb.Variable.(type) {
case *snmpgo.NoSucheObject: // shouldn't be possible in a getnext/getbulk
bad = true
case *snmpgo.NoSucheInstance: // shouldn't be possible in a getnext/getbulk
bad = true
case *snmpgo.EndOfMibView: // legal
bad = true
case *snmpgo.Null: // SNMP v1 behavior?
bad = true
}
if bad || !vb.Oid.Contains(ftms.tr.Oids[idx]) {
// fmt.Printf("i=%d mI=%d idx=%d %s does not contain %s\n", i, mI, idx, vb.Oid, tr.Oids[idx])
unhealthy[mI] = true
continue
}
//idx := off[mI]
ftms.last[idx] = vb.Oid
ftms.vbs[idx] = append(ftms.vbs[idx], vb)
}
// malloc inefficient, will fix if it ever becomes an issue (it won't)
next := make(snmpgo.Oids, 0, len(ftms.off))
nextOff := make([]int, 0, len(ftms.off))
for i, bad := range unhealthy {
if !bad {
idx := ftms.off[i]
v := ftms.vbs[idx]
next = append(next, v[len(v)-1].Oid)
nextOff = append(nextOff, ftms.off[i])
}
}
ftms.off = nextOff
if len(ftms.off) == 0 {
ftms.tr.send(ftms.ctx, ftms.vbs, nil, ftms.timeouts, ftms.took)
return
}
ftms.request.Message = NewMessageWithOids(snmpgo.V2c, snmpgo.GetBulkRequest, ftms.tr.Community, next)
ftms.request.Message.Pdu.SetMaxRepetitions(ftms.tr.MaxRep)
ftms.sendMessageRequest()
}