-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
202 lines (168 loc) · 6.07 KB
/
common.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// MIT License
//
// (C) Copyright [2021] Hewlett Packard Enterprise Development LP
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
package snmp_utilities
import (
"errors"
"fmt"
"github.com/k-sone/snmpgo"
"github.com/Cray-HPE/hms-discovery/pkg/switches"
"strconv"
"strings"
)
// The OID which has the model number of the switch
var OIDModelNumber string = "1.3.6.1.2.1.47.1.1.1.1.13.2"
// The OID which maps ifIndexes to human-readable names
var OIDifIndexPortNameMap string = "1.3.6.1.2.1.31.1.1.1.1"
// The OID which maps physical port numbers to ifIndexes
var OIDPortNumberifIndex string = "1.3.6.1.2.1.17.1.4.1.2"
// The OID for the mac address table (with VLANs - should be there on all switches)
var OIDMacAddressesWithVLAN string = "1.3.6.1.2.1.17.7.1.2.2.1.2"
// The OID for the source information for VLANs
var OIDMacAddressSourceWithVLAN string = "1.3.6.1.2.1.17.7.1.2.2.1.3"
// The OID for NON-VLAN mac address table. Only valid if the switch is
// configured with "enable-dot1d-mibwalk" first!
var OIDMACAddressesNoVLAN = "1.3.6.1.2.1.17.4.3.1.2"
// The OID for non-VLAN learned-mac sources. Also only valid if configured
// with "enable-dot1d-mibwalk".
var OIDMacAddressSourceNoVLAN = "1.3.6.1.2.1.17.4.3.1.3"
// OID returned if an authentication fialure occurs.
var OIDAuthFailure string = "1.3.6.1.6.3.15.1.1.5.0"
// OID for querying the full name and version identification of
//a switches software operating-system and networking software.
var OIDSysDescr string = "1.3.6.1.2.1.1.1.0"
func GetSNMPOjbect(managementSwitch switches.ManagementSwitch) (snmp *snmpgo.SNMP, err error) {
// Check that the address ends in a port number (required by goSNMP).
if !strings.Contains(managementSwitch.Address, ":") {
managementSwitch.Address = fmt.Sprintf("%s:161", managementSwitch.Address)
}
var securityLevel snmpgo.SecurityLevel
var authType snmpgo.AuthProtocol
var privType snmpgo.PrivProtocol
if strings.ToLower(managementSwitch.SNMPAuthProtocol) == "none" {
securityLevel = snmpgo.NoAuthNoPriv
} else if strings.ToLower(managementSwitch.SNMPPrivProtocol) == "none" {
securityLevel = snmpgo.AuthNoPriv
} else {
securityLevel = snmpgo.AuthPriv
}
if securityLevel != snmpgo.NoAuthNoPriv {
if strings.ToLower(managementSwitch.SNMPAuthProtocol) == "md5" {
authType = snmpgo.Md5
} else if strings.ToLower(managementSwitch.SNMPAuthProtocol) == "sha" {
authType = snmpgo.Sha
}
}
if securityLevel == snmpgo.AuthPriv {
if strings.ToLower(managementSwitch.SNMPPrivProtocol) == "aes" {
privType = snmpgo.Aes
} else if strings.ToLower(managementSwitch.SNMPPrivProtocol) == "des" {
privType = snmpgo.Des
}
}
snmp, err = snmpgo.NewSNMP(snmpgo.SNMPArguments{
Version: snmpgo.V3,
Address: managementSwitch.Address,
Retries: 5,
UserName: managementSwitch.SNMPUser,
SecurityLevel: securityLevel,
AuthPassword: managementSwitch.SNMPAuthPassword,
AuthProtocol: authType,
PrivPassword: managementSwitch.SNMPPrivPassword,
PrivProtocol: privType,
})
return
}
func snmpGetBulk(snmp *snmpgo.SNMP, oid string) (snmpgo.Pdu, error) {
oids, err := snmpgo.NewOids([]string{oid})
if err != nil {
return nil, err
}
err = snmp.Open()
if err != nil {
return nil, err
}
defer snmp.Close()
result, err := snmp.GetBulkWalk(oids, 0, 10)
if err != nil {
return nil, err
}
if result.ErrorStatus() != snmpgo.NoError {
return nil, errors.New(result.ErrorStatus().String())
}
return result, nil
}
func getDynamicMacs(snmp *snmpgo.SNMP, useVLANs bool) (macPortMap map[string]int, err error) {
var portSrc string
if useVLANs {
portSrc = OIDMacAddressesWithVLAN
} else {
portSrc = OIDMACAddressesNoVLAN
}
port, bulkErr := snmpGetBulk(snmp, portSrc)
if bulkErr != nil {
err = fmt.Errorf("unable to get port src: %w", bulkErr)
return
}
// Process the MAC->port list into a map.
macPortMap = make(map[string]int)
for _, portEntry := range port.VarBinds() {
portMac, conversionErr := MacAddressFromOID(portEntry.Oid.String())
if conversionErr != nil {
err = fmt.Errorf("failed to parse OID (%s) into MAC address: %w",
portEntry.Oid.String(), conversionErr)
continue
}
portNum, conversionErr := portEntry.Variable.BigInt()
if conversionErr != nil {
err = fmt.Errorf("failed to turn port number (%s) into an integer: %w",
portEntry.Variable.String(), conversionErr)
}
if int((*portNum).Int64()) != 0 {
macPortMap[portMac] = int((*portNum).Int64())
}
}
return
}
func MacAddressFromOID(OID string) (macAddress string, err error) {
OIDParts := strings.Split(OID, ".")
if len(OIDParts) < 6 {
err = errors.New("oid has fewer than 6 parts so it cannot contain a MAC address")
return
}
for _, part := range OIDParts[len(OIDParts)-6:] {
val, conversionErr := strconv.Atoi(part)
if conversionErr != nil {
err = fmt.Errorf("failed to convert part to int: %w", conversionErr)
return
}
if val > 255 || val < 0 {
err = fmt.Errorf("%s is >255 or <0 which is invalid", part)
return
}
str := strconv.FormatInt(int64(val), 16)
if len(str) < 2 {
str = "0" + str
}
macAddress += str
}
return
}