forked from gosnmp/gosnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosnmp.go
343 lines (298 loc) · 9.27 KB
/
gosnmp.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
// Copyright 2012-2014 The GoSNMP Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gosnmp
import (
"fmt"
"math/big"
"math/rand"
"net"
"strconv"
"time"
)
const (
// maxOids is the maximum number of oids allowed in a Get()
maxOids = 60
// Base OID for MIB-2 defined SNMP variables
baseOid = ".1.3.6.1.2.1"
// Java SNMP uses 50, snmp-net uses 10
defaultMaxRepetitions = 50
// TODO comment
defaultNonRepeaters = 0
)
// LoggingDisabled is set if the Logger is nil, short circuits any 'slog' calls
var LoggingDisabled bool
// GoSNMP represents GoSNMP library state
type GoSNMP struct {
// Target is an ipv4 address
Target string
// Port is a udp port
Port uint16
// Community is an SNMP Community string
Community string
// Version is an SNMP Version
Version SnmpVersion
// Timeout is the timeout for the SNMP Query
Timeout time.Duration
// Set the number of retries to attempt within timeout.
Retries int
// Conn is net connection to use, typically establised using GoSNMP.Connect()
Conn net.Conn
// Logger is the GoSNMP.Logger to use for debugging. If nil, debugging
// output will be discarded (/dev/null). For verbose logging to stdout:
// x.Logger = log.New(os.Stdout, "", 0)
Logger Logger
// Internal - used to sync requests to responses
requestID uint32
random *rand.Rand
}
// The default connection settings
var Default = &GoSNMP{
Port: 161,
Community: "public",
Version: Version2c,
Timeout: time.Duration(2) * time.Second,
Retries: 3,
}
// SnmpPDU will be used when doing SNMP Set's
type SnmpPDU struct {
// Name is an oid in string format eg ".1.3.6.1.4.9.27"
Name string
// The type of the value eg Integer
Type Asn1BER
// The value to be set by the SNMP set
Value interface{}
}
// Asn1BER is the type of the SNMP PDU
type Asn1BER byte
// Asn1BER's - http://www.ietf.org/rfc/rfc1442.txt
const (
EndOfContents Asn1BER = 0x00
UnknownType = 0x00 // TODO these should all be type Asn1BER, however
Boolean = 0x01 // tests fail if implemented. See for example
Integer = 0x02 /// http://stackoverflow.com/questions/5037610/typed-constant-declaration-list.
BitString = 0x03
OctetString = 0x04
Null = 0x05
ObjectIdentifier = 0x06
ObjectDescription = 0x07
IPAddress = 0x40
Counter32 = 0x41
Gauge32 = 0x42
TimeTicks = 0x43
Opaque = 0x44
NsapAddress = 0x45
Counter64 = 0x46
Uinteger32 = 0x47
NoSuchObject = 0x80
NoSuchInstance = 0x81
EndOfMibView = 0x82
)
//
// Public Functions (main interface)
//
// Connect initiates a connection to the target host
func (x *GoSNMP) Connect() error {
if x.Logger == nil {
LoggingDisabled = true
}
Conn, err := net.DialTimeout("udp", fmt.Sprintf("%s:%d", x.Target, x.Port), x.Timeout)
if err == nil {
x.Conn = Conn
} else {
return fmt.Errorf("Error establishing connection to host: %s\n", err.Error())
}
if x.random == nil {
x.random = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
}
x.requestID = x.random.Uint32()
return nil
}
// Get sends an SNMP GET request
func (x *GoSNMP) Get(oids []string) (result *SnmpPacket, err error) {
oidCount := len(oids)
if oidCount > maxOids {
return nil, fmt.Errorf("oid count (%d) is greater than maxOids (%d)",
oidCount, maxOids)
}
// convert oids slice to pdu slice
var pdus []SnmpPDU
for _, oid := range oids {
pdus = append(pdus, SnmpPDU{oid, Null, nil})
}
// build up SnmpPacket
packetOut := &SnmpPacket{
Community: x.Community,
Error: 0,
ErrorIndex: 0,
PDUType: GetRequest,
Version: x.Version,
}
return x.send(pdus, packetOut)
}
// Set sends an SNMP SET request
func (x *GoSNMP) Set(pdus []SnmpPDU) (result *SnmpPacket, err error) {
if len(pdus) != 1 {
return nil, fmt.Errorf("gosnmp currently only supports SNMP SETs for one oid")
}
if pdus[0].Type != Integer {
return nil, fmt.Errorf("gosnmp currently only supports SNMP SETs for Integers")
}
// build up SnmpPacket
packetOut := &SnmpPacket{
Community: x.Community,
Error: 0,
ErrorIndex: 0,
PDUType: SetRequest,
Version: x.Version,
}
return x.send(pdus, packetOut)
}
// GetNext sends an SNMP GETNEXT request
func (x *GoSNMP) GetNext(oids []string) (result *SnmpPacket, err error) {
oidCount := len(oids)
if oidCount > maxOids {
return nil, fmt.Errorf("oid count (%d) is greater than maxOids (%d)",
oidCount, maxOids)
}
// convert oids slice to pdu slice
var pdus []SnmpPDU
for _, oid := range oids {
pdus = append(pdus, SnmpPDU{oid, Null, nil})
}
// Marshal and send the packet
packetOut := &SnmpPacket{
Community: x.Community,
Error: 0,
ErrorIndex: 0,
PDUType: GetNextRequest,
Version: x.Version,
}
return x.send(pdus, packetOut)
}
// GetBulk sends an SNMP GETBULK request
func (x *GoSNMP) GetBulk(oids []string, nonRepeaters uint8, maxRepetitions uint8) (result *SnmpPacket, err error) {
oidCount := len(oids)
if oidCount > maxOids {
return nil, fmt.Errorf("oid count (%d) is greater than maxOids (%d)",
oidCount, maxOids)
}
// convert oids slice to pdu slice
var pdus []SnmpPDU
for _, oid := range oids {
pdus = append(pdus, SnmpPDU{oid, Null, nil})
}
// Marshal and send the packet
packetOut := &SnmpPacket{
Community: x.Community,
PDUType: GetBulkRequest,
Version: x.Version,
NonRepeaters: nonRepeaters,
MaxRepetitions: maxRepetitions,
}
return x.send(pdus, packetOut)
}
//
// SNMP Walk functions - Analogous to net-snmp's snmpwalk commands
//
// WalkFunc is the type of the function called for each data unit visited
// by the Walk function. If an error is returned processing stops.
type WalkFunc func(dataUnit SnmpPDU) error
// BulkWalk retrieves a subtree of values using GETBULK. As the tree is
// walked walkFn is called for each new value. The function immediately returns
// an error if either there is an underlaying SNMP error (e.g. GetBulk fails),
// or if walkFn returns an error.
func (x *GoSNMP) BulkWalk(rootOid string, walkFn WalkFunc) error {
return x.walk(GetBulkRequest, rootOid, walkFn)
}
// BulkWalkAll is similar to BulkWalk but returns a filled array of all values
// rather than using a callback function to stream results.
func (x *GoSNMP) BulkWalkAll(rootOid string) (results []SnmpPDU, err error) {
return x.walkAll(GetBulkRequest, rootOid)
}
// Walk retrieves a subtree of values using GETNEXT - a request is made for each
// value, unlike BulkWalk which does this operation in batches. As the tree is
// walked walkFn is called for each new value. The function immediately returns
// an error if either there is an underlaying SNMP error (e.g. GetNext fails),
// or if walkFn returns an error.
func (x *GoSNMP) Walk(rootOid string, walkFn WalkFunc) error {
return x.walk(GetNextRequest, rootOid, walkFn)
}
// WalkAll is similar to Walk but returns a filled array of all values rather
// than using a callback function to stream results.
func (x *GoSNMP) WalkAll(rootOid string) (results []SnmpPDU, err error) {
return x.walkAll(GetNextRequest, rootOid)
}
//
// Public Functions (helpers) - in alphabetical order
//
// Partition - returns true when dividing a slice into
// partitionSize lengths, including last partition which may be smaller
// than partitionSize. This is useful when you have a large array of OIDs
// to run Get() on. See the tests for example usage.
//
// For example for a slice of 8 items to be broken into partitions of
// length 3, Partition returns true for the currentPosition having
// the following values:
//
// 0 1 2 3 4 5 6 7
// T T T
//
func Partition(currentPosition, partitionSize, sliceLength int) bool {
if currentPosition < 0 || currentPosition >= sliceLength {
return false
}
if partitionSize == 1 { // redundant, but an obvious optimisation
return true
}
if currentPosition%partitionSize == partitionSize-1 {
return true
}
if currentPosition == sliceLength-1 {
return true
}
return false
}
// ToBigInt converts SnmpPDU.Value to big.Int, or returns a zero big.Int for
// non int-like types (eg strings).
//
// This is a convenience function to make working with SnmpPDU's easier - it
// reduces the need for type assertions. A big.Int is convenient, as SNMP can
// return int32, uint32, and uint64.
func ToBigInt(value interface{}) *big.Int {
var val int64
switch value := value.(type) { // shadow
case int:
val = int64(value)
case int8:
val = int64(value)
case int16:
val = int64(value)
case int32:
val = int64(value)
case int64:
val = int64(value)
case uint:
val = int64(value)
case uint8:
val = int64(value)
case uint16:
val = int64(value)
case uint32:
val = int64(value)
case uint64:
return (uint64ToBigInt(value))
case string:
// for testing and other apps - numbers may appear as strings
var err error
if val, err = strconv.ParseInt(value, 10, 64); err != nil {
return new(big.Int)
}
default:
return new(big.Int)
}
return big.NewInt(val)
}