-
Notifications
You must be signed in to change notification settings - Fork 0
/
snmphelper.go
344 lines (302 loc) · 7.78 KB
/
snmphelper.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
// Package snmphelper implements some functions on top of gosnmp.
package snmphelper
import (
"encoding/binary"
"fmt"
"math"
"strings"
"time"
"github.com/gosnmp/gosnmp"
)
// Version of release
const Version = "1.1.3"
// SNMP session object
type Session struct {
Host, User, Prot, Pass, Slevel, PrivProt, PrivPass string
Ver int
MaxRepetitions, Timeout uint32 // Defaults: MaxRepetitions: 5, Timeout: 2
Snmp *gosnmp.GoSNMP
}
// Session.Result data type. SNMP query results will appear here.
type SnmpOut map[string]snmpValue
// Initializes and returns *gosnmp.GoSNMP in Session.Snmp
func (s *Session) New() (*Session, error) {
// Setup SNMP security model
usm := &gosnmp.UsmSecurityParameters{
UserName: s.User,
AuthenticationPassphrase: s.Pass,
PrivacyPassphrase: s.PrivPass,
}
//Initialise the SNMP object
snmp := &gosnmp.GoSNMP{
Target: s.Host,
Port: 161,
Community: s.User,
Timeout: time.Duration(4) * time.Second,
SecurityModel: gosnmp.UserSecurityModel,
MaxRepetitions: 5,
Retries: 1,
ExponentialTimeout: false,
SecurityParameters: usm,
}
// set some non default parameters if present
if s.MaxRepetitions != 0 {
snmp.MaxRepetitions = s.MaxRepetitions
}
if s.Timeout != 0 {
snmp.Timeout = time.Duration(s.Timeout) * time.Second
}
// Set version
switch s.Ver {
case 1:
snmp.Version = gosnmp.Version1
case 2:
snmp.Version = gosnmp.Version2c
case 3:
snmp.Version = gosnmp.Version3
default:
return nil, fmt.Errorf("invalid SNMP version - %d", s.Ver)
}
if s.Ver == 3 {
// Set seclevel
switch s.Slevel {
case "noAuthNoPriv":
snmp.MsgFlags = gosnmp.NoAuthNoPriv
case "authNoPriv":
snmp.MsgFlags = gosnmp.AuthNoPriv
case "authPriv":
snmp.MsgFlags = gosnmp.AuthPriv
default:
return nil, fmt.Errorf("invalid SNMP sec level - %s", s.Slevel)
}
// Set AuthenticationProtocol
switch s.Prot {
case "NoAuth":
usm.AuthenticationProtocol = gosnmp.NoAuth
case "MD5":
usm.AuthenticationProtocol = gosnmp.MD5
case "SHA":
usm.AuthenticationProtocol = gosnmp.SHA
default:
return nil, fmt.Errorf("invalid SNMP authentication protocol - %s", s.Prot)
}
// Set PrivacyProtocol
switch s.PrivProt {
case "NoPriv":
usm.PrivacyProtocol = gosnmp.NoPriv
case "DES":
usm.PrivacyProtocol = gosnmp.DES
case "AES":
usm.PrivacyProtocol = gosnmp.AES
case "AES192":
usm.PrivacyProtocol = gosnmp.AES192
case "AES192C":
usm.PrivacyProtocol = gosnmp.AES192C
case "AES256":
usm.PrivacyProtocol = gosnmp.AES256
case "AES256C":
usm.PrivacyProtocol = gosnmp.AES256C
default:
return nil, fmt.Errorf("invalid SNMP privacy protocol - %s", s.PrivProt)
}
}
s.Snmp = snmp
return s, nil
}
// Do SNMP get
func (s *Session) Get(oids []string) (SnmpOut, error) {
var out = SnmpOut{}
snmp := s.Snmp
if err := snmp.Connect(); err != nil {
return out, fmt.Errorf("%s - %s", s.Host, err)
}
defer snmp.Conn.Close()
// Do get
res, err := snmp.Get(oids)
if err != nil {
return out, fmt.Errorf("%s get %v - %s", s.Host, oids, err)
} else if res.Error != 0 {
return out, fmt.Errorf("%s get %v - %s", s.Host, oids, res.Error.String())
}
// Make formatted output
for _, p := range res.Variables {
k, v, err2 := formatValue(p, "", false)
if err2 != nil {
return out, fmt.Errorf("%s get %v - %s", s.Host, k, err2)
}
out[k] = v
}
return out, nil
}
// Do SNMP walk or bulkwalk
func (s *Session) Walk(oid string, bulk bool, stripoid bool) (SnmpOut, error) {
var out = SnmpOut{}
var pdus []gosnmp.SnmpPDU
snmp := s.Snmp
err := snmp.Connect()
if err != nil {
return out, fmt.Errorf("%s - %s", s.Host, err)
}
defer snmp.Conn.Close()
// Get all returned PDU's returned by SNMP query
getResults := func(pdu gosnmp.SnmpPDU) error {
pdus = append(pdus, pdu)
return nil
}
// Do walk
if bulk && s.Ver != 1 {
err = snmp.BulkWalk(oid, getResults)
} else {
err = snmp.Walk(oid, getResults)
}
if err != nil {
return out, fmt.Errorf("%s walk %v - %s", s.Host, oid, err)
} else if pdus == nil {
return out, fmt.Errorf("%s walk %v - no results", s.Host, oid)
}
// Make formatted output
for _, p := range pdus {
k, v, err2 := formatValue(p, oid, stripoid)
if err2 != nil {
return out, fmt.Errorf("%s get %v - %s", s.Host, k, err2)
}
out[k] = v
}
return out, err
}
// Return pointer to new SnmpPDU
type SetPDU struct {
Oid string
// Integer, OctetString, IPAddress, Gauge32
Vtype string
Value interface{}
}
// Do SNMP set
func (s *Session) Set(setPdus []SetPDU) (SnmpOut, error) {
var out = SnmpOut{}
var pdus = []gosnmp.SnmpPDU{}
for _, v := range setPdus {
pdu := gosnmp.SnmpPDU{
Name: v.Oid,
Value: v.Value,
}
switch {
case v.Vtype == "Integer":
pdu.Type = 2
case v.Vtype == "OctetString":
pdu.Type = 4
case v.Vtype == "IPAddress":
pdu.Type = 40
case v.Vtype == "Gauge32":
pdu.Type = 42
default:
return out, fmt.Errorf("not supported pdu type - %s", v.Vtype)
}
pdus = append(pdus, pdu)
}
snmp := s.Snmp
if err := snmp.Connect(); err != nil {
return out, fmt.Errorf("%s - %s", s.Host, err)
}
defer snmp.Conn.Close()
// Do set
res, err := snmp.Set(pdus)
if err != nil {
return out, err
} else if res.Error != 0 {
return out, fmt.Errorf("%s set %v - %s", s.Host, pdus, res.Error.String())
}
// Make formatted output
for _, p := range res.Variables {
k, v, err2 := formatValue(p, "", false)
if err2 != nil {
return out, err2
}
out[k] = v
}
return out, nil
}
// Local part
// SNMP guery result value
type snmpValue struct {
Raw interface{}
Vtype string
Counter32 uint64
Counter64 uint64
Gauge32 uint64
Integer int64
IPAddress string
ObjectIdentifier string
Opaque string
OctetString string
OpaqueDouble float64
OpaqueFloat float64
TimeTicks uint64
}
// Format SNMP return value
func formatValue(pdu gosnmp.SnmpPDU, oid string, stripoid bool) (string, snmpValue, error) {
key := pdu.Name
if stripoid {
key = strings.TrimPrefix(pdu.Name, oid+".")
}
value := snmpValue{
Raw: pdu.Value,
Vtype: pdu.Type.String(),
}
err := value.snmpValDecode()
return key, value, err
}
// Decode SNMP return value
func (v *snmpValue) snmpValDecode() error {
// Type assertions
switch v.Vtype {
case "Integer": // 0x02. signed
val, _ := v.Raw.(int)
v.Integer = int64(val)
case "Counter32": // 0x41. unsigned
val, _ := v.Raw.(uint)
v.Counter32 = uint64(val)
case "Counter64": // 0x46. unsigned
val, _ := v.Raw.(uint64)
v.Counter64 = uint64(val)
case "Gauge32": // 0x42. unsigned
val, _ := v.Raw.(uint)
v.Gauge32 = uint64(val)
case "TimeTicks": // 0x43. unsigned
val, _ := v.Raw.(uint32)
v.TimeTicks = uint64(val)
case "ObjectIdentifier": // 0x06
val, _ := v.Raw.(string)
v.ObjectIdentifier = val
case "OctetString": // 0x04
b, _ := v.Raw.([]byte)
v.OctetString = string(b)
case "IPAddress": // 0x40
val, _ := v.Raw.(string)
v.IPAddress = val
case "Opaque": // 0x44. NOT TESTED!
b, _ := v.Raw.([]byte)
v.Opaque = string(b)
case "OpaqueDouble": // 0x79. NOT TESTED!
b, _ := v.Raw.([]byte)
var bits uint64
if len(b) > 4 {
// We'll overflow a uint64 in this case.
return fmt.Errorf("OpaqueDouble too large")
}
bits = binary.BigEndian.Uint64(b)
v.OpaqueDouble = math.Float64frombits(bits)
case "OpaqueFloat": // 0x78. NOT TESTED!
b, _ := v.Raw.([]byte)
var bits uint64
if len(b) > 4 {
// We'll overflow a uint32 in this case.
return fmt.Errorf("OpaqueFloat too large")
}
bits = binary.BigEndian.Uint64(b)
v.OpaqueFloat = math.Float64frombits(bits)
default:
return fmt.Errorf("SNMP error - %v", v.Vtype)
}
return nil
}