forked from cdevr/WapSNMP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnmp.go
368 lines (309 loc) · 10.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
// Package wapsnmp provides an SNMP query library.
package wapsnmp
import (
"fmt"
"log"
"math/rand"
"net"
"reflect"
"time"
)
// WapSNMP is the type that lets you do SNMP requests.
type WapSNMP struct {
Target string // Target device for these SNMP events.
Community string // Community to use to contact the device.
Version SNMPVersion // SNMPVersion to encode in the packets.
timeout time.Duration // Timeout to use for all SNMP packets.
retries int // Number of times to retry an operation.
conn net.Conn // Cache the UDP connection in the object.
}
// SNMPValue type to express an oid value pair.
type SNMPValue struct {
Oid Oid
Value interface{}
}
const (
bufSize int = 16384
)
// NewWapSNMP creates a new WapSNMP object. Opens a udp connection to the device that will be used for the SNMP packets.
func NewWapSNMP(target, community string, version SNMPVersion, timeout time.Duration, retries int) (*WapSNMP, error) {
targetPort := fmt.Sprintf("%s:161", target)
conn, err := net.DialTimeout("udp", targetPort, timeout)
if err != nil {
return nil, fmt.Errorf(`error connecting to ("udp", "%s"): %s`, targetPort, err)
}
return &WapSNMP{target, community, version, timeout, retries, conn}, nil
}
// NewWapSNMPOnConn creates a new WapSNMP object from an existing net.Conn.
//
// It does not check if the provided target is valid.
func NewWapSNMPOnConn(target, community string, version SNMPVersion, timeout time.Duration, retries int, conn net.Conn) *WapSNMP {
return &WapSNMP{target, community, version, timeout, retries, conn}
}
// RandomRequestID generates a valid SNMP request ID.
func RandomRequestID() int {
return int(rand.Int31())
}
// poll sends a packet and wait for a response. Both operations can timeout, they're retried up to retries times.
func poll(conn net.Conn, toSend []byte, respondBuffer []byte, retries int, timeout time.Duration) (int, error) {
var err error
for i := 0; i < retries+1; i++ {
deadline := time.Now().Add(timeout)
if err = conn.SetWriteDeadline(deadline); err != nil {
log.Printf("Couldn't set write deadline. Retrying. Retry %d/%d\n", i, retries)
continue
}
if _, err = conn.Write(toSend); err != nil {
log.Printf("Couldn't write. Retrying. Retry %d/%d\n", i, retries)
continue
}
deadline = time.Now().Add(timeout)
if err = conn.SetReadDeadline(deadline); err != nil {
log.Printf("Couldn't set read deadline. Retrying. Retry %d/%d\n", i, retries)
continue
}
numRead := 0
if numRead, err = conn.Read(respondBuffer); err != nil {
log.Printf("Couldn't read. Retrying. Retry %d/%d\n", i, retries)
continue
}
return numRead, nil
}
return 0, err
}
// Get sends an SNMP get request requesting the value for an oid.
func (w WapSNMP) Get(oid Oid) (interface{}, error) {
requestID := RandomRequestID()
req, err := EncodeSequence([]interface{}{Sequence, int(w.Version), w.Community,
[]interface{}{AsnGetRequest, requestID, 0, 0,
[]interface{}{Sequence,
[]interface{}{Sequence, oid, nil}}}})
if err != nil {
return nil, err
}
response := make([]byte, bufSize, bufSize)
numRead, err := poll(w.conn, req, response, w.retries, w.timeout)
if err != nil {
return nil, err
}
decodedResponse, err := DecodeSequence(response[:numRead])
if err != nil {
return nil, err
}
// Fetch the varbinds out of the packet.
respPacket := decodedResponse[3].([]interface{})
varbinds := respPacket[4].([]interface{})
result := varbinds[1].([]interface{})[2]
return result, nil
}
// GetMultiple issues a single GET SNMP request requesting multiple values
func (w WapSNMP) GetMultiple(oids []Oid) (map[string]interface{}, error) {
requestID := RandomRequestID()
varbinds := []interface{}{Sequence}
for _, oid := range oids {
varbinds = append(varbinds, []interface{}{Sequence, oid, nil})
}
req, err := EncodeSequence([]interface{}{Sequence, int(w.Version), w.Community,
[]interface{}{AsnGetRequest, requestID, 0, 0, varbinds}})
if err != nil {
return nil, err
}
response := make([]byte, bufSize, bufSize)
numRead, err := poll(w.conn, req, response, w.retries, w.timeout)
if err != nil {
return nil, err
}
decodedResponse, err := DecodeSequence(response[:numRead])
if err != nil {
return nil, err
}
// Find the varbinds
respPacket := decodedResponse[3].([]interface{})
respVarbinds := respPacket[4].([]interface{})
result := make(map[string]interface{})
for _, v := range respVarbinds[1:] { // First element is just a sequence
oid := v.([]interface{})[1].(Oid).String()
value := v.([]interface{})[2]
result[oid] = value
}
return result, nil
}
// Set sends an SNMP set request to change the value associated with an oid.
func (w WapSNMP) Set(oid Oid, value interface{}) (interface{}, error) {
requestID := RandomRequestID()
req, err := EncodeSequence([]interface{}{Sequence, int(w.Version), w.Community,
[]interface{}{AsnSetRequest, requestID, 0, 0,
[]interface{}{Sequence,
[]interface{}{Sequence, oid, value}}}})
if err != nil {
return nil, err
}
response := make([]byte, bufSize, bufSize)
numRead, err := poll(w.conn, req, response, w.retries, w.timeout)
if err != nil {
return nil, err
}
decodedResponse, err := DecodeSequence(response[:numRead])
if err != nil {
return nil, err
}
// Fetch the varbinds out of the packet.
respPacket := decodedResponse[3].([]interface{})
varbinds := respPacket[4].([]interface{})
result := varbinds[1].([]interface{})[2]
return result, nil
}
// SetMultiple issues a single GET SNMP request requesting multiple values
func (w WapSNMP) SetMultiple(toset map[string]interface{}) (map[string]interface{}, error) {
requestID := RandomRequestID()
varbinds := []interface{}{Sequence}
for oid, value := range toset {
varbinds = append(varbinds, []interface{}{Sequence, oid, value})
}
req, err := EncodeSequence([]interface{}{Sequence, int(w.Version), w.Community,
[]interface{}{AsnGetRequest, requestID, 0, 0, varbinds}})
if err != nil {
return nil, err
}
response := make([]byte, bufSize, bufSize)
numRead, err := poll(w.conn, req, response, w.retries, w.timeout)
if err != nil {
return nil, err
}
decodedResponse, err := DecodeSequence(response[:numRead])
if err != nil {
return nil, err
}
// Find the varbinds
respPacket := decodedResponse[3].([]interface{})
respVarbinds := respPacket[4].([]interface{})
result := make(map[string]interface{})
for _, v := range respVarbinds[1:] { // First element is just a sequence
oid := v.([]interface{})[1].(Oid).String()
value := v.([]interface{})[2]
result[oid] = value
}
return result, nil
}
// GetNext issues a GETNEXT SNMP request.
func (w WapSNMP) GetNext(oid Oid) (*Oid, interface{}, error) {
requestID := RandomRequestID()
req, err := EncodeSequence([]interface{}{Sequence, int(w.Version), w.Community,
[]interface{}{AsnGetNextRequest, requestID, 0, 0,
[]interface{}{Sequence,
[]interface{}{Sequence, oid, nil}}}})
if err != nil {
return nil, nil, err
}
response := make([]byte, bufSize)
numRead, err := poll(w.conn, req, response, w.retries, w.timeout)
if err != nil {
return nil, nil, err
}
decodedResponse, err := DecodeSequence(response[:numRead])
if err != nil {
return nil, nil, err
}
// Find the varbinds
respPacket := decodedResponse[3].([]interface{})
varbinds := respPacket[4].([]interface{})
result := varbinds[1].([]interface{})
resultOid := result[1].(Oid)
resultVal := result[2]
return &resultOid, resultVal, nil
}
// GetBulk is semantically the same as maxRepetitions getnext requests, but in a single GETBULK SNMP packet.
//
// Caveat: many devices will silently drop GETBULK requests for more than some number of maxrepetitions, if
// it doesn't work, try with a lower value and/or use GetTable.
//
// Caveat: as codedance (on github) pointed out, iteration order on a map is indeterminate. You can alternatively
// use GetBulkArray to get the entries as a list, with deterministic iteration order.
func (w WapSNMP) GetBulk(oid Oid, maxRepetitions int) (map[string]interface{}, error) {
requestID := RandomRequestID()
req, err := EncodeSequence([]interface{}{Sequence, int(w.Version), w.Community,
[]interface{}{AsnGetBulkRequest, requestID, 0, maxRepetitions,
[]interface{}{Sequence,
[]interface{}{Sequence, oid, nil}}}})
if err != nil {
return nil, err
}
response := make([]byte, bufSize, bufSize)
numRead, err := poll(w.conn, req, response, w.retries, w.timeout)
if err != nil {
return nil, err
}
decodedResponse, err := DecodeSequence(response[:numRead])
if err != nil {
return nil, err
}
// Find the varbinds
respPacket := decodedResponse[3].([]interface{})
respVarbinds := respPacket[4].([]interface{})
result := make(map[string]interface{})
for _, v := range respVarbinds[1:] { // First element is just a sequence
oid := v.([]interface{})[1].(Oid).String()
value := v.([]interface{})[2]
result[oid] = value
}
return result, nil
}
// GetBulkArray is the same as GetBulk, but returns it's results as a list, for those who want deterministic
// iteration instead of convenient access.
func (w WapSNMP) GetBulkArray(oid Oid, maxRepetitions int) ([]SNMPValue, error) {
requestID := RandomRequestID()
req, err := EncodeSequence([]interface{}{Sequence, int(w.Version), w.Community,
[]interface{}{AsnGetBulkRequest, requestID, 0, maxRepetitions,
[]interface{}{Sequence,
[]interface{}{Sequence, oid, nil}}}})
if err != nil {
return nil, err
}
response := make([]byte, bufSize, bufSize)
numRead, err := poll(w.conn, req, response, w.retries, w.timeout)
if err != nil {
return nil, err
}
decodedResponse, err := DecodeSequence(response[:numRead])
if err != nil {
return nil, fmt.Errorf("error during sequence decoding: %v", err)
}
// Find the varbinds
respPacket := decodedResponse[3].([]interface{})
respVarbinds := respPacket[4].([]interface{})
result := make([]SNMPValue, 0, len(respVarbinds[1:]))
for _, v := range respVarbinds[1:] { // First element is just a sequence
oid := v.([]interface{})[1].(Oid)
value := v.([]interface{})[2]
result = append(result, SNMPValue{oid, value})
}
return result, nil
}
// GetTable efficiently gets an entire table from an SNMP agent. Uses GETBULK requests to go fast.
func (w WapSNMP) GetTable(oid Oid) (map[string]interface{}, error) {
result := make(map[string]interface{})
lastOid := oid.Copy()
for lastOid.Within(oid) {
results, err := w.GetBulkArray(lastOid, 50)
if err != nil {
return nil, fmt.Errorf("received GetBulk error => %v\n", err)
}
newLastOid := lastOid.Copy()
for _, v := range results {
if v.Oid.Within(oid) {
result[v.Oid.String()] = v.Value
}
newLastOid = v.Oid
}
if reflect.DeepEqual(lastOid, newLastOid) {
// Not making any progress ? Assume we reached end of table.
break
}
lastOid = newLastOid
}
return result, nil
}
// Close the net.conn in WapSNMP.
func (w WapSNMP) Close() error {
return w.conn.Close()
}