-
Notifications
You must be signed in to change notification settings - Fork 0
/
ekinops_module_reader_opm.go
183 lines (151 loc) · 5.37 KB
/
ekinops_module_reader_opm.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
package codecommunicator
import (
"context"
"fmt"
"github.com/RincewindsHat/thola/internal/device"
"github.com/RincewindsHat/thola/internal/network"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"strconv"
"strings"
)
type ekinopsModuleReaderOPM8 struct {
ekinopsModuleData
ports ekinopsOPMOIDs
}
type ekinopsOPMOIDs struct {
identifierOID network.OID
labelOID network.OID
rxPowerOID network.OID
channelsOid network.OID
powerTransformFunc ekinopsPowerTransformFunc
}
func (m *ekinopsModuleReaderOPM8) readModuleMetrics(ctx context.Context, interfaces []device.Interface) ([]device.Interface, error) {
opticalOPMInterfaces, err := ekinopsReadOPMMetrics(ctx, m.ports)
if err != nil {
return nil, errors.Wrap(err, "failed to read out metrics for booster ports")
}
mappings, err := ekinopsInterfacesIfIdentifierToSliceIndex(interfaces)
if err != nil {
return nil, errors.Wrap(err, "failed to get interface identifier mappings")
}
for i, opticalOPMInterface := range opticalOPMInterfaces {
identifier := m.slotIdentifier + "/" + m.moduleName + "/" + strings.Split(*opticalOPMInterface.Identifier, "(")[0]
idx, ok := mappings[identifier]
if !ok {
log.Ctx(ctx).Debug().Msgf("interface for identifier '%s' not found", identifier)
continue
}
interfaces[idx].OpticalOPM = &opticalOPMInterfaces[i]
interfaces[idx].IfAlias = interfaces[idx].OpticalOPM.Label
}
return interfaces, nil
}
func ekinopsReadOPMMetrics(ctx context.Context, oids ekinopsOPMOIDs) ([]device.OpticalOPMInterface, 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 rxPowerResult []network.SNMPResponse
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")
}
}
var opticalOPMInterfaces []device.OpticalOPMInterface
for k, identifierResult := range identifierResults {
var opticalOPMInterface device.OpticalOPMInterface
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]
opticalOPMInterface.Identifier = &identifierString
opticalOPMInterface.Label = &labelString
if rxPowerResult != nil {
value, err := rxPowerResult[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)
}
opticalOPMInterface.RXPower = &valueFloat
}
opticalOPMInterfaces = append(opticalOPMInterfaces, opticalOPMInterface)
}
// read out channels
channelsResults, err := con.SNMP.SnmpClient.SNMPWalk(ctx, oids.channelsOid)
if err != nil {
return nil, errors.Wrap(err, "snmpwalk for identifier oid failed")
}
// results to map
channelValues := make(map[int]map[int]float64)
for _, channelResult := range channelsResults {
val, err := channelResult.GetValue()
if err != nil {
return nil, errors.Wrapf(err, "failed to get snmp response as string (oid: %s)", channelResult.GetOID())
}
value, err := val.Float64()
if err != nil {
return nil, errors.Wrapf(err, "failed to parse snmp response to float64 (response: %s)", val)
}
if oids.powerTransformFunc != nil {
value = oids.powerTransformFunc(value)
}
oidArr := strings.Split(channelResult.GetOID().String(), ".")
if oidArr[len(oidArr)-2] == "1" {
continue
}
portIdx, err := strconv.Atoi(oidArr[len(oidArr)-1])
if err != nil {
return nil, errors.Wrapf(err, "failed to parse oid port index to int (index: %s)", oidArr[len(oidArr)-1])
}
channelIdx, err := strconv.Atoi(oidArr[len(oidArr)-4])
if err != nil {
return nil, errors.Wrapf(err, "failed to parse oid channel index to int (index: %s)", oidArr[len(oidArr)-4])
}
if channelIdx > 776 {
break
}
if _, ok := channelValues[portIdx]; !ok {
channelValues[portIdx] = make(map[int]float64)
}
channelValues[portIdx][channelIdx] = value
}
for k := range opticalOPMInterfaces {
channelNum := 13.0
for channelIdx := 16; channelIdx <= 776; channelIdx += 8 {
channelName := fmt.Sprintf("C%.2f", channelNum)
rxPower := channelValues[k][channelIdx]
channel := device.OpticalChannel{
Channel: &channelName,
RXPower: &rxPower,
}
opticalOPMInterfaces[k].Channels = append(opticalOPMInterfaces[k].Channels, channel)
channelNum += 0.5
}
}
return opticalOPMInterfaces, nil
}