-
Notifications
You must be signed in to change notification settings - Fork 2
/
exporter.go
465 lines (409 loc) · 14.7 KB
/
exporter.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
/*
* Copyright 2024 Comcast Cable Communications Management, LLC
*
* 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 moonshot
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/comcast/fishymetrics/common"
"github.com/comcast/fishymetrics/config"
"github.com/comcast/fishymetrics/oem/moonshot"
"github.com/comcast/fishymetrics/pool"
"go.uber.org/zap"
"github.com/hashicorp/go-retryablehttp"
"github.com/prometheus/client_golang/prometheus"
)
const (
// MOONSHOT is a HPE Hardware Device we scrape
MOONSHOT = "Moonshot"
// SWITCHA is the Moonshot Hardware Device we scrape
SWITCHA = "SwitchA"
// SWITCHB is the Moonshot Hardware Device we scrape
SWITCHB = "SwitchB"
// THERMAL represents the thermal metric endpoint
THERMAL = "ThermalMetrics"
// POWER represents the power metric endpoint
POWER = "PowerMetrics"
// SWITCH represents the Moonshot switch metric endpoint
SWITCH = "SwitchMetrics"
// OK is a string representation of the float 1.0 for device status
OK = 1.0
// BAD is a string representation of the float 0.0 for device status
BAD = 0.0
)
var (
log *zap.Logger
)
// Exporter collects chassis manager stats from the given URI and exports them using
// the prometheus metrics package.
type Exporter struct {
ctx context.Context
mutex sync.RWMutex
pool *pool.MoonshotPool
host string
credProfile string
deviceMetrics *map[string]*metrics
}
// NewExporter returns an initialized Exporter for HPE Moonshot device.
func NewExporter(ctx context.Context, target, uri, profile string) (*Exporter, error) {
var fqdn *url.URL
var tasks []*pool.MoonshotTask
var exp = Exporter{
ctx: ctx,
credProfile: profile,
deviceMetrics: NewDeviceMetrics(),
}
log = zap.L()
tr := &http.Transport{
Dial: (&net.Dialer{
Timeout: 3 * time.Second,
}).Dial,
MaxIdleConns: 1,
MaxConnsPerHost: 1,
MaxIdleConnsPerHost: 1,
IdleConnTimeout: 90 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
TLSHandshakeTimeout: 10 * time.Second,
}
retryClient := retryablehttp.NewClient()
retryClient.CheckRetry = retryablehttp.ErrorPropagatedRetryPolicy
retryClient.HTTPClient.Transport = tr
retryClient.HTTPClient.Timeout = 30 * time.Second
retryClient.Logger = nil
retryClient.RetryWaitMin = 2 * time.Second
retryClient.RetryWaitMax = 2 * time.Second
retryClient.RetryMax = 2
retryClient.RequestLogHook = func(l retryablehttp.Logger, r *http.Request, i int) {
retryCount := i
if retryCount > 0 {
log.Error("api call "+r.URL.String()+" failed, retry #"+strconv.Itoa(retryCount), zap.Any("trace_id", ctx.Value("traceID")))
}
}
// Check that the target passed in has http:// or https:// prefixed
fqdn, err := url.ParseRequestURI(target)
if err != nil {
fqdn = &url.URL{
Scheme: config.GetConfig().BMCScheme,
Host: target,
}
}
exp.host = fqdn.String()
// check if host is on the ignored list, if so we immediately return
if _, ok := common.IgnoredDevices[exp.host]; ok {
var upMetric = (*exp.deviceMetrics)["up"]
(*upMetric)["up"].WithLabelValues().Set(float64(2))
return &exp, nil
}
tasks = append(tasks,
pool.NewMoonshotTask(fetch(fqdn.String()+uri+"/ThermalMetrics", MOONSHOT, THERMAL, target, profile, retryClient)),
pool.NewMoonshotTask(fetch(fqdn.String()+uri+"/PowerMetrics", MOONSHOT, POWER, target, profile, retryClient)),
pool.NewMoonshotTask(fetch(fqdn.String()+uri+"/switches/sa", SWITCHA, SWITCH, target, profile, retryClient)),
pool.NewMoonshotTask(fetch(fqdn.String()+uri+"/switches/sb", SWITCHB, SWITCH, target, profile, retryClient)),
pool.NewMoonshotTask(fetch(fqdn.String()+uri+"/switches/sa/ThermalMetrics", SWITCHA, THERMAL, target, profile, retryClient)),
pool.NewMoonshotTask(fetch(fqdn.String()+uri+"/switches/sb/ThermalMetrics", SWITCHB, THERMAL, target, profile, retryClient)),
pool.NewMoonshotTask(fetch(fqdn.String()+uri+"/switches/sa/PowerMetrics", SWITCHA, POWER, target, profile, retryClient)),
pool.NewMoonshotTask(fetch(fqdn.String()+uri+"/switches/sb/PowerMetrics", SWITCHB, POWER, target, profile, retryClient)))
exp.pool = pool.NewMoonshotPool(tasks, 1)
return &exp, nil
}
// Describe describes all the metrics ever exported by the fishymetrics exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range *e.deviceMetrics {
for _, n := range *m {
n.Describe(ch)
}
}
}
// Collect fetches the stats from configured fishymetrics location and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
e.resetMetrics()
// perform scrape if target is not on ignored list
if _, ok := common.IgnoredDevices[e.host]; !ok {
e.scrape()
} else {
var upMetric = (*e.deviceMetrics)["up"]
(*upMetric)["up"].WithLabelValues().Set(float64(2))
}
e.collectMetrics(ch)
}
func (e *Exporter) resetMetrics() {
for _, m := range *e.deviceMetrics {
for _, n := range *m {
n.Reset()
}
}
}
func (e *Exporter) collectMetrics(metrics chan<- prometheus.Metric) {
for _, m := range *e.deviceMetrics {
for _, n := range *m {
n.Collect(metrics)
}
}
}
func fetch(uri, device, metricType, host, profile string, client *retryablehttp.Client) func() ([]byte, string, string, error) {
var resp *http.Response
var credential *common.Credential
var err error
retryCount := 0
return func() ([]byte, string, string, error) {
req := common.BuildRequest(uri, host)
resp, err = common.DoRequest(client, req)
if err != nil {
return nil, device, metricType, err
}
defer resp.Body.Close()
if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
if resp.StatusCode == http.StatusNotFound {
for retryCount < 3 && resp.StatusCode == http.StatusNotFound {
time.Sleep(client.RetryWaitMin)
resp, err = common.DoRequest(client, req)
retryCount = retryCount + 1
}
if err != nil {
return nil, device, metricType, err
} else if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
return nil, device, metricType, fmt.Errorf("HTTP status %d", resp.StatusCode)
}
} else if resp.StatusCode == http.StatusUnauthorized {
if common.ChassisCreds.Vault != nil {
// Credentials may have rotated, go to vault and get the latest
credential, err = common.ChassisCreds.GetCredentials(context.Background(), profile, host)
if err != nil {
return nil, device, metricType, fmt.Errorf("issue retrieving credentials from vault using target: %s", host)
}
common.ChassisCreds.Set(host, credential)
} else {
return nil, device, metricType, fmt.Errorf("HTTP status %d", resp.StatusCode)
}
// build new request with updated credentials
req = common.BuildRequest(uri, host)
time.Sleep(client.RetryWaitMin)
resp, err = common.DoRequest(client, req)
if err != nil {
return nil, device, metricType, fmt.Errorf("HTTP status %d", resp.StatusCode)
}
} else {
return nil, device, metricType, fmt.Errorf("HTTP status %d", resp.StatusCode)
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, device, metricType, fmt.Errorf("Error reading Response Body - " + err.Error())
}
return body, device, metricType, nil
}
}
func (e *Exporter) scrape() {
var result uint8
state := uint8(1)
scrapes := len(e.pool.Tasks)
scrapeChan := make(chan uint8, scrapes)
// Concurrently call the endpoints to help prevent reaching the maxiumum number of 4 simultaneous sessions
e.pool.Run()
for _, task := range e.pool.Tasks {
var err error
if task.Err != nil {
deviceState := uint8(0)
// If credentials are incorrect we will add host to be ignored until manual intervention
if errors.Is(task.Err, common.ErrInvalidCredential) {
common.IgnoredDevices[e.host] = common.IgnoredDevice{
Name: e.host,
Endpoint: "https://" + e.host + "/rest/v1/chassis/1",
Module: MOONSHOT,
CredentialProfile: e.credProfile,
}
log.Info("added host "+e.host+" to ignored list", zap.Any("trace_id", e.ctx.Value("traceID")))
deviceState = 2
} else {
deviceState = 0
}
var upMetric = (*e.deviceMetrics)["up"]
(*upMetric)["up"].WithLabelValues().Set(float64(deviceState))
log.Error("error from "+task.Device, zap.Error(task.Err), zap.String("api", task.MetricType), zap.Any("trace_id", e.ctx.Value("traceID")))
return
}
switch task.Device {
case MOONSHOT:
switch task.MetricType {
// Unmarshal and populate Moonshot Thermal metrics
case THERMAL:
err = e.exportThermalMetrics(task.Body)
// Unmarshal and populate Moonshot Power metrics
case POWER:
err = e.exportPowerMetrics(task.Body)
}
// Unmarshal and populate Switch metrics
case SWITCHA:
switch task.MetricType {
case SWITCH:
err = e.exportSwitchMetrics(task.Body)
// Unmarshal and populate SwitchA Thermal metrics
case THERMAL:
err = e.exportSwitchThermalMetrics("switch-a", task.Body)
// Unmarshal and populate SwitchA Power metrics
case POWER:
err = e.exportSwitchPowerMetrics("switch-a", task.Body)
}
case SWITCHB:
switch task.MetricType {
case SWITCH:
err = e.exportSwitchMetrics(task.Body)
// Unmarshal and populate SwitchB Thermal metrics
case THERMAL:
err = e.exportSwitchThermalMetrics("switch-b", task.Body)
// Unmarshal and populate SwitchB Power metrics
case POWER:
err = e.exportSwitchPowerMetrics("switch-b", task.Body)
}
}
if err != nil {
scrapeChan <- 0
log.Error("error exporting metrics - from "+task.Device, zap.Error(err), zap.String("api", task.MetricType), zap.Any("trace_id", e.ctx.Value("traceID")))
continue
}
scrapeChan <- 1
}
// Get scrape results from goroutine(s) and perform bitwise AND, any failures should
// result in a scrape failure
for i := 0; i < scrapes; i++ {
result = <-scrapeChan
state &= result
}
var upMetric = (*e.deviceMetrics)["up"]
(*upMetric)["up"].WithLabelValues().Set(float64(state))
}
// exportPowerMetrics collects the HPE Moonshot's power metrics in json format and sets the prometheus gauges
func (e *Exporter) exportPowerMetrics(body []byte) error {
var state float64
var pm moonshot.PowerMetrics
var msPower = (*e.deviceMetrics)["powerMetrics"]
err := json.Unmarshal(body, &pm)
if err != nil {
return fmt.Errorf("Error Unmarshalling PowerMetrics - " + err.Error())
}
(*msPower)["supplyTotalConsumed"].WithLabelValues().Set(float64(pm.PowerConsumedWatts))
(*msPower)["supplyTotalCapacity"].WithLabelValues().Set(float64(pm.PowerCapacityWatts))
for _, ps := range pm.PowerSupplies {
(*msPower)["supplyOutput"].WithLabelValues(ps.Name, ps.SparePartNumber).Set(float64(ps.LastPowerOutputWatts))
if ps.Status.State == "OK" {
state = OK
} else {
state = BAD
}
(*msPower)["supplyStatus"].WithLabelValues(ps.Name, ps.SparePartNumber).Set(state)
}
return nil
}
// exportThermalMetrics collects the HPE Moonshot's thermal and fan metrics in json format and sets the prometheus gauges
func (e *Exporter) exportThermalMetrics(body []byte) error {
var state float64
var tm moonshot.ThermalMetrics
var msThermal = (*e.deviceMetrics)["thermalMetrics"]
err := json.Unmarshal(body, &tm)
if err != nil {
return fmt.Errorf("Error Unmarshalling ThermalMetrics - " + err.Error())
}
// Iterate through fans
for _, fan := range tm.Fans {
(*msThermal)["fanSpeed"].WithLabelValues(fan.FanName).Set(float64(fan.CurrentReading))
// Check fan status and convert string to numeric values
if fan.Status.State == "OK" {
state = OK
} else {
state = BAD
}
(*msThermal)["fanStatus"].WithLabelValues(fan.FanName).Set(state)
}
// Iterate through sensors
for _, sensor := range tm.Temperatures {
(*msThermal)["sensorTemperature"].WithLabelValues(strings.TrimRight(sensor.Name, " ")).Set(float64(sensor.CurrentReading))
// Check sensor status and convert string to numeric values
if sensor.Status.State == "OK" {
state = OK
} else {
state = BAD
}
(*msThermal)["sensorStatus"].WithLabelValues(strings.TrimRight(sensor.Name, " ")).Set(state)
}
return nil
}
// exportSwitchMetrics collects the switches metrics in json format and sets the prometheus gauges
func (e *Exporter) exportSwitchMetrics(body []byte) error {
var state float64
var sm moonshot.Sw
var msSw = (*e.deviceMetrics)["swMetrics"]
err := json.Unmarshal(body, &sm)
if err != nil {
return fmt.Errorf("Error Unmarshalling Sw - " + err.Error())
}
if sm.Status.State == "OK" {
state = OK
} else {
state = BAD
}
(*msSw)["moonshotSwitchStatus"].WithLabelValues(sm.Name, sm.SerialNumber).Set(state)
return nil
}
// exportSwitchThermalMetrics collects the switches thermal metrics in json format and sets the prometheus gauges
func (e *Exporter) exportSwitchThermalMetrics(namePrefix string, body []byte) error {
var state float64
var tm moonshot.ThermalMetrics
var msSwThermal = (*e.deviceMetrics)["swThermalMetrics"]
err := json.Unmarshal(body, &tm)
if err != nil {
return fmt.Errorf("Error Unmarshalling ThermalMetrics - " + err.Error())
}
// Iterate through sensors
for _, sensor := range tm.Temperatures {
(*msSwThermal)["moonshotSwitchSensorTemperature"].WithLabelValues(strings.TrimRight(namePrefix+"-"+tm.Name, " ")).Set(float64(sensor.CurrentReading))
// Check sensor status and convert string to numeric values
if sensor.Status.State == "OK" {
state = OK
} else {
state = BAD
}
(*msSwThermal)["moonshotSwitchSensorStatus"].WithLabelValues(namePrefix + "-" + tm.Name).Set(state)
}
return nil
}
// exportSwitchPowerMetrics collects the switches power metrics in json format and sets the prometheus gauges
func (e *Exporter) exportSwitchPowerMetrics(namePrefix string, body []byte) error {
var spm moonshot.SwPowerMetrics
var msSwPower = (*e.deviceMetrics)["swPowerMetrics"]
err := json.Unmarshal(body, &spm)
if err != nil {
return fmt.Errorf("Error Unmarshalling SwPowerMetrics - " + err.Error())
}
(*msSwPower)["moonshotSwitchSupplyOutput"].WithLabelValues(namePrefix + "-" + spm.Name).Set(float64(spm.Oem.Hp.InstantWattage))
return nil
}