-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathpower.go
512 lines (464 loc) · 14.8 KB
/
power.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package collectors
import (
"errors"
"fmt"
"github.com/netapp/harvest/v2/cmd/poller/plugin"
"github.com/netapp/harvest/v2/cmd/tools/rest"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/slogx"
"github.com/netapp/harvest/v2/pkg/util"
"log/slog"
"net/http"
"regexp"
"sort"
"strings"
"time"
)
const (
zapiValueKey = "environment-sensors-info.threshold-sensor-value"
restValueKey = "value"
)
// CollectChassisFRU is here because both ZAPI and REST sensor.go plugin call it to collect
// `system chassis fru show`.
// Chassis FRU information is only available via private CLI
func collectChassisFRU(client *rest.Client, logger *slog.Logger) (map[string]int, error) {
fields := []string{"fru-name", "type", "status", "connected-nodes", "num-nodes"}
query := "api/private/cli/system/chassis/fru"
filter := []string{"type=psu"}
href := rest.NewHrefBuilder().
APIPath(query).
Fields(fields).
Filter(filter).
MaxRecords(DefaultBatchSize).
Build()
result, err := rest.FetchAll(client, href)
if err != nil {
return nil, fmt.Errorf("failed to fetch data href=%s err=%w", href, err)
}
// map of PSUs node -> numNode
nodeToNumNode := make(map[string]int)
for _, r := range result {
cn := r.Get("connected_nodes")
if !cn.Exists() {
logger.Warn(
"fru has no connected nodes",
slog.String("cluster", client.Remote().Name),
slog.String("fru", r.Get("fru_name").ClonedString()),
)
continue
}
numNodes := int(r.Get("num_nodes").Int())
for _, e := range cn.Array() {
nodeToNumNode[e.ClonedString()] = numNodes
}
}
return nodeToNumNode, nil
}
type sensorValue struct {
node string
name string
value float64
unit string
}
type environmentMetric struct {
key string
ambientTemperature []float64
nonAmbientTemperature []float64
fanSpeed []float64
powerSensor map[string]*sensorValue
voltageSensor map[string]*sensorValue
currentSensor map[string]*sensorValue
}
var ambientRegex = regexp.MustCompile(`^(Ambient Temp|Ambient Temp \d|PSU\d AmbTemp|PSU\d Inlet|PSU\d Inlet Temp|In Flow Temp|Front Temp|Bat_Ambient \d|Riser Inlet Temp)$`)
var powerInRegex = regexp.MustCompile(`^PSU\d (InPwr Monitor|InPower|PIN|Power In|In Pwr)$`)
var voltageRegex = regexp.MustCompile(`^PSU\d (\d+V|InVoltage|VIN|AC In Volt|In Volt)$`)
var currentRegex = regexp.MustCompile(`^PSU\d (\d+V Curr|Curr|InCurrent|Curr IIN|AC In Curr|In Curr)$`)
var eMetrics = []string{
"average_ambient_temperature",
"average_fan_speed",
"average_temperature",
"max_fan_speed",
"max_temperature",
"min_ambient_temperature",
"min_fan_speed",
"min_temperature",
"power",
}
func calculateEnvironmentMetrics(data *matrix.Matrix, logger *slog.Logger, valueKey string, myData *matrix.Matrix, nodeToNumNode map[string]int) []*matrix.Matrix {
sensorEnvironmentMetricMap := make(map[string]*environmentMetric)
excludedSensors := make(map[string][]sensorValue)
for k, instance := range data.GetInstances() {
if !instance.IsExportable() {
continue
}
iKey := instance.GetLabel("node")
if iKey == "" {
logger.Warn("missing node label for instance", slog.String("key", k))
continue
}
sensorName := instance.GetLabel("sensor")
if sensorName == "" {
logger.Warn("missing sensor name for instance", slog.String("key", k))
continue
}
if _, ok := sensorEnvironmentMetricMap[iKey]; !ok {
sensorEnvironmentMetricMap[iKey] = &environmentMetric{key: iKey, ambientTemperature: []float64{}, nonAmbientTemperature: []float64{}, fanSpeed: []float64{}}
}
for mKey, metric := range data.GetMetrics() {
if mKey != valueKey {
continue
}
sensorType := instance.GetLabel("type")
sensorUnit := instance.GetLabel("unit")
isAmbientMatch := ambientRegex.MatchString(sensorName)
isPowerMatch := powerInRegex.MatchString(sensorName)
isVoltageMatch := voltageRegex.MatchString(sensorName)
isCurrentMatch := currentRegex.MatchString(sensorName)
if sensorType == "thermal" && isAmbientMatch {
if value, ok := metric.GetValueFloat64(instance); ok {
sensorEnvironmentMetricMap[iKey].ambientTemperature = append(sensorEnvironmentMetricMap[iKey].ambientTemperature, value)
}
}
if sensorType == "thermal" && !isAmbientMatch {
// Exclude temperature sensors that contains sensor name `Margin` and value < 0
value, ok := metric.GetValueFloat64(instance)
if value > 0 && !strings.Contains(sensorName, "Margin") {
if ok {
sensorEnvironmentMetricMap[iKey].nonAmbientTemperature = append(sensorEnvironmentMetricMap[iKey].nonAmbientTemperature, value)
}
} else {
excludedSensors[iKey] = append(excludedSensors[iKey], sensorValue{
node: iKey,
name: sensorName,
value: value,
})
}
}
if sensorType == "fan" {
if value, ok := metric.GetValueFloat64(instance); ok {
sensorEnvironmentMetricMap[iKey].fanSpeed = append(sensorEnvironmentMetricMap[iKey].fanSpeed, value)
}
}
if isPowerMatch {
if value, ok := metric.GetValueFloat64(instance); ok {
if !IsValidUnit(sensorUnit) {
logger.Warn("unknown power unit", slog.String("unit", sensorUnit), slog.Float64("value", value))
} else {
if sensorEnvironmentMetricMap[iKey].powerSensor == nil {
sensorEnvironmentMetricMap[iKey].powerSensor = make(map[string]*sensorValue)
}
sensorEnvironmentMetricMap[iKey].powerSensor[k] = &sensorValue{
node: iKey,
name: sensorName,
value: value,
unit: sensorUnit,
}
}
}
}
if isVoltageMatch {
if value, ok := metric.GetValueFloat64(instance); ok {
if sensorEnvironmentMetricMap[iKey].voltageSensor == nil {
sensorEnvironmentMetricMap[iKey].voltageSensor = make(map[string]*sensorValue)
}
sensorEnvironmentMetricMap[iKey].voltageSensor[k] = &sensorValue{
node: iKey,
name: sensorName,
value: value,
unit: sensorUnit,
}
}
}
if isCurrentMatch {
if value, ok := metric.GetValueFloat64(instance); ok {
if sensorEnvironmentMetricMap[iKey].currentSensor == nil {
sensorEnvironmentMetricMap[iKey].currentSensor = make(map[string]*sensorValue)
}
sensorEnvironmentMetricMap[iKey].currentSensor[k] = &sensorValue{
node: iKey,
name: sensorName,
value: value,
unit: sensorUnit,
}
}
}
}
}
if len(excludedSensors) > 0 {
var excludedSensorStr string
for k, v := range excludedSensors {
excludedSensorStr += " node:" + k + " sensor:" + fmt.Sprintf("%v", v)
}
logger.Info("sensor excluded", slog.String("sensor", excludedSensorStr))
}
whrSensors := make(map[string]*sensorValue)
for key, v := range sensorEnvironmentMetricMap {
instance, err2 := myData.NewInstance(key)
if err2 != nil {
logger.Warn("instance not found", slog.String("key", key))
continue
}
// set node label
instance.SetLabel("node", key)
for _, k := range eMetrics {
m := myData.GetMetric(k)
switch k {
case "power":
var sumPower float64
switch {
case len(v.powerSensor) > 0:
for _, v1 := range v.powerSensor {
switch {
case v1.unit == "mW" || v1.unit == "mW*hr":
sumPower += v1.value / 1000
case v1.unit == "W" || v1.unit == "W*hr":
sumPower += v1.value
default:
logger.Warn(
"unknown power unit",
slog.String("node", key),
slog.String("name", v1.name),
slog.String("unit", v1.unit),
slog.Float64("value", v1.value),
)
}
if v1.unit == "mW*hr" || v1.unit == "W*hr" {
whrSensors[v1.name] = v1
}
}
case len(v.voltageSensor) > 0 && len(v.voltageSensor) == len(v.currentSensor):
voltageKeys := make([]string, 0, len(v.voltageSensor))
for k := range v.voltageSensor {
voltageKeys = append(voltageKeys, k)
}
sort.Strings(voltageKeys)
currentKeys := make([]string, 0, len(v.currentSensor))
for k := range v.currentSensor {
currentKeys = append(currentKeys, k)
}
sort.Strings(currentKeys)
for i := range currentKeys {
currentKey := currentKeys[i]
voltageKey := voltageKeys[i]
// get values
currentSensorValue := v.currentSensor[currentKey]
voltageSensorValue := v.voltageSensor[voltageKey]
// convert units
if currentSensorValue.unit == "mA" {
currentSensorValue.value /= 1000
} else if currentSensorValue.unit != "A" {
logger.Warn(
"unknown current unit",
slog.String("node", key),
slog.String("unit", currentSensorValue.unit),
slog.Float64("value", currentSensorValue.value),
)
}
if voltageSensorValue.unit == "mV" {
voltageSensorValue.value /= 1000
} else if voltageSensorValue.unit != "V" {
logger.Warn(
"unknown voltage unit",
slog.String("node", key),
slog.String("unit", voltageSensorValue.unit),
slog.Float64("value", voltageSensorValue.value),
)
}
p := currentSensorValue.value * voltageSensorValue.value
if !strings.EqualFold(voltageSensorValue.name, "in") && !strings.EqualFold(currentSensorValue.name, "in") {
p /= 0.93 // If the sensor names to do NOT contain "IN" or "in", then we need to adjust the power to account for loss in the power supply. We will use 0.93 as the power supply efficiency factor for all systems.
}
sumPower += p
}
default:
logger.Warn(
"current and voltage sensor are ignored",
slog.String("node", key),
slog.Int("current size", len(v.currentSensor)),
slog.Int("voltage size", len(v.voltageSensor)),
)
}
numNode, ok := nodeToNumNode[key]
if !ok {
logger.Warn("node not found in nodeToNumNode map", slog.String("node", key))
numNode = 1
}
sumPower /= float64(numNode)
err2 = m.SetValueFloat64(instance, sumPower)
if err2 != nil {
logger.Error(
"unable to set power",
slog.Any("err", err2),
slog.Float64("power", sumPower),
)
}
case "average_ambient_temperature":
if len(v.ambientTemperature) > 0 {
aaT := util.Avg(v.ambientTemperature)
err2 = m.SetValueFloat64(instance, aaT)
if err2 != nil {
logger.Error(
"unable to set average_ambient_temperature",
slog.Any("err", err2),
slog.Float64("average_ambient_temperature", aaT),
)
}
}
case "min_ambient_temperature":
maT := util.Min(v.ambientTemperature)
err2 = m.SetValueFloat64(instance, maT)
if err2 != nil {
logger.Error(
"unable to set min_ambient_temperature",
slog.Any("err", err2),
slog.Float64("min_ambient_temperature", maT),
)
}
case "max_temperature":
mT := util.Max(v.nonAmbientTemperature)
err2 = m.SetValueFloat64(instance, mT)
if err2 != nil {
logger.Error(
"unable to set max_temperature",
slog.Any("err", err2),
slog.Float64("max_temperature", mT),
)
}
case "average_temperature":
if len(v.nonAmbientTemperature) > 0 {
nat := util.Avg(v.nonAmbientTemperature)
err2 = m.SetValueFloat64(instance, nat)
if err2 != nil {
logger.Error(
"unable to set average_temperature",
slog.Any("err", err2),
slog.Float64("average_temperature", nat),
)
}
}
case "min_temperature":
mT := util.Min(v.nonAmbientTemperature)
err2 = m.SetValueFloat64(instance, mT)
if err2 != nil {
logger.Error(
"unable to set min_temperature",
slog.Any("err", err2),
slog.Float64("min_temperature", mT),
)
}
case "average_fan_speed":
if len(v.fanSpeed) > 0 {
afs := util.Avg(v.fanSpeed)
err2 = m.SetValueFloat64(instance, afs)
if err2 != nil {
logger.Error(
"unable to set average_fan_speed",
slog.Any("err", err2),
slog.Float64("average_fan_speed", afs),
)
}
}
case "max_fan_speed":
mfs := util.Max(v.fanSpeed)
err2 = m.SetValueFloat64(instance, mfs)
if err2 != nil {
logger.Error(
"unable to set max_fan_speed",
slog.Any("err", err2),
slog.Float64("max_fan_speed", mfs),
)
}
case "min_fan_speed":
mfs := util.Min(v.fanSpeed)
err2 = m.SetValueFloat64(instance, mfs)
if err2 != nil {
logger.Error(
"unable to set min_fan_speed",
slog.Any("err", err2),
slog.Float64("min_fan_speed", mfs),
)
}
}
}
}
if len(whrSensors) > 0 {
var whrSensorsStr string
for _, v := range whrSensors {
whrSensorsStr += " sensor:" + fmt.Sprintf("%v", *v)
}
logger.Info("sensor with *hr units", slog.String("sensor", whrSensorsStr))
}
return []*matrix.Matrix{myData}
}
func NewSensor(p *plugin.AbstractPlugin) plugin.Plugin {
return &Sensor{AbstractPlugin: p}
}
type Sensor struct {
*plugin.AbstractPlugin
data *matrix.Matrix
client *rest.Client
instanceKeys map[string]string
instanceLabels map[string]map[string]string
hasREST bool
}
func (s *Sensor) Init(remote conf.Remote) error {
var err error
if err := s.InitAbc(); err != nil {
return err
}
timeout, _ := time.ParseDuration(rest.DefaultTimeout)
if s.client, err = rest.New(conf.ZapiPoller(s.ParentParams), timeout, s.Auth); err != nil {
s.SLogger.Error("connecting", slogx.Err(err))
return err
}
s.hasREST = true
if err := s.client.Init(5, remote); err != nil {
var re *errs.RestError
if errors.As(err, &re) && re.StatusCode == http.StatusNotFound {
s.SLogger.Warn("Cluster does not support REST. Power plugin disabled")
s.hasREST = false
return nil
}
return err
}
s.data = matrix.New(s.Parent+".Sensor", "environment_sensor", "environment_sensor")
s.instanceKeys = make(map[string]string)
s.instanceLabels = make(map[string]map[string]string)
// init environment metrics in plugin matrix
// create environment metric if not exists
for _, k := range eMetrics {
err := matrix.CreateMetric(k, s.data)
if err != nil {
s.SLogger.Warn("error while creating metric", slogx.Err(err), slog.String("key", k))
}
}
return nil
}
func (s *Sensor) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, *util.Metadata, error) {
if !s.hasREST {
return nil, nil, nil
}
data := dataMap[s.Object]
// Purge and reset data
s.data.PurgeInstances()
s.data.Reset()
s.client.Metadata.Reset()
// Set all global labels if they don't already exist
s.data.SetGlobalLabels(data.GetGlobalLabels())
// Collect chassis fru show, so we can determine if a controller's PSUs are shared or not
nodeToNumNode, err := collectChassisFRU(s.client, s.SLogger)
if err != nil {
return nil, nil, err
}
if len(nodeToNumNode) == 0 {
s.SLogger.Debug("No chassis field replaceable units found")
}
valueKey := zapiValueKey
if s.Parent == "Rest" {
valueKey = restValueKey
}
metrics := calculateEnvironmentMetrics(data, s.SLogger, valueKey, s.data, nodeToNumNode)
return metrics, s.client.Metadata, nil
}