forked from gosnmp/gosnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
339 lines (253 loc) · 8.72 KB
/
interface.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
// Copyright 2012 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 (
"time"
)
//go:generate mockgen --destination gosnmp_mock.go --package=gosnmp --source interface.go
// Handler is a GoSNMP interface
//
// Handler is provided to assist with testing using mocks
type Handler interface {
// Connect creates and opens a socket. Because UDP is a connectionless
// protocol, you won't know if the remote host is responding until you send
// packets. And if the host is regularly disappearing and reappearing, you won't
// know if you've only done a Connect().
//
// For historical reasons (ie this is part of the public API), the method won't
// be renamed.
Connect() error
// ConnectIPv4 connects using IPv4
ConnectIPv4() error
// ConnectIPv6 connects using IPv6
ConnectIPv6() error
// Get sends an SNMP GET request
Get(oids []string) (result *SnmpPacket, err error)
// GetBulk sends an SNMP GETBULK request
GetBulk(oids []string, nonRepeaters uint8, maxRepetitions uint32) (result *SnmpPacket, err error)
// GetNext sends an SNMP GETNEXT request
GetNext(oids []string) (result *SnmpPacket, err error)
// 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.
Walk(rootOid string, walkFn WalkFunc) error
// WalkAll is similar to Walk but returns a filled array of all values rather
// than using a callback function to stream results.
WalkAll(rootOid string) (results []SnmpPDU, err 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.
BulkWalk(rootOid string, walkFn WalkFunc) error
// BulkWalkAll is similar to BulkWalk but returns a filled array of all values
// rather than using a callback function to stream results.
BulkWalkAll(rootOid string) (results []SnmpPDU, err error)
// SendTrap sends a SNMP Trap (v2c/v3 only)
//
// pdus[0] can a pdu of Type TimeTicks (with the desired uint32 epoch
// time). Otherwise a TimeTicks pdu will be prepended, with time set to
// now. This mirrors the behaviour of the Net-SNMP command-line tools.
//
// SendTrap doesn't wait for a return packet from the NMS (Network
// Management Station).
//
// See also Listen() and examples for creating an NMS.
SendTrap(trap SnmpTrap) (result *SnmpPacket, err error)
// UnmarshalTrap unpacks the SNMP Trap.
UnmarshalTrap(trap []byte, useResponseSecurityParameters bool) (result *SnmpPacket, err error)
// Set sends an SNMP SET request
Set(pdus []SnmpPDU) (result *SnmpPacket, err error)
// Check makes checking errors easy, so they actually get a minimal check
Check(err error)
// Close closes the connection
Close() error
// Target gets the Target
Target() string
// SetTarget sets the Target
SetTarget(target string)
// Port gets the Port
Port() uint16
// SetPort sets the Port
SetPort(port uint16)
// Community gets the Community
Community() string
// SetCommunity sets the Community
SetCommunity(community string)
// Version gets the Version
Version() SnmpVersion
// SetVersion sets the Version
SetVersion(version SnmpVersion)
// Timeout gets the Timeout
Timeout() time.Duration
// SetTimeout sets the Timeout
SetTimeout(timeout time.Duration)
// Retries gets the Retries
Retries() int
// SetRetries sets the Retries
SetRetries(retries int)
// GetExponentialTimeout gets the ExponentialTimeout
GetExponentialTimeout() bool
// SetExponentialTimeout sets the ExponentialTimeout
SetExponentialTimeout(value bool)
// Logger gets the Logger
Logger() Logger
// SetLogger sets the Logger
SetLogger(logger Logger)
// MaxOids gets the MaxOids
MaxOids() int
// SetMaxOids sets the MaxOids
SetMaxOids(maxOids int)
// MaxRepetitions gets the maxRepetitions
MaxRepetitions() uint32
// SetMaxRepetitions sets the maxRepetitions
SetMaxRepetitions(maxRepetitions uint32)
// NonRepeaters gets the nonRepeaters
NonRepeaters() int
// SetNonRepeaters sets the nonRepeaters
SetNonRepeaters(nonRepeaters int)
// MsgFlags gets the MsgFlags
MsgFlags() SnmpV3MsgFlags
// SetMsgFlags sets the MsgFlags
SetMsgFlags(msgFlags SnmpV3MsgFlags)
// SecurityModel gets the SecurityModel
SecurityModel() SnmpV3SecurityModel
// SetSecurityModel sets the SecurityModel
SetSecurityModel(securityModel SnmpV3SecurityModel)
// SecurityParameters gets the SecurityParameters
SecurityParameters() SnmpV3SecurityParameters
// SetSecurityParameters sets the SecurityParameters
SetSecurityParameters(securityParameters SnmpV3SecurityParameters)
// ContextEngineID gets the ContextEngineID
ContextEngineID() string
// SetContextEngineID sets the ContextEngineID
SetContextEngineID(contextEngineID string)
// ContextName gets the ContextName
ContextName() string
// SetContextName sets the ContextName
SetContextName(contextName string)
}
// snmpHandler is a wrapper around gosnmp
type snmpHandler struct {
GoSNMP
}
// NewHandler creates a new Handler using gosnmp
func NewHandler() Handler {
return &snmpHandler{
GoSNMP{
Port: Default.Port,
Community: Default.Community,
Version: Default.Version,
Timeout: Default.Timeout,
Retries: Default.Retries,
MaxOids: Default.MaxOids,
},
}
}
func (x *snmpHandler) Target() string {
// not x.Target because it would reference function Target
return x.GoSNMP.Target
}
func (x *snmpHandler) SetTarget(target string) {
x.GoSNMP.Target = target
}
func (x *snmpHandler) Port() uint16 {
return x.GoSNMP.Port
}
func (x *snmpHandler) SetPort(port uint16) {
x.GoSNMP.Port = port
}
func (x *snmpHandler) Community() string {
return x.GoSNMP.Community
}
func (x *snmpHandler) SetCommunity(community string) {
x.GoSNMP.Community = community
}
func (x *snmpHandler) Version() SnmpVersion {
return x.GoSNMP.Version
}
func (x *snmpHandler) SetVersion(version SnmpVersion) {
x.GoSNMP.Version = version
}
func (x *snmpHandler) Timeout() time.Duration {
return x.GoSNMP.Timeout
}
func (x *snmpHandler) SetTimeout(timeout time.Duration) {
x.GoSNMP.Timeout = timeout
}
func (x *snmpHandler) Retries() int {
return x.GoSNMP.Retries
}
func (x *snmpHandler) SetRetries(retries int) {
x.GoSNMP.Retries = retries
}
func (x *snmpHandler) GetExponentialTimeout() bool {
return x.GoSNMP.ExponentialTimeout
}
func (x *snmpHandler) SetExponentialTimeout(value bool) {
x.GoSNMP.ExponentialTimeout = value
}
func (x *snmpHandler) Logger() Logger {
return x.GoSNMP.Logger
}
func (x *snmpHandler) SetLogger(logger Logger) {
x.GoSNMP.Logger = logger
}
func (x *snmpHandler) MaxOids() int {
return x.GoSNMP.MaxOids
}
func (x *snmpHandler) SetMaxOids(maxOids int) {
x.GoSNMP.MaxOids = maxOids
}
func (x *snmpHandler) MaxRepetitions() uint32 {
return (x.GoSNMP.MaxRepetitions & 0x7FFFFFFF)
}
// SetMaxRepetitions wraps to 0 at max int32.
func (x *snmpHandler) SetMaxRepetitions(maxRepetitions uint32) {
x.GoSNMP.MaxRepetitions = (maxRepetitions & 0x7FFFFFFF)
}
func (x *snmpHandler) NonRepeaters() int {
return x.GoSNMP.NonRepeaters
}
func (x *snmpHandler) SetNonRepeaters(nonRepeaters int) {
x.GoSNMP.NonRepeaters = nonRepeaters
}
func (x *snmpHandler) MsgFlags() SnmpV3MsgFlags {
return x.GoSNMP.MsgFlags
}
func (x *snmpHandler) SetMsgFlags(msgFlags SnmpV3MsgFlags) {
x.GoSNMP.MsgFlags = msgFlags
}
func (x *snmpHandler) SecurityModel() SnmpV3SecurityModel {
return x.GoSNMP.SecurityModel
}
func (x *snmpHandler) SetSecurityModel(securityModel SnmpV3SecurityModel) {
x.GoSNMP.SecurityModel = securityModel
}
func (x *snmpHandler) SecurityParameters() SnmpV3SecurityParameters {
return x.GoSNMP.SecurityParameters
}
func (x *snmpHandler) SetSecurityParameters(securityParameters SnmpV3SecurityParameters) {
x.GoSNMP.SecurityParameters = securityParameters
}
func (x *snmpHandler) ContextEngineID() string {
return x.GoSNMP.ContextEngineID
}
func (x *snmpHandler) SetContextEngineID(contextEngineID string) {
x.GoSNMP.ContextEngineID = contextEngineID
}
func (x *snmpHandler) ContextName() string {
return x.GoSNMP.ContextName
}
func (x *snmpHandler) SetContextName(contextName string) {
x.GoSNMP.ContextName = contextName
}
func (x *snmpHandler) Close() error {
// not x.Conn for consistency
return x.GoSNMP.Conn.Close()
}