gosnmp

Форк
0
/
interface.go 
339 строк · 8.7 Кб
1
// Copyright 2012 The GoSNMP Authors. All rights reserved.  Use of this
2
// source code is governed by a BSD-style license that can be found in the
3
// LICENSE file.
4

5
// Copyright 2009 The Go Authors. All rights reserved.
6
// Use of this source code is governed by a BSD-style
7
// license that can be found in the LICENSE file.
8

9
package gosnmp
10

11
import (
12
	"time"
13
)
14

15
//go:generate mockgen --destination gosnmp_mock.go --package=gosnmp --source interface.go
16

17
// Handler is a GoSNMP interface
18
//
19
// Handler is provided to assist with testing using mocks
20
type Handler interface {
21
	// Connect creates and opens a socket. Because UDP is a connectionless
22
	// protocol, you won't know if the remote host is responding until you send
23
	// packets. And if the host is regularly disappearing and reappearing, you won't
24
	// know if you've only done a Connect().
25
	//
26
	// For historical reasons (ie this is part of the public API), the method won't
27
	// be renamed.
28
	Connect() error
29

30
	// ConnectIPv4 connects using IPv4
31
	ConnectIPv4() error
32

33
	// ConnectIPv6 connects using IPv6
34
	ConnectIPv6() error
35

36
	// Get sends an SNMP GET request
37
	Get(oids []string) (result *SnmpPacket, err error)
38

39
	// GetBulk sends an SNMP GETBULK request
40
	GetBulk(oids []string, nonRepeaters uint8, maxRepetitions uint32) (result *SnmpPacket, err error)
41

42
	// GetNext sends an SNMP GETNEXT request
43
	GetNext(oids []string) (result *SnmpPacket, err error)
44

45
	// Walk retrieves a subtree of values using GETNEXT - a request is made for each
46
	// value, unlike BulkWalk which does this operation in batches. As the tree is
47
	// walked walkFn is called for each new value. The function immediately returns
48
	// an error if either there is an underlaying SNMP error (e.g. GetNext fails),
49
	// or if walkFn returns an error.
50
	Walk(rootOid string, walkFn WalkFunc) error
51

52
	// WalkAll is similar to Walk but returns a filled array of all values rather
53
	// than using a callback function to stream results.
54
	WalkAll(rootOid string) (results []SnmpPDU, err error)
55

56
	// BulkWalk retrieves a subtree of values using GETBULK. As the tree is
57
	// walked walkFn is called for each new value. The function immediately returns
58
	// an error if either there is an underlaying SNMP error (e.g. GetBulk fails),
59
	// or if walkFn returns an error.
60
	BulkWalk(rootOid string, walkFn WalkFunc) error
61

62
	// BulkWalkAll is similar to BulkWalk but returns a filled array of all values
63
	// rather than using a callback function to stream results.
64
	BulkWalkAll(rootOid string) (results []SnmpPDU, err error)
65

66
	// SendTrap sends a SNMP Trap (v2c/v3 only)
67
	//
68
	// pdus[0] can a pdu of Type TimeTicks (with the desired uint32 epoch
69
	// time).  Otherwise a TimeTicks pdu will be prepended, with time set to
70
	// now. This mirrors the behaviour of the Net-SNMP command-line tools.
71
	//
72
	// SendTrap doesn't wait for a return packet from the NMS (Network
73
	// Management Station).
74
	//
75
	// See also Listen() and examples for creating an NMS.
76
	SendTrap(trap SnmpTrap) (result *SnmpPacket, err error)
77

78
	// UnmarshalTrap unpacks the SNMP Trap.
79
	UnmarshalTrap(trap []byte, useResponseSecurityParameters bool) (result *SnmpPacket, err error)
80

81
	// Set sends an SNMP SET request
82
	Set(pdus []SnmpPDU) (result *SnmpPacket, err error)
83

84
	// Check makes checking errors easy, so they actually get a minimal check
85
	Check(err error)
86

87
	// Close closes the connection
88
	Close() error
89

90
	// Target gets the Target
91
	Target() string
92

93
	// SetTarget sets the Target
94
	SetTarget(target string)
95

96
	// Port gets the Port
97
	Port() uint16
98

99
	// SetPort sets the Port
100
	SetPort(port uint16)
101

102
	// Community gets the Community
103
	Community() string
104

105
	// SetCommunity sets the Community
106
	SetCommunity(community string)
107

108
	// Version gets the Version
109
	Version() SnmpVersion
110

111
	// SetVersion sets the Version
112
	SetVersion(version SnmpVersion)
113

114
	// Timeout gets the Timeout
115
	Timeout() time.Duration
116

117
	// SetTimeout sets the Timeout
118
	SetTimeout(timeout time.Duration)
119

120
	// Retries gets the Retries
121
	Retries() int
122

123
	// SetRetries sets the Retries
124
	SetRetries(retries int)
125

126
	// GetExponentialTimeout gets the ExponentialTimeout
127
	GetExponentialTimeout() bool
128

129
	// SetExponentialTimeout sets the ExponentialTimeout
130
	SetExponentialTimeout(value bool)
131

132
	// Logger gets the Logger
133
	Logger() Logger
134

135
	// SetLogger sets the Logger
136
	SetLogger(logger Logger)
137

138
	// MaxOids gets the MaxOids
139
	MaxOids() int
140

141
	// SetMaxOids sets the MaxOids
142
	SetMaxOids(maxOids int)
143

144
	// MaxRepetitions gets the maxRepetitions
145
	MaxRepetitions() uint32
146

147
	// SetMaxRepetitions sets the maxRepetitions
148
	SetMaxRepetitions(maxRepetitions uint32)
149

150
	// NonRepeaters gets the nonRepeaters
151
	NonRepeaters() int
152

153
	// SetNonRepeaters sets the nonRepeaters
154
	SetNonRepeaters(nonRepeaters int)
155

156
	// MsgFlags gets the MsgFlags
157
	MsgFlags() SnmpV3MsgFlags
158

159
	// SetMsgFlags sets the MsgFlags
160
	SetMsgFlags(msgFlags SnmpV3MsgFlags)
161

162
	// SecurityModel gets the SecurityModel
163
	SecurityModel() SnmpV3SecurityModel
164

165
	// SetSecurityModel sets the SecurityModel
166
	SetSecurityModel(securityModel SnmpV3SecurityModel)
167

168
	// SecurityParameters gets the SecurityParameters
169
	SecurityParameters() SnmpV3SecurityParameters
170

171
	// SetSecurityParameters sets the SecurityParameters
172
	SetSecurityParameters(securityParameters SnmpV3SecurityParameters)
173

174
	// ContextEngineID gets the ContextEngineID
175
	ContextEngineID() string
176

177
	// SetContextEngineID sets the ContextEngineID
178
	SetContextEngineID(contextEngineID string)
179

180
	// ContextName gets the ContextName
181
	ContextName() string
182

183
	// SetContextName sets the ContextName
184
	SetContextName(contextName string)
185
}
186

