gosnmp
/
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
9package gosnmp
10
11import (
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
20type 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.
28Connect() error
29
30// ConnectIPv4 connects using IPv4
31ConnectIPv4() error
32
33// ConnectIPv6 connects using IPv6
34ConnectIPv6() error
35
36// Get sends an SNMP GET request
37Get(oids []string) (result *SnmpPacket, err error)
38
39// GetBulk sends an SNMP GETBULK request
40GetBulk(oids []string, nonRepeaters uint8, maxRepetitions uint32) (result *SnmpPacket, err error)
41
42// GetNext sends an SNMP GETNEXT request
43GetNext(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.
50Walk(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.
54WalkAll(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.
60BulkWalk(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.
64BulkWalkAll(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.
76SendTrap(trap SnmpTrap) (result *SnmpPacket, err error)
77
78// UnmarshalTrap unpacks the SNMP Trap.
79UnmarshalTrap(trap []byte, useResponseSecurityParameters bool) (result *SnmpPacket, err error)
80
81// Set sends an SNMP SET request
82Set(pdus []SnmpPDU) (result *SnmpPacket, err error)
83
84// Check makes checking errors easy, so they actually get a minimal check
85Check(err error)
86
87// Close closes the connection
88Close() error
89
90// Target gets the Target
91Target() string
92
93// SetTarget sets the Target
94SetTarget(target string)
95
96// Port gets the Port
97Port() uint16
98
99// SetPort sets the Port
100SetPort(port uint16)
101
102// Community gets the Community
103Community() string
104
105// SetCommunity sets the Community
106SetCommunity(community string)
107
108// Version gets the Version
109Version() SnmpVersion
110
111// SetVersion sets the Version
112SetVersion(version SnmpVersion)
113
114// Timeout gets the Timeout
115Timeout() time.Duration
116
117// SetTimeout sets the Timeout
118SetTimeout(timeout time.Duration)
119
120// Retries gets the Retries
121Retries() int
122
123// SetRetries sets the Retries
124SetRetries(retries int)
125
126// GetExponentialTimeout gets the ExponentialTimeout
127GetExponentialTimeout() bool
128
129// SetExponentialTimeout sets the ExponentialTimeout
130SetExponentialTimeout(value bool)
131
132// Logger gets the Logger
133Logger() Logger
134
135// SetLogger sets the Logger
136SetLogger(logger Logger)
137
138// MaxOids gets the MaxOids
139MaxOids() int
140
141// SetMaxOids sets the MaxOids
142SetMaxOids(maxOids int)
143
144// MaxRepetitions gets the maxRepetitions
145MaxRepetitions() uint32
146
147// SetMaxRepetitions sets the maxRepetitions
148SetMaxRepetitions(maxRepetitions uint32)
149
150// NonRepeaters gets the nonRepeaters
151NonRepeaters() int
152
153// SetNonRepeaters sets the nonRepeaters
154SetNonRepeaters(nonRepeaters int)
155
156// MsgFlags gets the MsgFlags
157MsgFlags() SnmpV3MsgFlags
158
159// SetMsgFlags sets the MsgFlags
160SetMsgFlags(msgFlags SnmpV3MsgFlags)
161
162// SecurityModel gets the SecurityModel
163SecurityModel() SnmpV3SecurityModel
164
165// SetSecurityModel sets the SecurityModel
166SetSecurityModel(securityModel SnmpV3SecurityModel)
167
168// SecurityParameters gets the SecurityParameters
169SecurityParameters() SnmpV3SecurityParameters
170
171// SetSecurityParameters sets the SecurityParameters
172SetSecurityParameters(securityParameters SnmpV3SecurityParameters)
173
174// ContextEngineID gets the ContextEngineID
175ContextEngineID() string
176
177// SetContextEngineID sets the ContextEngineID
178SetContextEngineID(contextEngineID string)
179
180// ContextName gets the ContextName
181ContextName() string
182
183// SetContextName sets the ContextName
184SetContextName(contextName string)
185}
186
187// snmpHandler is a wrapper around gosnmp
188type snmpHandler struct {
189GoSNMP
190}
191
192// NewHandler creates a new Handler using gosnmp
193func NewHandler() Handler {
194return &snmpHandler{
195GoSNMP{
196Port: Default.Port,
197Community: Default.Community,
198Version: Default.Version,
199Timeout: Default.Timeout,
200Retries: Default.Retries,
201MaxOids: Default.MaxOids,
202},
203}
204}
205
206func (x *snmpHandler) Target() string {
207// not x.Target because it would reference function Target
208return x.GoSNMP.Target
209}
210
211func (x *snmpHandler) SetTarget(target string) {
212x.GoSNMP.Target = target
213}
214
215func (x *snmpHandler) Port() uint16 {
216return x.GoSNMP.Port
217}
218
219func (x *snmpHandler) SetPort(port uint16) {
220x.GoSNMP.Port = port
221}
222
223func (x *snmpHandler) Community() string {
224return x.GoSNMP.Community
225}
226
227func (x *snmpHandler) SetCommunity(community string) {
228x.GoSNMP.Community = community
229}
230
231func (x *snmpHandler) Version() SnmpVersion {
232return x.GoSNMP.Version
233}
234
235func (x *snmpHandler) SetVersion(version SnmpVersion) {
236x.GoSNMP.Version = version
237}
238
239func (x *snmpHandler) Timeout() time.Duration {
240return x.GoSNMP.Timeout
241}
242
243func (x *snmpHandler) SetTimeout(timeout time.Duration) {
244x.GoSNMP.Timeout = timeout
245}
246
247func (x *snmpHandler) Retries() int {
248return x.GoSNMP.Retries
249}
250
251func (x *snmpHandler) SetRetries(retries int) {
252x.GoSNMP.Retries = retries
253}
254
255func (x *snmpHandler) GetExponentialTimeout() bool {
256return x.GoSNMP.ExponentialTimeout
257}
258
259func (x *snmpHandler) SetExponentialTimeout(value bool) {
260x.GoSNMP.ExponentialTimeout = value
261}
262
263func (x *snmpHandler) Logger() Logger {
264return x.GoSNMP.Logger
265}
266
267func (x *snmpHandler) SetLogger(logger Logger) {
268x.GoSNMP.Logger = logger
269}
270
271func (x *snmpHandler) MaxOids() int {
272return x.GoSNMP.MaxOids
273}
274
275func (x *snmpHandler) SetMaxOids(maxOids int) {
276x.GoSNMP.MaxOids = maxOids
277}
278
279func (x *snmpHandler) MaxRepetitions() uint32 {
280return (x.GoSNMP.MaxRepetitions & 0x7FFFFFFF)
281}
282
283// SetMaxRepetitions wraps to 0 at max int32.
284func (x *snmpHandler) SetMaxRepetitions(maxRepetitions uint32) {
285x.GoSNMP.MaxRepetitions = (maxRepetitions & 0x7FFFFFFF)
286}
287
288func (x *snmpHandler) NonRepeaters() int {
289return x.GoSNMP.NonRepeaters
290}
291
292func (x *snmpHandler) SetNonRepeaters(nonRepeaters int) {
293x.GoSNMP.NonRepeaters = nonRepeaters
294}
295
296func (x *snmpHandler) MsgFlags() SnmpV3MsgFlags {
297return x.GoSNMP.MsgFlags
298}
299
300func (x *snmpHandler) SetMsgFlags(msgFlags SnmpV3MsgFlags) {
301x.GoSNMP.MsgFlags = msgFlags
302}
303
304func (x *snmpHandler) SecurityModel() SnmpV3SecurityModel {
305return x.GoSNMP.SecurityModel
306}
307
308func (x *snmpHandler) SetSecurityModel(securityModel SnmpV3SecurityModel) {
309x.GoSNMP.SecurityModel = securityModel
310}
311
312func (x *snmpHandler) SecurityParameters() SnmpV3SecurityParameters {
313return x.GoSNMP.SecurityParameters
314}
315
316func (x *snmpHandler) SetSecurityParameters(securityParameters SnmpV3SecurityParameters) {
317x.GoSNMP.SecurityParameters = securityParameters
318}
319
320func (x *snmpHandler) ContextEngineID() string {
321return x.GoSNMP.ContextEngineID
322}
323
324func (x *snmpHandler) SetContextEngineID(contextEngineID string) {
325x.GoSNMP.ContextEngineID = contextEngineID
326}
327
328func (x *snmpHandler) ContextName() string {
329return x.GoSNMP.ContextName
330}
331
332func (x *snmpHandler) SetContextName(contextName string) {
333x.GoSNMP.ContextName = contextName
334}
335
336func (x *snmpHandler) Close() error {
337// not x.Conn for consistency
338return x.GoSNMP.Conn.Close()
339}
340