forked from SumoLogic/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redfish.go
380 lines (331 loc) · 9.48 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
package redfish
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"path"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs"
)
const description = "Read CPU, Fans, Powersupply and Voltage metrics of hardware server through redfish APIs"
const sampleConfig = `
## Server url
address = "https://127.0.0.1:5000"
## Username, Password for hardware server
username = "root"
password = "password123456"
## ComputerSystemId
computer_system_id="2M220100SL"
## Amount of time allowed to complete the HTTP request
# timeout = "5s"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
`
type Redfish struct {
Address string `toml:"address"`
Username string `toml:"username"`
Password string `toml:"password"`
ComputerSystemId string `toml:"computer_system_id"`
Timeout config.Duration `toml:"timeout"`
client http.Client
tls.ClientConfig
baseURL *url.URL
}
type System struct {
Hostname string `json:"hostname"`
Links struct {
Chassis []struct {
Ref string `json:"@odata.id"`
}
}
}
type Chassis struct {
Location *Location
Power struct {
Ref string `json:"@odata.id"`
}
Thermal struct {
Ref string `json:"@odata.id"`
}
}
type Power struct {
PowerSupplies []struct {
Name string
PowerInputWatts *float64
PowerCapacityWatts *float64
PowerOutputWatts *float64
LastPowerOutputWatts *float64
Status Status
LineInputVoltage *float64
}
Voltages []struct {
Name string
ReadingVolts *float64
UpperThresholdCritical *float64
UpperThresholdFatal *float64
LowerThresholdCritical *float64
LowerThresholdFatal *float64
Status Status
}
}
type Thermal struct {
Fans []struct {
Name string
Reading *int64
ReadingUnits *string
UpperThresholdCritical *int64
UpperThresholdFatal *int64
LowerThresholdCritical *int64
LowerThresholdFatal *int64
Status Status
}
Temperatures []struct {
Name 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 (r *Redfish) Description() string {
return description
}
func (r *Redfish) SampleConfig() string {
return sampleConfig
}
func (r *Redfish) Init() error {
if r.Address == "" {
return fmt.Errorf("did not provide IP")
}
if r.Username == "" && r.Password == "" {
return fmt.Errorf("did not provide username and password")
}
if r.ComputerSystemId == "" {
return fmt.Errorf("did not provide the computer system ID of the resource")
}
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(url string, payload interface{}) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.SetBasicAuth(r.Username, r.Password)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
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), expected 200",
resp.StatusCode,
http.StatusText(resp.StatusCode))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, &payload)
if err != nil {
return fmt.Errorf("error parsing input: %v", 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 (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
}
thermal, err := r.getThermal(chassis.Thermal.Ref)
if err != nil {
return err
}
for _, j := range thermal.Temperatures {
tags := map[string]string{}
tags["address"] = address
tags["name"] = j.Name
tags["source"] = system.Hostname
tags["state"] = j.Status.State
tags["health"] = j.Status.Health
if 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
}
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["address"] = address
tags["name"] = j.Name
tags["source"] = system.Hostname
tags["state"] = j.Status.State
tags["health"] = j.Status.Health
if 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 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 {
fields["reading_percent"] = j.Reading
}
acc.AddFields("redfish_thermal_fans", fields, tags)
}
power, err := r.getPower(chassis.Power.Ref)
if err != nil {
return err
}
for _, j := range power.PowerSupplies {
tags := map[string]string{}
tags["address"] = address
tags["name"] = j.Name
tags["source"] = system.Hostname
tags["state"] = j.Status.State
tags["health"] = j.Status.Health
if 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
}
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["address"] = address
tags["name"] = j.Name
tags["source"] = system.Hostname
tags["state"] = j.Status.State
tags["health"] = j.Status.Health
if 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
}
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{}
})
}