forked from rosspeoples/go-snmplib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.go
133 lines (115 loc) · 3.04 KB
/
example.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
package snmplib
import (
"fmt"
"time"
)
func DoGetTableTest(target string) {
community := "public"
version := SNMPv2c
oid := MustParseOid(".1.3.6.1.4.1.2636.3.2.3.1.20")
fmt.Printf("Contacting %v %v %v\n", target, community, version)
wsnmp, err := NewSNMP(target, community, version, 2*time.Second, 5)
defer wsnmp.Close()
if err != nil {
fmt.Printf("Error creating wsnmp => %v\n", wsnmp)
return
}
table, err := wsnmp.GetTable(oid)
if err != nil {
fmt.Printf("Error getting table => %v\n", wsnmp)
return
}
for k, v := range table {
fmt.Printf("%v => %v\n", k, v)
}
}
func DoWalkTest(target string) {
community := "monitorhcm"
version := SNMPv2c
oid := MustParseOid(".1.3.6.1.2.1.1")
oid0 := oid
fmt.Printf("Contacting %v %v %v\n", target, community, version)
wsnmp, err := NewSNMP(target, community, version, 2*time.Second, 5)
if err != nil {
fmt.Printf("Error creating wsnmp => %v\n", wsnmp)
return
}
defer wsnmp.Close()
for {
result_oid, val, err := wsnmp.GetNext(oid)
if err != nil {
fmt.Printf("GetNext error => %v\n", err)
return
}
fmt.Printf("GetNext(%v, %v, %v, %v) => %s, %v\n", target, community, version, oid, result_oid, val)
oid = *result_oid
if !oid.Within(oid0) {
break
}
}
}
func DoWalkTestV3(target string, oidstr, username, authAlg, authKey, privAlg, privKey string) {
oid := MustParseOid(oidstr)
oid0 := oid
fmt.Printf("Contacting %v using SNMP v3\n", target)
wsnmp, err := NewSNMPv3(target, username, authAlg, authKey, privAlg, privKey, 2*time.Second, 2)
if err != nil {
fmt.Printf("Error creating wsnmp => %v\n", wsnmp)
return
}
defer wsnmp.Close()
wsnmp.Discover()
for {
result_oid, val, err := wsnmp.GetNextV3(oid)
if err != nil {
fmt.Printf("GetNext error => %v\n", err)
return
}
fmt.Printf("GetNext(%v, %v) => %s, %v\n", target, oid, result_oid, val)
oid = *result_oid
if !oid.Within(oid0) {
break
}
}
}
func DoGetTest(target string) {
community := "public"
version := SNMPv2c
oids := []Oid{
MustParseOid(".1.3.6.1.2.1.1.1.0"),
MustParseOid(".1.3.6.1.2.1.1.2.0"),
MustParseOid(".1.3.6.1.2.1.2.1.0"),
}
wsnmp, err := NewSNMP(target, community, version, 2*time.Second, 5)
defer wsnmp.Close()
if err != nil {
fmt.Printf("Error creating wsnmp => %v\n", wsnmp)
return
}
for _, oid := range oids {
val, err := wsnmp.Get(oid)
fmt.Printf("Getting %v\n", oid)
if err != nil {
fmt.Printf("Get error => %v\n", wsnmp)
return
}
fmt.Printf("Get(%v, %v, %v, %v) => %v\n", target, community, version, oid, val)
}
}
func DoGetTestV3(target string, oidstr, username, authAlg, authKey, privAlg, privKey string) {
oid := MustParseOid(oidstr)
fmt.Printf("Contacting %v using SNMP v3\n", target)
wsnmp, err := NewSNMPv3(target, username, authAlg, authKey, privAlg, privKey, 2*time.Second, 2)
if err != nil {
fmt.Printf("Error creating wsnmp => %v\n", wsnmp)
return
}
defer wsnmp.Close()
wsnmp.Discover()
val, err := wsnmp.GetV3(oid)
if err != nil {
fmt.Printf("GetV3 error => %v\n", err)
return
}
fmt.Printf("GetV3(%v) => %v\n", oid, val)
}