187
// snmpHandler is a wrapper around gosnmp
188
type snmpHandler struct {
189
	GoSNMP
190
}
191

192
// NewHandler creates a new Handler using gosnmp
193
func NewHandler() Handler {
194
	return &snmpHandler{
195
		GoSNMP{
196
			Port:      Default.Port,
197
			Community: Default.Community,
198
			Version:   Default.Version,
199
			Timeout:   Default.Timeout,
200
			Retries:   Default.Retries,
201
			MaxOids:   Default.MaxOids,
202
		},
203
	}
204
}
205

206
func (x *snmpHandler) Target() string {
207
	// not x.Target because it would reference function Target
208
	return x.GoSNMP.Target
209
}
210

211
func (x *snmpHandler) SetTarget(target string) {
212
	x.GoSNMP.Target = target
213
}
214

215
func (x *snmpHandler) Port() uint16 {
216
	return x.GoSNMP.Port
217
}
218

219
func (x *snmpHandler) SetPort(port uint16) {
220
	x.GoSNMP.Port = port
221
}
222

223
func (x *snmpHandler) Community() string {
224
	return x.GoSNMP.Community
225
}
226

227
func (x *snmpHandler) SetCommunity(community string) {
228
	x.GoSNMP.Community = community
229
}
230

231
func (x *snmpHandler) Version() SnmpVersion {
232
	return x.GoSNMP.Version
233
}
234

