-
Notifications
You must be signed in to change notification settings - Fork 1
/
snmp.go
298 lines (256 loc) · 7.04 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
// Copyright (c) 2019 Arista Networks, Inc.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package devices
import (
"context"
"errors"
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/aristanetworks/cloudvision-go/device"
"github.com/aristanetworks/cloudvision-go/provider"
psnmp "github.com/aristanetworks/cloudvision-go/provider/snmp"
"github.com/gosnmp/gosnmp"
)
var options = map[string]device.Option{
"a": {
Description: "SNMPv3 authentication protocol",
Pattern: `sha|SHA|md5|MD5`,
},
"A": {
Description: "SNMPv3 authentication key",
},
"address": {
Description: "Hostname or address of device",
Required: true,
},
"port": {
Description: "Device SNMP port to use",
Default: "161",
},
"c": {
Description: "SNMP community string",
},
"l": {
Description: "SNMPv3 security level (noAuthNoPriv|authNoPriv|authPriv)",
Default: "authPriv",
Pattern: `noAuthNoPriv|authNoPriv|authPriv`,
},
"mibs": {
Description: "Comma-separated list of mib files/directories",
Required: true,
},
"pollInterval": {
Description: "Polling interval, with unit suffix (s/m/h)",
Default: "20s",
},
"u": {
Description: "SNMPv3 security name",
},
"v": {
Description: "SNMP version (2c|3)",
Pattern: `2c|3`,
Default: "2c",
},
"x": {
Description: "SNMPv3 privacy protocol",
Pattern: `des|DES|aes|AES`,
},
"X": {
Description: "SNMPv3 privacy key",
},
}
func init() {
device.Register("snmp", newSnmp, options)
}
type snmp struct {
address string
authKey string
authProto string
community string
level string
mibs []string
pollInterval time.Duration
port uint16
privacyKey string
privacyProto string
securityName string
systemID string
version string
v3Params *psnmp.V3Params
v gosnmp.SnmpVersion
snmpProvider provider.GNMIProvider
mgmtIP string
}
// XXX NOTE: For now, we return an error rather than just returning false. We
// may want to rethink that in the future.
func (s *snmp) Alive(ctx context.Context) (bool, error) {
return s.snmpProvider.(*psnmp.Snmp).Alive(ctx)
}
func (s *snmp) DeviceID(ctx context.Context) (string, error) {
if s.systemID != "" {
return s.systemID, nil
}
systemID, err := s.snmpProvider.(*psnmp.Snmp).DeviceID(ctx)
if err != nil {
return "", err
}
s.systemID = systemID
return s.systemID, nil
}
func (s *snmp) Providers() ([]provider.Provider, error) {
return []provider.Provider{s.snmpProvider}, nil
}
func (s *snmp) Type() string {
return ""
}
func (s *snmp) IPAddr(ctx context.Context) (string, error) {
if s.mgmtIP == "" {
// Attempt to resolve management IP
ip, err := net.ResolveIPAddr("ip", s.address)
if err == nil {
s.mgmtIP = ip.String()
}
}
return s.mgmtIP, nil
}
func (s *snmp) validateOptions() error {
if s.pollInterval == 0*time.Second {
return errors.New("poll interval must be greater than 0 seconds")
}
if s.version == "2c" {
if s.community == "" {
return errors.New("community string required for version 2c")
}
return nil
}
if s.securityName == "" {
return errors.New("v3 is configured, so a username is required")
}
if s.level == "authNoPriv" || s.level == "authPriv" {
if s.authProto == "" {
return errors.New("auth is configured, so an authentication " +
"protocol must be specified")
}
if s.authKey == "" {
return errors.New("auth is configured, so an authentication " +
"key must be specified")
}
}
if s.level == "authPriv" {
if s.privacyProto == "" {
return errors.New("privacy is configured, so a privacy " +
"protocol must be specified")
}
if s.privacyKey == "" {
return errors.New("privacy is configured, so a privacy " +
"key must be specified")
}
}
return nil
}
func (s *snmp) formatOptions() (gosnmp.SnmpVersion, *psnmp.V3Params) {
if s.version == "2c" {
return gosnmp.Version2c, nil
}
v3Params := &psnmp.V3Params{
SecurityModel: gosnmp.UserSecurityModel,
UsmParams: &gosnmp.UsmSecurityParameters{},
}
v3Params.UsmParams.UserName = s.securityName
if s.level == "noAuthNoPriv" {
v3Params.Level = gosnmp.NoAuthNoPriv
} else if s.level == "authNoPriv" {
v3Params.Level = gosnmp.AuthNoPriv
} else if s.level == "authPriv" {
v3Params.Level = gosnmp.AuthPriv
}
if strings.ToLower(s.authProto) == "sha" {
v3Params.UsmParams.AuthenticationProtocol = gosnmp.SHA
} else if strings.ToLower(s.authProto) == "md5" {
v3Params.UsmParams.AuthenticationProtocol = gosnmp.MD5
}
if strings.ToLower(s.privacyProto) == "aes" {
v3Params.UsmParams.PrivacyProtocol = gosnmp.AES
} else if strings.ToLower(s.privacyProto) == "des" {
v3Params.UsmParams.PrivacyProtocol = gosnmp.DES
}
v3Params.UsmParams.AuthenticationPassphrase = s.authKey
v3Params.UsmParams.PrivacyPassphrase = s.privacyKey
return gosnmp.Version3, v3Params
}
func (s *snmp) deviceConfigErr(err error) error {
return fmt.Errorf("Configuration error for device %s: %v",
s.address, err)
}
// XXX NOTE: The network operations here could fail on startup, and if
// they do, the error will be passed back to Collector and it will fail.
// Are we OK with this or should we be doing retries?
func newSnmp(ctx context.Context, options map[string]string,
monitor provider.Monitor) (device.Device, error) {
s := &snmp{}
var err error
s.address, err = device.GetAddressOption("address", options)
if err != nil {
return nil, err
}
s.authKey, err = device.GetStringOption("A", options)
if err != nil {
return nil, s.deviceConfigErr(err)
}
s.authProto, err = device.GetStringOption("a", options)
if err != nil {
return nil, s.deviceConfigErr(err)
}
s.community, err = device.GetStringOption("c", options)
if err != nil {
return nil, s.deviceConfigErr(err)
}
s.level, err = device.GetStringOption("l", options)
if err != nil {
return nil, s.deviceConfigErr(err)
}
s.mibs, err = device.GetStringListOption("mibs", options)
if err != nil {
return nil, s.deviceConfigErr(err)
}
s.pollInterval, err = device.GetDurationOption("pollInterval", options)
if err != nil {
return nil, s.deviceConfigErr(err)
}
port, err := device.GetPortOption("port", options)
if err != nil {
return nil, s.deviceConfigErr(err)
}
portint, err := strconv.Atoi(port)
if err != nil {
return nil, s.deviceConfigErr(err)
}
s.port = uint16(portint)
s.privacyKey, err = device.GetStringOption("X", options)
if err != nil {
return nil, s.deviceConfigErr(err)
}
s.privacyProto, err = device.GetStringOption("x", options)
if err != nil {
return nil, s.deviceConfigErr(err)
}
s.securityName, err = device.GetStringOption("u", options)
if err != nil {
return nil, s.deviceConfigErr(err)
}
s.version, err = device.GetStringOption("v", options)
if err != nil {
return nil, s.deviceConfigErr(err)
}
if err := s.validateOptions(); err != nil {
return nil, s.deviceConfigErr(err)
}
s.v, s.v3Params = s.formatOptions()
s.snmpProvider = psnmp.NewSNMPProvider(ctx, s.address, s.port, s.community,
s.pollInterval, s.v, s.v3Params, s.mibs, false, monitor)
return s, nil
}