-
Notifications
You must be signed in to change notification settings - Fork 0
/
ekinops_module_reader_transponder.go
195 lines (163 loc) · 6.23 KB
/
ekinops_module_reader_transponder.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
package codecommunicator
import (
"context"
"github.com/RincewindsHat/thola/internal/device"
"github.com/RincewindsHat/thola/internal/network"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"strings"
)
type ekinopsModuleReaderTransponder struct {
ekinopsModuleData
networkLinePortsOIDs ekinopsTransponderOIDs
clientPortsOIDs ekinopsTransponderOIDs
}
type ekinopsTransponderOIDs struct {
identifierOID network.OID
labelOID network.OID
txPowerOID network.OID
rxPowerOID network.OID
correctedFEC network.OID
uncorrectedFEC network.OID
powerTransformFunc ekinopsPowerTransformFunc
}
func (m *ekinopsModuleReaderTransponder) readModuleMetrics(ctx context.Context, interfaces []device.Interface) ([]device.Interface, error) {
var OpticalTransponderInterfaces []device.OpticalTransponderInterface
// network / line ports
oti, err := ekinopsReadTransponderMetrics(ctx, m.networkLinePortsOIDs)
if err != nil {
return nil, errors.Wrap(err, "failed to read out metrics for booster ports")
}
OpticalTransponderInterfaces = append(OpticalTransponderInterfaces, oti...)
// client ports
oti, err = ekinopsReadTransponderMetrics(ctx, m.clientPortsOIDs)
if err != nil {
return nil, errors.Wrap(err, "failed to read out metrics for booster ports")
}
OpticalTransponderInterfaces = append(OpticalTransponderInterfaces, oti...)
mappings, err := ekinopsInterfacesIfIdentifierToSliceIndex(interfaces)
if err != nil {
return nil, errors.Wrap(err, "failed to get interface identifier mappings")
}
for i, opticalTransponderInterface := range OpticalTransponderInterfaces {
identifier := m.slotIdentifier + "/" + m.moduleName + "/" + strings.Split(*opticalTransponderInterface.Identifier, "(")[0]
idx, ok := mappings[identifier]
if !ok {
log.Ctx(ctx).Debug().Msgf("interface for identifier '%s' not found", identifier)
continue
}
interfaces[idx].OpticalTransponder = &OpticalTransponderInterfaces[i]
interfaces[idx].IfAlias = interfaces[idx].OpticalTransponder.Label
}
return interfaces, nil
}
func ekinopsReadTransponderMetrics(ctx context.Context, oids ekinopsTransponderOIDs) ([]device.OpticalTransponderInterface, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return nil, errors.New("no device connection available")
}
if oids.identifierOID == "" || oids.labelOID == "" {
return nil, errors.New("identifier and label oid need to be defined")
}
identifierResults, err := con.SNMP.SnmpClient.SNMPWalk(ctx, oids.identifierOID)
if err != nil {
return nil, errors.Wrap(err, "snmpwalk for identifier oid failed")
}
labelResults, err := con.SNMP.SnmpClient.SNMPWalk(ctx, oids.labelOID)
if err != nil {
return nil, errors.Wrap(err, "snmpwalk for label oid failed")
}
var txPowerResult, rxPowerResult, correctedFECResult, uncorrectedFECResult []network.SNMPResponse
if oids.txPowerOID != "" {
txPowerResult, err = con.SNMP.SnmpClient.SNMPWalk(ctx, oids.txPowerOID)
if err != nil {
return nil, errors.Wrap(err, "snmpwalk for tx power oid failed")
}
}
if oids.rxPowerOID != "" {
rxPowerResult, err = con.SNMP.SnmpClient.SNMPWalk(ctx, oids.rxPowerOID)
if err != nil {
return nil, errors.Wrap(err, "snmpwalk for rx power oid failed")
}
}
if oids.correctedFEC != "" {
correctedFECResult, err = con.SNMP.SnmpClient.SNMPWalk(ctx, oids.correctedFEC)
if err != nil {
return nil, errors.Wrap(err, "snmpwalk for corrected fec oid failed")
}
}
if oids.uncorrectedFEC != "" {
uncorrectedFECResult, err = con.SNMP.SnmpClient.SNMPWalk(ctx, oids.uncorrectedFEC)
if err != nil {
return nil, errors.Wrap(err, "snmpwalk for uncorrected fec oid failed")
}
}
var opticalTransponderInterfaces []device.OpticalTransponderInterface
for k, identifierResult := range identifierResults {
var opticalTransponderInterface device.OpticalTransponderInterface
identifier, err := identifierResult.GetValue()
if err != nil {
return nil, errors.Wrap(err, "failed to get identifier for optical amplifier interface")
}
identifierString := strings.Split(identifier.String(), "\n")[0]
label, err := labelResults[k].GetValue()
if err != nil {
return nil, errors.Wrap(err, "failed to get label for optical amplifier interface")
}
labelString := strings.Split(label.String(), "\n")[0]
opticalTransponderInterface.Identifier = &identifierString
opticalTransponderInterface.Label = &labelString
if rxPowerResult != nil {
value, err := rxPowerResult[k].GetValue()
if err != nil {
return nil, errors.Wrap(err, "failed to get rx power for optical amplifier interface")
}
valueFloat, err := value.Float64()
if err != nil {
return nil, errors.Wrap(err, "failed to parse snmp response to float64")
}
if oids.powerTransformFunc != nil {
valueFloat = oids.powerTransformFunc(valueFloat)
}
opticalTransponderInterface.RXPower = &valueFloat
}
if txPowerResult != nil {
value, err := txPowerResult[k].GetValue()
if err != nil {
return nil, errors.Wrap(err, "failed to get tx power for optical amplifier interface")
}
valueFloat, err := value.Float64()
if err != nil {
return nil, errors.Wrap(err, "failed to parse snmp response to float64")
}
if oids.powerTransformFunc != nil {
valueFloat = oids.powerTransformFunc(valueFloat)
}
opticalTransponderInterface.TXPower = &valueFloat
}
if correctedFECResult != nil {
value, err := correctedFECResult[k].GetValue()
if err != nil {
return nil, errors.Wrap(err, "failed to get corrected for optical amplifier interface")
}
valueUint, err := value.UInt64()
if err != nil {
return nil, errors.Wrap(err, "failed to parse snmp response to uint64")
}
opticalTransponderInterface.CorrectedFEC = &valueUint
}
if uncorrectedFECResult != nil {
value, err := uncorrectedFECResult[k].GetValue()
if err != nil {
return nil, errors.Wrap(err, "failed to get uncorrected for optical amplifier interface")
}
valueUint, err := value.UInt64()
if err != nil {
return nil, errors.Wrap(err, "failed to parse snmp response to uint64")
}
opticalTransponderInterface.UncorrectedFEC = &valueUint
}
opticalTransponderInterfaces = append(opticalTransponderInterfaces, opticalTransponderInterface)
}
return opticalTransponderInterfaces, nil
}