forked from k-sone/snmpgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getv2.go
56 lines (48 loc) · 983 Bytes
/
getv2.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
package main
import (
"fmt"
"github.com/k-sone/snmpgo"
)
func main() {
snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
Version: snmpgo.V2c,
Address: "127.0.0.1:161",
Retries: 1,
Community: "public",
})
if err != nil {
// Failed to create snmpgo.SNMP object
fmt.Println(err)
return
}
oids, err := snmpgo.NewOids([]string{
"1.3.6.1.2.1.1.1.0",
"1.3.6.1.2.1.1.2.0",
"1.3.6.1.2.1.1.3.0",
})
if err != nil {
// Failed to parse Oids
fmt.Println(err)
return
}
if err = snmp.Open(); err != nil {
// Failed to open connection
fmt.Println(err)
return
}
defer snmp.Close()
pdu, err := snmp.GetRequest(oids)
if err != nil {
// Failed to request
fmt.Println(err)
return
}
if pdu.ErrorStatus() != snmpgo.NoError {
// Received an error from the agent
fmt.Println(pdu.ErrorStatus(), pdu.ErrorIndex())
}
// get VarBind list
fmt.Println(pdu.VarBinds())
// select a VarBind
fmt.Println(pdu.VarBinds().MatchOid(oids[0]))
}