-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnmp.go
717 lines (613 loc) · 14.8 KB
/
snmp.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
package snmp
import (
"bytes"
"errors"
"fmt"
"log"
"math/rand"
"net"
"strings"
"sync"
"time"
"github.com/beorn7/perks/quantile"
"github.com/nikandfor/snmp/asn1"
)
var ( // errors
ErrReadTimeout = errors.New("timeout")
ErrExtraData = errors.New("extra data")
ErrClosed = errors.New("closed client")
)
type (
Type = asn1.Type
Command = Type
Version int
Var struct {
ObjectID asn1.OID
Type asn1.Type
Value interface{}
}
PDU struct {
Version Version
Community string
Command Command
ReqID int32
NonRepeaters, MaxRepetitions int
ErrorStatus, ErrorIndex int
Vars []Var
}
Client struct {
conn net.PacketConn
Receive chan<- Packet
ReadTimeout time.Duration
Retries int
Version Version
Community string
NonRepeaters int
MaxRepetitions int
mu sync.Mutex
reqid int32
reqs map[int32]chan Packet
stopc chan struct{}
}
Packet struct {
Addr net.Addr
PDU *PDU
Err error
}
Telemetry struct {
Requests *quantile.Stream
Duration time.Duration
Errors int
}
)
const ( // Versions
Version1 Version = 0
Version2c Version = 1
Version3 Version = 3
)
const ( // Commands
Get Command = asn1.Context | asn1.Constructor | 0x00
GetNext Command = asn1.Context | asn1.Constructor | 0x01
Response Command = asn1.Context | asn1.Constructor | 0x02
Set Command = asn1.Context | asn1.Constructor | 0x03
Trap Command = asn1.Context | asn1.Constructor | 0x04
GetBulk Command = asn1.Context | asn1.Constructor | 0x05
)
const ( // Types
Boolean Type = asn1.Universal | 0x1
Integer Type = asn1.Universal | 0x2
BitString Type = asn1.Universal | 0x3
OctetString Type = asn1.Universal | 0x4
Null Type = asn1.Universal | 0x5
ObjectID Type = asn1.Universal | 0x6
Sequence Type = asn1.Universal | 0x10
TypeSet Type = asn1.Universal | 0x11
IPAddress Type = asn1.Application | 0x0
Counter Type = asn1.Application | 0x1
Gauge Type = asn1.Application | 0x2
Timeticks Type = asn1.Application | 0x3
Opaque Type = asn1.Application | 0x4
Counter64 Type = asn1.Application | 0x6
Float Type = asn1.Application | 0x8
Double Type = asn1.Application | 0x9
Integer64 Type = asn1.Application | 0x10
Unsigned64 Type = asn1.Application | 0x11
NoSuchObject Type = asn1.Context | asn1.Primitive | 0x0
NoSuchInstance Type = asn1.Context | asn1.Primitive | 0x1
EndOfMIBView Type = asn1.Context | asn1.Primitive | 0x2
)
const ( // Error statuses
NoError = iota
TooBigError
NoSuchNameError
BadValueError
ReadOnlyError
GeneralError
)
// NewTelemetry returns inited telemetry object
func NewTelemetry() *Telemetry {
return &Telemetry{
Requests: quantile.NewHighBiased((time.Millisecond / 10).Seconds()),
}
}
// NewClient creates SNMP client with default options
func NewClient(conn net.PacketConn) *Client {
return &Client{
conn: conn,
ReadTimeout: time.Second,
Retries: 1,
Version: Version2c,
Community: "public",
NonRepeaters: 0,
MaxRepetitions: 50,
reqid: rand.Int31n(0x1000000),
reqs: make(map[int32]chan Packet),
stopc: make(chan struct{}),
}
}
// Send sends Protocol Data Unit to addr
func (c *Client) Send(addr net.Addr, p *PDU) error {
b := p.EncodeTo(nil)
_, err := c.conn.WriteTo(b, addr)
return err
}
// Read reads Protocol Data Unit from connection
func (c *Client) Read(buf []byte, p *PDU) (net.Addr, *PDU, error) {
if buf == nil {
buf = make([]byte, 0x4000)
}
err := c.conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
if err != nil {
return nil, nil, err
}
n, addr, err := c.conn.ReadFrom(buf)
if err != nil {
return nil, nil, err
}
if p == nil {
p = new(PDU)
}
err = p.Decode(buf[:n])
return addr, p, err
}
// Call sends Protocol Data Unit to address and waits for response for c.ReadTimeout
func (c *Client) Call(addr net.Addr, p *PDU) (_ *PDU, err error) {
// defer func() {
// log.Printf("Call %v %+v -> %v", addr, p, err)
// }()
rc := make(chan Packet, 1)
c.mu.Lock()
again:
c.reqid++
reqid := c.reqid
if _, ok := c.reqs[reqid]; ok {
goto again
}
c.reqs[reqid] = rc
c.mu.Unlock()
p.ReqID = reqid
defer func() {
c.mu.Lock()
delete(c.reqs, reqid)
c.mu.Unlock()
}()
err = c.Send(addr, p)
if err != nil {
return nil, err
}
select {
case resp := <-rc:
return resp.PDU, resp.Err
case <-time.After(c.ReadTimeout):
return nil, ErrReadTimeout
}
}
// Walk scans addr for variables with prefix root.
// It retries on fail and downgrade protocol version if no answer.
// Additional arguments are supported:
// *PDU - use that pdu instead of default
// Version - set protocol version
// Command - set command
// *Telemetry - collect telemetry of requests
func (c *Client) Walk(addr net.Addr, root asn1.OID, args ...interface{}) (*PDU, error) {
p := &PDU{
Version: c.Version,
Community: c.Community,
Command: GetBulk,
NonRepeaters: c.NonRepeaters,
MaxRepetitions: c.MaxRepetitions,
}
var t *Telemetry
var start time.Time
for _, a := range args {
switch a := a.(type) {
case nil:
case *PDU:
p = a
case Version:
p.Version = a
case Command:
p.Command = a
case *Telemetry:
t = a
start = time.Now()
}
}
var vars []Var
var err error
var resp *PDU
obj := root
try := 0
out:
for {
if cap(p.Vars) == 0 {
p.Vars = []Var{{ObjectID: obj, Type: asn1.Null}}
} else {
p.Vars = p.Vars[:1]
p.Vars[0] = Var{ObjectID: obj, Type: asn1.Null}
}
var st time.Time
if t != nil {
st = time.Now()
}
resp, err = c.Call(addr, p)
if t != nil {
t.Requests.Insert(time.Since(st).Seconds())
if err != nil || resp.ErrorStatus != 0 {
t.Errors++
}
}
switch {
case err == nil && resp.ErrorStatus == 0:
// ok
case err == ErrReadTimeout:
if try >= c.Retries {
break out
}
if try < c.Retries/2 && p.MaxRepetitions > 4 {
p.MaxRepetitions /= 2
} else if p.Version == Version2c {
p.Version = Version1
p.Command = GetNext
}
try++
continue
case err != nil:
break out
case resp.ErrorStatus == NoSuchNameError:
if len(resp.Vars) > 1 {
log.Printf("some vars are probably lost\n%v", resp.Dump())
}
break out
case resp.ErrorStatus == TooBigError && p.MaxRepetitions > 1:
p.MaxRepetitions /= 2
continue
default:
err = fmt.Errorf("SNMP error: %d (%d)", resp.ErrorStatus, resp.ErrorIndex)
break out
}
if len(resp.Vars) == 0 {
break
}
for _, v := range resp.Vars {
if v.Type == EndOfMIBView {
break out
}
if !v.ObjectID.HasPrefix(root) {
break out
}
vars = append(vars, v)
}
last := resp.Vars[len(resp.Vars)-1]
obj = last.ObjectID
try = 0
}
if t != nil {
t.Duration = time.Since(start)
}
if err != nil {
if resp == nil {
resp = p
resp.Vars = nil
}
resp.Vars = append(vars, resp.Vars...)
resp.NonRepeaters = p.NonRepeaters
resp.MaxRepetitions = p.MaxRepetitions
return resp, err
}
resp.Vars = vars
resp.NonRepeaters = p.NonRepeaters
resp.MaxRepetitions = p.MaxRepetitions
return resp, nil
}
// Run does background tasks reqiured for work.
// It's blocked until Client Closed
func (c *Client) Run() {
buf := make([]byte, 0x10000)
for {
err := c.conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
if err != nil {
log.Printf("SetDeadline: %v", err)
}
n, addr, err := c.conn.ReadFrom(buf)
select {
case <-c.stopc:
c.closeAllRequests(err)
return
default:
}
if err != nil {
if e, ok := err.(net.Error); ok && e.Timeout() {
continue
}
log.Printf("conn.Read: %v %v", err, err)
continue
}
p := new(PDU)
err = p.Decode(buf[:n])
c.mu.Lock()
rc := c.reqs[p.ReqID]
c.mu.Unlock()
// log.Printf("Read packet ver %v from %v id %x err-st %d vars %d err %v bytes %d", p.Version, addr, p.ReqID, p.ErrorStatus, len(p.Vars), err, n)
if rc == nil {
select {
case c.Receive <- Packet{Addr: addr, PDU: p, Err: err}:
default:
}
continue
}
select {
case rc <- Packet{
Addr: addr,
PDU: p,
Err: err,
}:
default:
log.Printf("packet dropped: from %20v, id %8x, err %v", addr, p.ReqID, err)
}
}
}
func (c *Client) closeAllRequests(err error) {
if err == nil {
err = ErrClosed
}
c.mu.Lock()
for _, rc := range c.reqs {
rc <- Packet{Err: err}
}
c.mu.Unlock()
}
// Close stops Run and closes connection
func (c *Client) Close() error {
close(c.stopc)
return c.conn.Close()
}
func Dumper(conn net.PacketConn) error {
buf := make([]byte, 0x10000)
for {
n, addr, err := conn.ReadFrom(buf)
if err != nil {
log.Printf("read: %v", err)
continue
}
p := new(PDU)
err = p.Decode(buf[:n])
if err != nil {
log.Printf("decode: %v", err)
}
log.Printf("pdu from %v:\n%v", addr, p.Dump())
}
return nil
}
// EncodeTo encodes Protocol Data Unit to buffer.
// If buffer is nil or not big enough new buffer is allocated.
func (p *PDU) EncodeTo(b []byte) []byte {
return asn1.BuildSequence(b, asn1.Sequence|asn1.Constructor, func(b []byte) []byte {
b = asn1.BuildInt(b, asn1.Universal|asn1.Primitive|asn1.Integer, int(p.Version))
b = asn1.BuildString(b, asn1.Universal|asn1.Primitive|asn1.OctetString, p.Community)
b = asn1.BuildSequence(b, p.Command, func(b []byte) []byte {
b = asn1.BuildInt32(b, asn1.Universal|asn1.Primitive|asn1.Integer, p.ReqID)
switch p.Command {
case GetBulk:
b = asn1.BuildInt(b, asn1.Universal|asn1.Primitive|asn1.Integer, p.NonRepeaters)
b = asn1.BuildInt(b, asn1.Universal|asn1.Primitive|asn1.Integer, p.MaxRepetitions)
default:
b = asn1.BuildInt(b, asn1.Universal|asn1.Primitive|asn1.Integer, p.ErrorStatus)
b = asn1.BuildInt(b, asn1.Universal|asn1.Primitive|asn1.Integer, p.ErrorIndex)
}
b = asn1.BuildSequence(b, asn1.Sequence|asn1.Constructor, func(b []byte) []byte {
for _, v := range p.Vars {
b = v.EncodeTo(b)
}
return b
})
return b
})
return b
})
}
// Decode decodes Protocol Data Unit from the buffer
func (p *PDU) Decode(b []byte) (err error) {
b, err = asn1.ParseSequence(b, func(tp asn1.Type, b []byte) (err error) {
b, _, v := asn1.ParseInt(b)
p.Version = Version(v)
b, _, p.Community = asn1.ParseString(b)
b, err = asn1.ParseSequence(b, func(tp asn1.Type, b []byte) (err error) {
p.Command = tp
b, _, p.ReqID = asn1.ParseInt32(b)
switch p.Command {
case GetBulk:
b, _, p.NonRepeaters = asn1.ParseInt(b)
b, _, p.MaxRepetitions = asn1.ParseInt(b)
default:
b, _, p.ErrorStatus = asn1.ParseInt(b)
b, _, p.ErrorIndex = asn1.ParseInt(b)
}
b, err = asn1.ParseSequence(b, func(tp asn1.Type, b []byte) error {
for len(b) > 0 {
var v Var
b, err = v.Decode(b)
if err != nil {
return err
}
p.Vars = append(p.Vars, v)
}
return nil
})
if err != nil {
return err
}
if len(b) != 0 {
return ErrExtraData
}
return nil
})
if err != nil {
return err
}
if len(b) != 0 {
return ErrExtraData
}
return nil
})
if err != nil {
return err
}
if len(b) != 0 {
return ErrExtraData
}
return nil
}
// EncodeTo encodes Variable to buffer.
// If buffer is nil or not big enough new buffer is allocated.
func (v *Var) EncodeTo(b []byte) []byte {
return asn1.BuildSequence(b, asn1.Sequence|asn1.Constructor, func(b []byte) []byte {
b = asn1.BuildObjectID(b, asn1.ObjectID, v.ObjectID)
switch v.Type {
case asn1.Null:
b = asn1.BuildNull(b, v.Type)
case asn1.Integer:
b = asn1.BuildInt(b, v.Type, v.Value.(int))
case asn1.OctetString:
b = asn1.BuildString(b, v.Type, v.Value.(string))
default:
panic(v.Type)
}
return b
})
}
// Decode decodes Variable from the buffer
func (v *Var) Decode(b []byte) ([]byte, error) {
return asn1.ParseSequence(b, func(_ asn1.Type, b []byte) error {
b, _, obj := asn1.ParseObjectID(b)
v.ObjectID = obj
_, tp, _ := asn1.ParseHeader(b)
switch tp {
case asn1.Integer, Counter, Counter64, Gauge:
b, v.Type, v.Value = asn1.ParseInt(b)
case Timeticks:
var tk int64
b, v.Type, tk = asn1.ParseInt64(b)
v.Value = time.Second / 100 * time.Duration(tk)
case asn1.OctetString:
b, v.Type, v.Value = asn1.ParseString(b)
case asn1.ObjectID:
b, v.Type, v.Value = asn1.ParseObjectID(b)
case IPAddress:
var r []byte
b, v.Type, r = asn1.ParseRaw(b)
v.Value = net.IP(r)
case Null:
b, v.Type, _ = asn1.ParseRaw(b)
default:
b, v.Type, v.Value = asn1.ParseRaw(b)
}
if len(b) != 0 {
return ErrExtraData
}
return nil
})
}
func (p *PDU) String() string {
var b strings.Builder
fmt.Fprintf(&b, "ver %2v community %v cmd %v reqid %x ", p.Version, p.Community, CommandString(p.Command), p.ReqID)
switch p.Command {
case GetBulk:
fmt.Fprintf(&b, "non-rep %d max-rep %d", p.NonRepeaters, p.MaxRepetitions)
default:
fmt.Fprintf(&b, "err-status %d err-index %d", p.ErrorStatus, p.ErrorIndex)
}
fmt.Fprintf(&b, " vars %d", len(p.Vars))
// b.WriteByte('\n')
// for _, v := range p.Vars {
// b.WriteString(v.String())
// b.WriteByte('\n')
// }
return b.String()
}
// Dump returns Protocol Data Unit header info and all Variables
func (p *PDU) Dump() string {
var b strings.Builder
b.WriteString(p.String())
b.WriteByte('\n')
for _, v := range p.Vars {
b.WriteString(v.String())
b.WriteByte('\n')
}
return b.String()
}
func (v Var) String() string {
var b bytes.Buffer
fmt.Fprintf(&b, "%-30v tp %-12v : ", v.ObjectID, TypeString(v.Type))
switch v.Type {
case asn1.OctetString:
fmt.Fprintf(&b, "%q", v.Value)
default:
fmt.Fprintf(&b, "%v", v.Value)
}
return b.String()
}
func (v Version) String() string {
switch v {
case Version1:
return "1"
case Version2c:
return "2c" //nolint:goconst
case Version3:
return "3"
default:
return fmt.Sprintf("%d", int(v))
}
}
// TypeString formats Type as a string
func TypeString(t Type) string {
if t&0xc0 == 0 {
return t.String()
}
v, ok := map[asn1.Type]string{
IPAddress: "IPAddress",
Counter: "Counter",
Gauge: "Gauge",
Timeticks: "Timeticks",
Opaque: "Opaque",
Counter64: "Counter64",
Float: "Float",
Double: "Double",
Integer64: "Integer64",
Unsigned64: "Unsigned64",
NoSuchObject: "NoSuchObject",
NoSuchInstance: "NoSuchInstance",
EndOfMIBView: "EndOfMIBView",
}[t]
if ok {
return v
}
return fmt.Sprintf("Type[%x]", int(t))
}
// CommandString formats Command as a string
func CommandString(t Command) string {
v, ok := map[asn1.Type]string{
Get: "Get",
GetNext: "GetNext",
Response: "Response",
Set: "Set",
Trap: "Trap",
GetBulk: "GetBulk",
}[t]
if ok {
return v
}
return fmt.Sprintf("Command[%x]", int(t))
}
func ParseVersion(s string) (Version, error) {
switch s {
case "1":
return Version1, nil
case "2c":
return Version2c, nil
case "3":
return Version3, nil
default:
return 0, errors.New("unsupported version")
}
}