Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
296 lines (280 loc) · 9.1 KB
/
Copy pathexample_test.go
File metadata and controls
296 lines (280 loc) · 9.1 KB
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
// Copyright 2026 Netdata Inc. 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_test
import (
"context"
"errors"
"fmt"
"log"
"net"
"time"
"github.com/netdata/gosnmp"
)
// A client of an SNMPv2c agent gets two objects. The examples that need an
// agent use the documentation address 192.0.2.1.
func ExampleNewClient() {
client, err := gosnmp.NewClient(gosnmp.Config{
Target: "192.0.2.1",
Community: "public",
Version: gosnmp.Version2c,
Timeout: 2 * time.Second,
Retries: 2,
})
if err != nil {
log.Fatal(err) // the error names the field NewClient rejects
}
// The context bounds each call it is given: here Connect and the Get.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err = client.Connect(ctx); err != nil {
log.Fatal(err)
}
defer client.Close()
result, err := client.Get(ctx, []string{"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.7.0"}) // sysContact, sysServices
if err != nil {
log.Fatal(err)
}
for _, vb := range result.VarBinds {
fmt.Printf("%s = %v: %v\n", vb.Name, vb.Value.Type(), vb.Value)
}
}
// An SNMPv3 client at authPriv: the configuration holds the user's
// passphrases; the client discovers the agent's engine at its first request
// and localizes the keys to it.
func ExampleNewClient_snmpv3() {
client, err := gosnmp.NewClient(gosnmp.Config{
Target: "192.0.2.1",
Version: gosnmp.Version3,
SecurityLevel: gosnmp.AuthPriv,
User: gosnmp.User{
Name: "monitor",
AuthProtocol: gosnmp.SHA256,
AuthPassphrase: "authentication passphrase",
PrivProtocol: gosnmp.AES,
PrivPassphrase: "privacy passphrase",
},
})
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
if err = client.Connect(ctx); err != nil {
log.Fatal(err)
}
defer client.Close()
result, err := client.Get(ctx, []string{"1.3.6.1.2.1.1.5.0"}) // sysName
if errors.Is(err, gosnmp.ErrUnknownEngineID) {
// The agent's engine ID changed: forget the engine and ask again.
client.ForgetEngine()
result, err = client.Get(ctx, []string{"1.3.6.1.2.1.1.5.0"})
}
if err != nil {
log.Fatal(err)
}
for _, vb := range result.VarBinds {
fmt.Printf("%s = %v\n", vb.Name, vb.Value)
}
}
// A failed request returns no message: its error tells whether the agent
// answered, and how.
func ExampleClient_Get() {
client, err := gosnmp.NewClient(gosnmp.Config{Target: "192.0.2.1", Community: "public", Version: gosnmp.Version2c})
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
if err = client.Connect(ctx); err != nil {
log.Fatal(err)
}
defer client.Close()
result, err := client.Get(ctx, []string{"1.3.6.1.2.1.1.3.0"}) // sysUpTime
var status *gosnmp.StatusError
switch {
case err == nil:
// An agent without the object answers NoSuchObject or NoSuchInstance
// in its place, without an error: check each value's type.
for _, vb := range result.VarBinds {
if ticks, ok := vb.Value.Uint32(); ok && vb.Value.Type() == gosnmp.TimeTicks {
fmt.Println("uptime:", time.Duration(ticks)*10*time.Millisecond)
} else {
fmt.Printf("%s: %v\n", vb.Name, vb.Value.Type())
}
}
case errors.As(err, &status):
// A Response with an error-status: the agent answered.
fmt.Printf("the agent answered %v for varbind %d\n", status.Status, status.Index)
case errors.Is(err, gosnmp.ErrAnswered):
// An SNMPv3 Report (of an SNMPv3 client), whose counter's category
// the error matches.
fmt.Println("the agent answered:", err)
default:
// No answer: a timeout, the context, a transport error.
fmt.Println("no answer:", err)
}
}
// A walk returns the subtree's values and how it ended; a walk that fails
// returns the values before its error.
func ExampleClient_BulkWalkAll() {
client, err := gosnmp.NewClient(gosnmp.Config{Target: "192.0.2.1", Community: "public", Version: gosnmp.Version2c})
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
if err = client.Connect(ctx); err != nil {
log.Fatal(err)
}
defer client.Close()
// The interfaces table, 10 objects per GETBULK request (0 sends 50).
values, end, err := client.BulkWalkAll(ctx, "1.3.6.1.2.1.2.2", 10)
for _, vb := range values {
fmt.Printf("%s = %v\n", vb.Name, vb.Value)
}
if err != nil {
log.Fatal(err)
}
// The end: past the subtree, an exception value such as EndOfMibView, or
// the Get of a root that is a leaf, whose NoSuchObject or NoSuchInstance
// tells an object the agent lacks from an instance it lacks.
fmt.Printf("ended by %v at %s\n", end.Reason, end.VarBind.Name)
}
// Each SMI type has one Go type, read through its accessor.
func ExampleValue() {
values := []gosnmp.Value{
gosnmp.Counter32Value(4294967295),
gosnmp.Counter64Value(18446744073709551615),
gosnmp.OctetStringValue([]byte{0x00, 0x1b, 0x21}),
gosnmp.NoSuchInstanceValue(),
}
for _, v := range values {
if n, ok := v.Uint64(); ok {
fmt.Printf("%v: %d\n", v.Type(), n)
} else if b, ok := v.Bytes(); ok {
fmt.Printf("%v: %x\n", v.Type(), b)
} else {
fmt.Printf("%v: no value (%v)\n", v.Type(), v)
}
}
// Output:
// Counter32: 4294967295
// Counter64: 18446744073709551615
// OctetString: 001b21
// NoSuchInstance: no value (<nil>)
}
// A receiver decodes a datagram the caller read: here an SNMPv2c trap that a
// client encodes.
func ExampleReceiver() {
sender, err := gosnmp.NewClient(gosnmp.Config{Version: gosnmp.Version2c, Community: "public"})
if err != nil {
log.Fatal(err)
}
datagram, err := sender.EncodeMessage(gosnmp.SNMPv2Trap, []gosnmp.VarBind{
// sysUpTime.0, then snmpTrapOID.0: linkDown.
{Name: ".1.3.6.1.2.1.1.3.0", Value: gosnmp.TimeTicksValue(12345)},
{Name: ".1.3.6.1.6.3.1.1.4.1.0", Value: gosnmp.ObjectIdentifierValue(".1.3.6.1.6.3.1.1.5.3")},
}, 0, 0)
if err != nil {
log.Fatal(err)
}
receiver, err := gosnmp.NewReceiver(gosnmp.ReceiverConfig{})
if err != nil {
log.Fatal(err)
}
notification, response, err := receiver.Receive(datagram)
if err != nil {
log.Fatal(err)
}
m := notification.Message
fmt.Println(m.Version, m.PDUType, m.Community)
for _, vb := range m.VarBinds {
fmt.Printf("%s = %v: %v\n", vb.Name, vb.Value.Type(), vb.Value)
}
fmt.Println("response to send:", response != nil)
// Output:
// 2c SNMPv2Trap public
// .1.3.6.1.2.1.1.3.0 = TimeTicks: 12345
// .1.3.6.1.6.3.1.1.4.1.0 = ObjectIdentifier: .1.3.6.1.6.3.1.1.5.3
// response to send: false
}
// A receiver's read loop: the caller owns the socket, checks what the library
// does not (the PDU type and the community) and sends the answers.
func ExampleReceiver_Receive() {
// The receiver's own engine, the authoritative engine of the informs it
// receives: the caller keeps the engine ID and the boots across restarts
// and raises the boots at each (RFC 3414 section 2.2.2).
localEngineID := []byte{0x80, 0x00, 0x1f, 0x88, 0x04, 't', 'r', 'a', 'p', 's'}
// raiseBoots stands for the caller's store: it loads the boots of the
// last start, adds one, stores and returns them.
raiseBoots := func() uint32 { return 1 }
user := gosnmp.User{
Name: "monitor",
AuthProtocol: gosnmp.SHA256,
AuthPassphrase: "authentication passphrase",
PrivProtocol: gosnmp.AES,
PrivPassphrase: "privacy passphrase",
}
receiver, err := gosnmp.NewReceiver(gosnmp.ReceiverConfig{
EngineID: localEngineID,
EngineBoots: raiseBoots(),
// Informs name the receiver's engine; an agent's traps name its own.
Users: []gosnmp.EngineUser{
{EngineID: localEngineID, User: user},
{EngineID: []byte{0x80, 0x00, 0x1f, 0x88, 0x04, 'a', 'g', 'e', 'n', 't'}, User: user},
},
})
if err != nil {
log.Fatal(err)
}
conn, err := net.ListenUDP("udp", &net.UDPAddr{Port: 162})
if err != nil {
log.Fatal(err)
}
defer conn.Close()
// A Report goes to a source anyone can forge and can be larger than the
// datagram it answers: send at most reportsPerSecond of them in each
// one-second window.
const reportsPerSecond = 10
var reportWindow time.Time
var reportsSent int
reportAllowed := func() bool {
if now := time.Now(); now.Sub(reportWindow) >= time.Second {
reportWindow, reportsSent = now, 0
}
reportsSent++
return reportsSent <= reportsPerSecond
}
buf := make([]byte, 65535)
for {
n, from, err := conn.ReadFromUDP(buf)
if err != nil {
log.Fatal(err)
}
notification, response, err := receiver.Receive(buf[:n])
if err != nil {
// A rejected SNMPv3 message may get a Report that lets its sender
// discover the engine or its time: the caller decides whether to
// send it, and how often.
if response != nil && reportAllowed() {
_, _ = conn.WriteToUDP(response, from)
}
log.Printf("rejected a datagram from %v: %v", from, err)
continue
}
m := ¬ification.Message
switch m.PDUType {
case gosnmp.Trap, gosnmp.SNMPv2Trap, gosnmp.InformRequest:
default:
continue
}
if m.Version != gosnmp.Version3 && m.Community != "public" {
continue
}
if response != nil {
_, _ = conn.WriteToUDP(response, from) // an inform's Response
}
// The values alias buf: use or copy them before the next read.
for _, vb := range m.VarBinds {
fmt.Printf("%v %s = %v\n", from, vb.Name, vb.Value)
}
}
}