235
func (x *snmpHandler) SetVersion(version SnmpVersion) {
236
	x.GoSNMP.Version = version
237
}
238

239
func (x *snmpHandler) Timeout() time.Duration {
240
	return x.GoSNMP.Timeout
241
}
242

243
func (x *snmpHandler) SetTimeout(timeout time.Duration) {
244
	x.GoSNMP.Timeout = timeout
245
}
246

247
func (x *snmpHandler) Retries() int {
248
	return x.GoSNMP.Retries
249
}
250

251
func (x *snmpHandler) SetRetries(retries int) {
252
	x.GoSNMP.Retries = retries
253
}
254

255
func (x *snmpHandler) GetExponentialTimeout() bool {
256
	return x.GoSNMP.ExponentialTimeout
257
}
258

259
func (x *snmpHandler) SetExponentialTimeout(value bool) {
260
	x.GoSNMP.ExponentialTimeout = value
261
}
262

263
func (x *snmpHandler) Logger() Logger {
264
	return x.GoSNMP.Logger
265
}
266

267
func (x *snmpHandler) SetLogger(logger Logger) {
268
	x.GoSNMP.Logger = logger
269
}
270

271
func (x *snmpHandler) MaxOids() int {
272
	return x.GoSNMP.MaxOids
273
}
274

275
func (x *snmpHandler) SetMaxOids(maxOids int) {
276
	x.GoSNMP.MaxOids = maxOids
277
}
278

279
func (x *snmpHandler) MaxRepetitions() uint32 {
280
	return (x.GoSNMP.MaxRepetitions & 0x7FFFFFFF)
281
}
282

283
// SetMaxRepetitions wraps to 0 at max int32.
284
func (x *snmpHandler) SetMaxRepetitions(maxRepetitions uint32) {
285
	x.GoSNMP.MaxRepetitions = (maxRepetitions & 0x7FFFFFFF)
286
}
287

288
func (x *snmpHandler) NonRepeaters() int {
289
	return x.GoSNMP.NonRepeaters
290
}
291

292
func (x *snmpHandler) SetNonRepeaters(nonRepeaters int) {
293
	x.GoSNMP.NonRepeaters = nonRepeaters
294
}
295

296
func (x *snmpHandler) MsgFlags() SnmpV3MsgFlags {
297
	return x.GoSNMP.MsgFlags
298
}
299

300
func (x *snmpHandler) SetMsgFlags(msgFlags SnmpV3MsgFlags) {
301
	x.GoSNMP.MsgFlags = msgFlags
302
}
303

304
func (x *snmpHandler) SecurityModel() SnmpV3SecurityModel {
305
	return x.GoSNMP.SecurityModel
306
}
307

308
func (x *snmpHandler) SetSecurityModel(securityModel SnmpV3SecurityModel) {
309
	x.GoSNMP.SecurityModel = securityModel
310
}
311

312
func (x *snmpHandler) SecurityParameters() SnmpV3SecurityParameters {
313
	return x.GoSNMP.SecurityParameters
314
}
315

316
func (x *snmpHandler) SetSecurityParameters(securityParameters SnmpV3SecurityParameters) {
317
	x.GoSNMP.SecurityParameters = securityParameters
318
}
319

320
func (x *snmpHandler) ContextEngineID() string {
321
	return x.GoSNMP.ContextEngineID
322
}
323

324
func (x *snmpHandler) SetContextEngineID(contextEngineID string) {
325
	x.GoSNMP.ContextEngineID = contextEngineID
326
}
327

328
func (x *snmpHandler) ContextName() string {
329
	return x.GoSNMP.ContextName
330
}
331

332
func (x *snmpHandler) SetContextName(contextName string) {
333
	x.GoSNMP.ContextName = contextName
334
}
335

336
func (x *snmpHandler) Close() error {
337
	// not x.Conn for consistency
338
	return x.GoSNMP.Conn.Close()
339
}
340

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.