-
Notifications
You must be signed in to change notification settings - Fork 48
/
client.go
342 lines (304 loc) · 8.75 KB
/
client.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
package snmpgo
import (
"fmt"
"math"
"net"
"time"
)
// An argument for creating a SNMP Object
type SNMPArguments struct {
Version SNMPVersion // SNMP version to use
Network string // See net.Dial parameter (The default is `udp`)
Address string // See net.Dial parameter
Timeout time.Duration // Request timeout (The default is 5sec)
Retries uint // Number of retries (The default is `0`)
MessageMaxSize int // Maximum size of an SNMP message (The default is `1400`)
Community string // Community (V1 or V2c specific)
UserName string // Security name (V3 specific)
SecurityLevel SecurityLevel // Security level (V3 specific)
AuthPassword string // Authentication protocol pass phrase (V3 specific)
AuthProtocol AuthProtocol // Authentication protocol (V3 specific)
PrivPassword string // Privacy protocol pass phrase (V3 specific)
PrivProtocol PrivProtocol // Privacy protocol (V3 specific)
SecurityEngineId string // Security engine ID (V3 specific)
ContextEngineId string // Context engine ID (V3 specific)
ContextName string // Context name (V3 specific)
authEngineBoots int
authEngineTime int
}
func (a *SNMPArguments) setDefault() {
if a.Network == "" {
a.Network = "udp"
}
if a.Timeout <= 0 {
a.Timeout = timeoutDefault
}
if a.MessageMaxSize == 0 {
a.MessageMaxSize = msgSizeDefault
}
}
func (a *SNMPArguments) validate() error {
if v := a.Version; v != V1 && v != V2c && v != V3 {
return &ArgumentError{
Value: v,
Message: "Unknown SNMP Version",
}
}
// RFC3412 Section 6
if m := a.MessageMaxSize; (m != 0 && m < msgSizeMinimum) || m > math.MaxInt32 {
return &ArgumentError{
Value: m,
Message: fmt.Sprintf("MessageMaxSize is range %d..%d",
msgSizeMinimum, math.MaxInt32),
}
}
if a.Version == V3 {
// RFC3414 Section 5
if l := len(a.UserName); l < 1 || l > 32 {
return &ArgumentError{
Value: a.UserName,
Message: "UserName length is range 1..32",
}
}
if a.SecurityLevel > NoAuthNoPriv {
// RFC3414 Section 11.2
if len(a.AuthPassword) < 8 {
return &ArgumentError{
Value: a.AuthPassword,
Message: "AuthPassword is at least 8 characters in length",
}
}
if p := a.AuthProtocol; p != Md5 && p != Sha {
return &ArgumentError{
Value: a.AuthProtocol,
Message: "Illegal AuthProtocol",
}
}
}
if a.SecurityLevel > AuthNoPriv {
// RFC3414 Section 11.2
if len(a.PrivPassword) < 8 {
return &ArgumentError{
Value: a.PrivPassword,
Message: "PrivPassword is at least 8 characters in length",
}
}
if p := a.PrivProtocol; p != Des && p != Aes {
return &ArgumentError{
Value: a.PrivProtocol,
Message: "Illegal PrivProtocol",
}
}
}
if a.SecurityEngineId != "" {
a.SecurityEngineId = stripHexPrefix(a.SecurityEngineId)
_, err := engineIdToBytes(a.SecurityEngineId)
if err != nil {
return err
}
}
if a.ContextEngineId != "" {
a.ContextEngineId = stripHexPrefix(a.ContextEngineId)
_, err := engineIdToBytes(a.ContextEngineId)
if err != nil {
return err
}
}
}
return nil
}
func (a *SNMPArguments) String() string {
return escape(a)
}
// SNMP Object provides functions for the SNMP Client
type SNMP struct {
conn net.Conn
args *SNMPArguments
engine *snmpEngine
}
// Open a connection
func (s *SNMP) Open() (err error) {
if s.conn != nil {
return
}
err = retry(int(s.args.Retries), func() error {
conn, e := net.DialTimeout(s.args.Network, s.args.Address, s.args.Timeout)
if e == nil {
s.conn = conn
}
return e
})
if err != nil {
return
}
s.engine = newSNMPEngine(s.args)
if err = s.engine.Discover(s); err != nil {
s.Close()
}
return
}
// Close a connection
func (s *SNMP) Close() {
if s.conn != nil {
s.conn.Close()
s.conn = nil
s.engine = nil
}
}
func (s *SNMP) GetRequest(oids Oids) (result Pdu, err error) {
pdu := NewPduWithOids(s.args.Version, GetRequest, oids)
return s.sendPdu(pdu)
}
func (s *SNMP) GetNextRequest(oids Oids) (result Pdu, err error) {
pdu := NewPduWithOids(s.args.Version, GetNextRequest, oids)
return s.sendPdu(pdu)
}
func (s *SNMP) GetBulkRequest(oids Oids, nonRepeaters, maxRepetitions int) (result Pdu, err error) {
if s.args.Version < V2c {
return nil, &ArgumentError{
Value: s.args.Version,
Message: "Unsupported SNMP Version",
}
}
// RFC 3416 Section 3
if nonRepeaters < 0 || nonRepeaters > math.MaxInt32 {
return nil, &ArgumentError{
Value: nonRepeaters,
Message: fmt.Sprintf("NonRepeaters is range %d..%d", 0, math.MaxInt32),
}
}
if maxRepetitions < 0 || maxRepetitions > math.MaxInt32 {
return nil, &ArgumentError{
Value: maxRepetitions,
Message: fmt.Sprintf("NonRepeaters is range %d..%d", 0, math.MaxInt32),
}
}
pdu := NewPduWithOids(s.args.Version, GetBulkRequest, oids)
pdu.SetNonrepeaters(nonRepeaters)
pdu.SetMaxRepetitions(maxRepetitions)
return s.sendPdu(pdu)
}
// This method inquire about OID subtrees by repeatedly using GetBulkRequest.
// Returned PDU contains the varbind list of all subtrees.
// however, if the ErrorStatus of PDU is not the NoError, return only the last query result.
func (s *SNMP) GetBulkWalk(oids Oids, nonRepeaters, maxRepetitions int) (result Pdu, err error) {
var nonRepBinds, resBinds VarBinds
oids = append(oids[:nonRepeaters], oids[nonRepeaters:].Sort().UniqBase()...)
reqOids := make(Oids, len(oids))
copy(reqOids, oids)
for len(reqOids) > 0 {
pdu, err := s.GetBulkRequest(reqOids, nonRepeaters, maxRepetitions)
if err != nil {
return nil, err
}
if s := pdu.ErrorStatus(); s != NoError &&
(s != NoSuchName || pdu.ErrorIndex() <= nonRepeaters) {
return pdu, nil
}
varBinds := pdu.VarBinds()
if nonRepeaters > 0 {
nonRepBinds = append(nonRepBinds, varBinds[:nonRepeaters]...)
varBinds = varBinds[nonRepeaters:]
oids = oids[nonRepeaters:]
reqOids = reqOids[nonRepeaters:]
nonRepeaters = 0
}
filled := len(varBinds) == len(reqOids)*maxRepetitions
varBinds = varBinds.Sort().Uniq()
for i, _ := range reqOids {
matched := varBinds.MatchBaseOids(oids[i])
mLength := len(matched)
if mLength == 0 || resBinds.MatchOid(matched[mLength-1].Oid) != nil {
reqOids[i] = nil
continue
}
hasError := false
for _, val := range matched {
switch val.Variable.(type) {
case *NoSucheObject, *NoSucheInstance, *EndOfMibView:
hasError = true
default:
resBinds = append(resBinds, val)
reqOids[i] = val.Oid
}
}
if hasError || (filled && mLength < maxRepetitions) {
reqOids[i] = nil
}
}
// sweep completed oids
for i := len(reqOids) - 1; i >= 0; i-- {
if reqOids[i] == nil {
reqOids = append(reqOids[:i], reqOids[i+1:]...)
oids = append(oids[:i], oids[i+1:]...)
}
}
}
resBinds = append(nonRepBinds, resBinds.Sort().Uniq()...)
return NewPduWithVarBinds(s.args.Version, GetResponse, resBinds), nil
}
func (s *SNMP) V2Trap(varBinds VarBinds) error {
return s.v2trap(SNMPTrapV2, varBinds)
}
// Send trap with the authoritative engine boots and time when used with SNMP V3.
func (s *SNMP) V2TrapWithBootsTime(varBinds VarBinds, eBoots, eTime int) error {
if eBoots < 0 || eBoots > math.MaxInt32 {
return &ArgumentError{
Value: eBoots,
Message: fmt.Sprintf("EngineBoots is range %d..%d", 0, math.MaxInt32),
}
}
if eTime < 0 || eTime > math.MaxInt32 {
return &ArgumentError{
Value: eTime,
Message: fmt.Sprintf("EngineTime is range %d..%d", 0, math.MaxInt32),
}
}
defer func() {
s.args.authEngineBoots = 0
s.args.authEngineTime = 0
}()
s.args.authEngineBoots = eBoots
s.args.authEngineTime = eTime
return s.v2trap(SNMPTrapV2, varBinds)
}
func (s *SNMP) InformRequest(varBinds VarBinds) error {
return s.v2trap(InformRequest, varBinds)
}
func (s *SNMP) v2trap(pduType PduType, varBinds VarBinds) (err error) {
if s.args.Version < V2c {
return &ArgumentError{
Value: s.args.Version,
Message: "Unsupported SNMP Version",
}
}
pdu := NewPduWithVarBinds(s.args.Version, pduType, varBinds)
_, err = s.sendPdu(pdu)
return
}
func (s *SNMP) sendPdu(pdu Pdu) (result Pdu, err error) {
if err = s.Open(); err != nil {
return
}
retry(int(s.args.Retries), func() error {
result, err = s.engine.SendPdu(pdu, s.conn, s.args)
return err
})
return
}
func (s *SNMP) String() string {
if s.conn == nil {
return fmt.Sprintf(`{"conn": false, "args": %s, "engine": null}`, s.args.String())
} else {
return fmt.Sprintf(`{"conn": true, "args": %s, "engine": %s}`,
s.args.String(), s.engine.String())
}
}
// Create a SNMP Object
func NewSNMP(args SNMPArguments) (*SNMP, error) {
if err := args.validate(); err != nil {
return nil, err
}
args.setDefault()
return &SNMP{args: &args}, nil
}