This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
snmp.go
182 lines (152 loc) · 4.63 KB
/
snmp.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
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
// +build linux
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2016 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package snmp
import (
"fmt"
"strings"
"time"
"github.com/intelsdi-x/snap-plugin-collector-snmp/collector/configReader"
"github.com/k-sone/snmpgo"
)
func NewHandler(agentConfig configReader.SnmpAgent) (*snmpgo.SNMP, error) {
handler, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
Version: getSNMPVersion(agentConfig.SnmpVersion),
Network: agentConfig.Network,
Address: agentConfig.Address,
Timeout: time.Duration(agentConfig.Timeout) * time.Second,
Retries: agentConfig.Retries,
Community: agentConfig.Community,
UserName: agentConfig.UserName,
SecurityLevel: getSNMPSecurityLevel(agentConfig.SecurityLevel),
AuthPassword: agentConfig.AuthPassword,
AuthProtocol: getSNMPAuthProtocol(agentConfig.AuthProtocol),
PrivPassword: agentConfig.PrivPassword,
PrivProtocol: getPrivProtocol(agentConfig.PrivProtocol),
SecurityEngineId: agentConfig.SecurityEngineId,
ContextEngineId: agentConfig.ContextEngineId,
ContextName: agentConfig.ContextName,
})
if err != nil {
return nil, err
}
return handler, nil
}
func ReadElements(handler *snmpgo.SNMP, oid string, mode string) ([]*snmpgo.VarBind, error) {
//results received through SNMP requests
results := []*snmpgo.VarBind{}
if err := handler.Open(); err != nil {
// Failed to open connection
return results, err
}
//get elements in node OID
nodeOid := strings.Trim(oid, ".")
oidParts := strings.Split(nodeOid, ".")
//get length of node OID (used to stop reading in table and walk modes)
nodeOIDLength := len(oidParts)
//previous OID (used to stop reading in table and walk modes)
var prevOid string
//loop through one node of MIB
for {
oids, err := snmpgo.NewOids([]string{oid})
if err != nil {
// Failed to parse Oids
return results, err
}
var pdu snmpgo.Pdu
if mode == configReader.ModeSingle {
pdu, err = handler.GetRequest(oids)
} else {
pdu, err = handler.GetNextRequest(oids)
}
if err != nil {
// Failed to request
return results, err
}
if pdu.ErrorStatus() != snmpgo.NoError {
// Received an error from the agent
return results, fmt.Errorf("Received an error from the SNMP agent: %v", pdu.ErrorStatus())
}
if len(pdu.VarBinds()) != 1 {
return results, fmt.Errorf("Unaccepted number of results, received %v results", len(pdu.VarBinds()))
}
// select a VarBind
result := pdu.VarBinds()[0]
if mode == configReader.ModeSingle {
results = append(results, result)
break
} else {
oid = result.Oid.String()
//get current elements in node OID
currOidParts := strings.Split(strings.Trim(oid, "."), ".")
// if length of new oid is lower then it is the another node
if len(currOidParts) < nodeOIDLength {
break
}
currNodeOid := strings.Join(currOidParts[:nodeOIDLength], ".")
//check if there is a new element to read
if nodeOid != currNodeOid || prevOid == oid ||
(mode == configReader.ModeTable && (len(oidParts)+1) != len(currOidParts)) {
break
}
prevOid = oid
results = append(results, result)
}
}
return results, nil
}
func getSNMPVersion(s string) snmpgo.SNMPVersion {
var snmpVersion snmpgo.SNMPVersion
switch s {
case "v1":
snmpVersion = snmpgo.V1
case "v2c":
snmpVersion = snmpgo.V2c
case "v3":
snmpVersion = snmpgo.V3
}
return snmpVersion
}
func getSNMPSecurityLevel(s string) snmpgo.SecurityLevel {
var securitylevel snmpgo.SecurityLevel
switch s {
case "NoAuthNoPriv":
securitylevel = snmpgo.NoAuthNoPriv
case "AuthNoPriv":
securitylevel = snmpgo.AuthNoPriv
case "AuthPriv":
securitylevel = snmpgo.AuthPriv
}
return securitylevel
}
func getSNMPAuthProtocol(s string) snmpgo.AuthProtocol {
var authProtocol snmpgo.AuthProtocol
switch s {
case "MD5":
authProtocol = snmpgo.Md5
case "SHA":
authProtocol = snmpgo.Sha
}
return authProtocol
}
func getPrivProtocol(s string) snmpgo.PrivProtocol {
var privProtocol snmpgo.PrivProtocol
switch s {
case "DES":
privProtocol = snmpgo.Des
case "AES":
privProtocol = snmpgo.Aes
}
return privProtocol
}