/
redfish.go
536 lines (479 loc) · 14.2 KB
/
redfish.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//go:generate ../../../tools/readme_config_includer/generator
package redfish
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"path"
"slices"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
type Redfish struct {
Address string `toml:"address"`
Username config.Secret `toml:"username"`
Password config.Secret `toml:"password"`
ComputerSystemID string `toml:"computer_system_id"`
IncludeMetrics []string `toml:"include_metrics"`
IncludeTagSets []string `toml:"include_tag_sets"`
Workarounds []string `toml:"workarounds"`
Timeout config.Duration `toml:"timeout"`
tagSet map[string]bool
client http.Client
tls.ClientConfig
baseURL *url.URL
}
const (
// tag sets used for including redfish OData link parent data
tagSetChassisLocation = "chassis.location"
tagSetChassis = "chassis"
)
type System struct {
Hostname string `json:"hostname"`
Links struct {
Chassis []struct {
Ref string `json:"@odata.id"`
}
}
}
type Chassis struct {
ChassisType string
Location *Location
Manufacturer string
Model string
PartNumber string
Power struct {
Ref string `json:"@odata.id"`
}
PowerState string
SKU string
SerialNumber string
Status Status
Thermal struct {
Ref string `json:"@odata.id"`
}
}
type Power struct {
PowerControl []struct {
Name string
MemberID string
PowerAllocatedWatts *float64
PowerAvailableWatts *float64
PowerCapacityWatts *float64
PowerConsumedWatts *float64
PowerRequestedWatts *float64
PowerMetrics struct {
AverageConsumedWatts *float64
IntervalInMin int
MaxConsumedWatts *float64
MinConsumedWatts *float64
}
}
PowerSupplies []struct {
Name string
MemberID string
PowerInputWatts *float64
PowerCapacityWatts *float64
PowerOutputWatts *float64
LastPowerOutputWatts *float64
Status Status
LineInputVoltage *float64
}
Voltages []struct {
Name string
MemberID string
ReadingVolts *float64
UpperThresholdCritical *float64
UpperThresholdFatal *float64
LowerThresholdCritical *float64
LowerThresholdFatal *float64
Status Status
}
}
type Thermal struct {
Fans []struct {
Name string
MemberID string
FanName string
CurrentReading *int64
Reading *int64
ReadingUnits *string
UpperThresholdCritical *int64
UpperThresholdFatal *int64
LowerThresholdCritical *int64
LowerThresholdFatal *int64
Status Status
}
Temperatures []struct {
Name string
MemberID string
ReadingCelsius *float64
UpperThresholdCritical *float64
UpperThresholdFatal *float64
LowerThresholdCritical *float64
LowerThresholdFatal *float64
Status Status
}
}
type Location struct {
PostalAddress struct {
DataCenter string
Room string
}
Placement struct {
Rack string
Row string
}
}
type Status struct {
State string
Health string
}
func (*Redfish) SampleConfig() string {
return sampleConfig
}
func (r *Redfish) Init() error {
if r.Address == "" {
return errors.New("did not provide IP")
}
if r.Username.Empty() && r.Password.Empty() {
return errors.New("did not provide username and password")
}
if r.ComputerSystemID == "" {
return errors.New("did not provide the computer system ID of the resource")
}
if len(r.IncludeMetrics) == 0 {
return errors.New("no metrics specified to collect")
}
for _, metric := range r.IncludeMetrics {
switch metric {
case "thermal", "power":
default:
return fmt.Errorf("unknown metric requested: %s", metric)
}
}
for _, workaround := range r.Workarounds {
switch workaround {
case "ilo4-thermal":
default:
return fmt.Errorf("unknown workaround requested: %s", workaround)
}
}
r.tagSet = make(map[string]bool, len(r.IncludeTagSets))
for _, setLabel := range r.IncludeTagSets {
r.tagSet[setLabel] = true
}
var err error
r.baseURL, err = url.Parse(r.Address)
if err != nil {
return err
}
tlsCfg, err := r.ClientConfig.TLSConfig()
if err != nil {
return err
}
r.client = http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsCfg,
Proxy: http.ProxyFromEnvironment,
},
Timeout: time.Duration(r.Timeout),
}
return nil
}
func (r *Redfish) getData(address string, payload interface{}) error {
req, err := http.NewRequest("GET", address, nil)
if err != nil {
return err
}
username, err := r.Username.Get()
if err != nil {
return fmt.Errorf("getting username failed: %w", err)
}
user := username.String()
username.Destroy()
password, err := r.Password.Get()
if err != nil {
return fmt.Errorf("getting password failed: %w", err)
}
pass := password.String()
password.Destroy()
req.SetBasicAuth(user, pass)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("OData-Version", "4.0")
// workaround for iLO4 thermal data
if slices.Contains(r.Workarounds, "ilo4-thermal") && strings.Contains(address, "/Thermal") {
req.Header.Del("OData-Version")
}
resp, err := r.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("received status code %d (%s) for address %s, expected 200",
resp.StatusCode,
http.StatusText(resp.StatusCode),
r.Address)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, &payload)
if err != nil {
return fmt.Errorf("error parsing input: %w", err)
}
return nil
}
func (r *Redfish) getComputerSystem(id string) (*System, error) {
loc := r.baseURL.ResolveReference(&url.URL{Path: path.Join("/redfish/v1/Systems/", id)})
system := &System{}
err := r.getData(loc.String(), system)
if err != nil {
return nil, err
}
return system, nil
}
func (r *Redfish) getChassis(ref string) (*Chassis, error) {
loc := r.baseURL.ResolveReference(&url.URL{Path: ref})
chassis := &Chassis{}
err := r.getData(loc.String(), chassis)
if err != nil {
return nil, err
}
return chassis, nil
}
func (r *Redfish) getPower(ref string) (*Power, error) {
loc := r.baseURL.ResolveReference(&url.URL{Path: ref})
power := &Power{}
err := r.getData(loc.String(), power)
if err != nil {
return nil, err
}
return power, nil
}
func (r *Redfish) getThermal(ref string) (*Thermal, error) {
loc := r.baseURL.ResolveReference(&url.URL{Path: ref})
thermal := &Thermal{}
err := r.getData(loc.String(), thermal)
if err != nil {
return nil, err
}
return thermal, nil
}
func setChassisTags(chassis *Chassis, tags map[string]string) {
tags["chassis_chassistype"] = chassis.ChassisType
tags["chassis_manufacturer"] = chassis.Manufacturer
tags["chassis_model"] = chassis.Model
tags["chassis_partnumber"] = chassis.PartNumber
tags["chassis_powerstate"] = chassis.PowerState
tags["chassis_sku"] = chassis.SKU
tags["chassis_serialnumber"] = chassis.SerialNumber
tags["chassis_state"] = chassis.Status.State
tags["chassis_health"] = chassis.Status.Health
}
func (r *Redfish) Gather(acc telegraf.Accumulator) error {
address, _, err := net.SplitHostPort(r.baseURL.Host)
if err != nil {
address = r.baseURL.Host
}
system, err := r.getComputerSystem(r.ComputerSystemID)
if err != nil {
return err
}
for _, link := range system.Links.Chassis {
chassis, err := r.getChassis(link.Ref)
if err != nil {
return err
}
for _, metric := range r.IncludeMetrics {
var err error
switch metric {
case "thermal":
err = r.gatherThermal(acc, address, system, chassis)
case "power":
err = r.gatherPower(acc, address, system, chassis)
default:
return fmt.Errorf("unknown metric requested: %s", metric)
}
if err != nil {
return err
}
}
}
return nil
}
func (r *Redfish) gatherThermal(acc telegraf.Accumulator, address string, system *System, chassis *Chassis) error {
thermal, err := r.getThermal(chassis.Thermal.Ref)
if err != nil {
return err
}
for _, j := range thermal.Temperatures {
tags := map[string]string{}
tags["member_id"] = j.MemberID
tags["address"] = address
tags["name"] = j.Name
tags["source"] = system.Hostname
tags["state"] = j.Status.State
tags["health"] = j.Status.Health
if _, ok := r.tagSet[tagSetChassisLocation]; ok && chassis.Location != nil {
tags["datacenter"] = chassis.Location.PostalAddress.DataCenter
tags["room"] = chassis.Location.PostalAddress.Room
tags["rack"] = chassis.Location.Placement.Rack
tags["row"] = chassis.Location.Placement.Row
}
if _, ok := r.tagSet[tagSetChassis]; ok {
setChassisTags(chassis, tags)
}
fields := make(map[string]interface{})
fields["reading_celsius"] = j.ReadingCelsius
fields["upper_threshold_critical"] = j.UpperThresholdCritical
fields["upper_threshold_fatal"] = j.UpperThresholdFatal
fields["lower_threshold_critical"] = j.LowerThresholdCritical
fields["lower_threshold_fatal"] = j.LowerThresholdFatal
acc.AddFields("redfish_thermal_temperatures", fields, tags)
}
for _, j := range thermal.Fans {
tags := map[string]string{}
fields := make(map[string]interface{})
tags["member_id"] = j.MemberID
tags["address"] = address
tags["name"] = j.Name
if j.FanName != "" {
tags["name"] = j.FanName
}
tags["source"] = system.Hostname
tags["state"] = j.Status.State
tags["health"] = j.Status.Health
if _, ok := r.tagSet[tagSetChassisLocation]; ok && chassis.Location != nil {
tags["datacenter"] = chassis.Location.PostalAddress.DataCenter
tags["room"] = chassis.Location.PostalAddress.Room
tags["rack"] = chassis.Location.Placement.Rack
tags["row"] = chassis.Location.Placement.Row
}
if _, ok := r.tagSet[tagSetChassis]; ok {
setChassisTags(chassis, tags)
}
if j.ReadingUnits != nil && *j.ReadingUnits == "RPM" {
fields["upper_threshold_critical"] = j.UpperThresholdCritical
fields["upper_threshold_fatal"] = j.UpperThresholdFatal
fields["lower_threshold_critical"] = j.LowerThresholdCritical
fields["lower_threshold_fatal"] = j.LowerThresholdFatal
fields["reading_rpm"] = j.Reading
} else if j.CurrentReading != nil {
fields["reading_percent"] = j.CurrentReading
} else {
fields["reading_percent"] = j.Reading
}
acc.AddFields("redfish_thermal_fans", fields, tags)
}
return nil
}
func (r *Redfish) gatherPower(acc telegraf.Accumulator, address string, system *System, chassis *Chassis) error {
power, err := r.getPower(chassis.Power.Ref)
if err != nil {
return err
}
for _, j := range power.PowerControl {
tags := map[string]string{
"member_id": j.MemberID,
"address": address,
"name": j.Name,
"source": system.Hostname,
}
if _, ok := r.tagSet[tagSetChassisLocation]; ok && chassis.Location != nil {
tags["datacenter"] = chassis.Location.PostalAddress.DataCenter
tags["room"] = chassis.Location.PostalAddress.Room
tags["rack"] = chassis.Location.Placement.Rack
tags["row"] = chassis.Location.Placement.Row
}
if _, ok := r.tagSet[tagSetChassis]; ok {
setChassisTags(chassis, tags)
}
fields := map[string]interface{}{
"power_allocated_watts": j.PowerAllocatedWatts,
"power_available_watts": j.PowerAvailableWatts,
"power_capacity_watts": j.PowerCapacityWatts,
"power_consumed_watts": j.PowerConsumedWatts,
"power_requested_watts": j.PowerRequestedWatts,
"average_consumed_watts": j.PowerMetrics.AverageConsumedWatts,
"interval_in_min": j.PowerMetrics.IntervalInMin,
"max_consumed_watts": j.PowerMetrics.MaxConsumedWatts,
"min_consumed_watts": j.PowerMetrics.MinConsumedWatts,
}
acc.AddFields("redfish_power_powercontrol", fields, tags)
}
for _, j := range power.PowerSupplies {
tags := map[string]string{}
tags["member_id"] = j.MemberID
tags["address"] = address
tags["name"] = j.Name
tags["source"] = system.Hostname
tags["state"] = j.Status.State
tags["health"] = j.Status.Health
if _, ok := r.tagSet[tagSetChassisLocation]; ok && chassis.Location != nil {
tags["datacenter"] = chassis.Location.PostalAddress.DataCenter
tags["room"] = chassis.Location.PostalAddress.Room
tags["rack"] = chassis.Location.Placement.Rack
tags["row"] = chassis.Location.Placement.Row
}
if _, ok := r.tagSet[tagSetChassis]; ok {
setChassisTags(chassis, tags)
}
fields := make(map[string]interface{})
fields["power_input_watts"] = j.PowerInputWatts
fields["power_output_watts"] = j.PowerOutputWatts
fields["line_input_voltage"] = j.LineInputVoltage
fields["last_power_output_watts"] = j.LastPowerOutputWatts
fields["power_capacity_watts"] = j.PowerCapacityWatts
acc.AddFields("redfish_power_powersupplies", fields, tags)
}
for _, j := range power.Voltages {
tags := map[string]string{}
tags["member_id"] = j.MemberID
tags["address"] = address
tags["name"] = j.Name
tags["source"] = system.Hostname
tags["state"] = j.Status.State
tags["health"] = j.Status.Health
if _, ok := r.tagSet[tagSetChassisLocation]; ok && chassis.Location != nil {
tags["datacenter"] = chassis.Location.PostalAddress.DataCenter
tags["room"] = chassis.Location.PostalAddress.Room
tags["rack"] = chassis.Location.Placement.Rack
tags["row"] = chassis.Location.Placement.Row
}
if _, ok := r.tagSet[tagSetChassis]; ok {
setChassisTags(chassis, tags)
}
fields := make(map[string]interface{})
fields["reading_volts"] = j.ReadingVolts
fields["upper_threshold_critical"] = j.UpperThresholdCritical
fields["upper_threshold_fatal"] = j.UpperThresholdFatal
fields["lower_threshold_critical"] = j.LowerThresholdCritical
fields["lower_threshold_fatal"] = j.LowerThresholdFatal
acc.AddFields("redfish_power_voltages", fields, tags)
}
return nil
}
func init() {
inputs.Add("redfish", func() telegraf.Input {
return &Redfish{
// default tag set of chassis.location required for backwards compatibility
IncludeTagSets: []string{tagSetChassisLocation},
IncludeMetrics: []string{"power", "thermal"},
}
})
}