-
Notifications
You must be signed in to change notification settings - Fork 34
/
collector.go
491 lines (451 loc) · 14.2 KB
/
collector.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
package collector
import (
"fmt"
"runtime"
"strings"
"sync"
"sync/atomic"
"github.com/mrlhansen/idrac_exporter/internal/config"
"github.com/mrlhansen/idrac_exporter/internal/version"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)
var mu sync.Mutex
var collectors = map[string]*Collector{}
type Collector struct {
// Internal variables
client *Client
registry *prometheus.Registry
collected *sync.Cond
collecting bool
retries uint
errors atomic.Uint64
builder *strings.Builder
// Exporter
ExporterBuildInfo *prometheus.Desc
ExporterScrapeErrorsTotal *prometheus.Desc
// System
SystemPowerOn *prometheus.Desc
SystemHealth *prometheus.Desc
SystemIndicatorLED *prometheus.Desc
SystemMemorySize *prometheus.Desc
SystemCpuCount *prometheus.Desc
SystemBiosInfo *prometheus.Desc
SystemMachineInfo *prometheus.Desc
// Sensors
SensorsTemperature *prometheus.Desc
SensorsFanHealth *prometheus.Desc
SensorsFanSpeed *prometheus.Desc
// Power supply
PowerSupplyHealth *prometheus.Desc
PowerSupplyOutputWatts *prometheus.Desc
PowerSupplyInputWatts *prometheus.Desc
PowerSupplyCapacityWatts *prometheus.Desc
PowerSupplyInputVoltage *prometheus.Desc
PowerSupplyEfficiencyPercent *prometheus.Desc
// Power control
PowerControlConsumedWatts *prometheus.Desc
PowerControlCapacityWatts *prometheus.Desc
PowerControlMinConsumedWatts *prometheus.Desc
PowerControlMaxConsumedWatts *prometheus.Desc
PowerControlAvgConsumedWatts *prometheus.Desc
PowerControlInterval *prometheus.Desc
// System event log
SelEntry *prometheus.Desc
// Disk drives
DriveInfo *prometheus.Desc
DriveHealth *prometheus.Desc
DriveCapacity *prometheus.Desc
DriveLifeLeft *prometheus.Desc
// Memory modules
MemoryModuleInfo *prometheus.Desc
MemoryModuleHealth *prometheus.Desc
MemoryModuleCapacity *prometheus.Desc
MemoryModuleSpeed *prometheus.Desc
// Network
NetworkInterfaceHealth *prometheus.Desc
NetworkPortHealth *prometheus.Desc
NetworkPortSpeed *prometheus.Desc
NetworkPortLinkUp *prometheus.Desc
}
func NewCollector() *Collector {
prefix := config.Config.MetricsPrefix
collector := &Collector{
ExporterBuildInfo: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "exporter", "build_info"),
"Constant metric with build information for the exporter",
nil, prometheus.Labels{
"version": version.Version,
"revision": version.Revision,
"goversion": runtime.Version(),
},
),
ExporterScrapeErrorsTotal: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "exporter", "scrape_errors_total"),
"Total number of errors encountered while scraping target",
nil, nil,
),
SystemPowerOn: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "system", "power_on"),
"Power state of the system",
nil, nil,
),
SystemHealth: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "system", "health"),
"Health status of the system",
[]string{"status"}, nil,
),
SystemIndicatorLED: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "system", "indicator_led_on"),
"Indicator LED state of the system",
[]string{"state"}, nil,
),
SystemMemorySize: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "system", "memory_size_bytes"),
"Total memory size of the system in bytes",
nil, nil,
),
SystemCpuCount: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "system", "cpu_count"),
"Total number of CPUs in the system",
[]string{"model"}, nil,
),
SystemBiosInfo: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "system", "bios_info"),
"Information about the BIOS",
[]string{"version"}, nil,
),
SystemMachineInfo: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "system", "machine_info"),
"Information about the machine",
[]string{"manufacturer", "model", "serial", "sku"}, nil,
),
SensorsTemperature: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "sensors", "temperature"),
"Sensors reporting temperature measurements",
[]string{"id", "name", "units"}, nil,
),
SensorsFanHealth: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "sensors", "fan_health"),
"Health status for fans",
[]string{"id", "name", "status"}, nil,
),
SensorsFanSpeed: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "sensors", "fan_speed"),
"Sensors reporting fan speed measurements",
[]string{"id", "name", "units"}, nil,
),
PowerSupplyHealth: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "power_supply", "health"),
"Power supply health status",
[]string{"id", "status"}, nil,
),
PowerSupplyOutputWatts: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "power_supply", "output_watts"),
"Power supply output in watts",
[]string{"id"}, nil,
),
PowerSupplyInputWatts: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "power_supply", "input_watts"),
"Power supply input in watts",
[]string{"id"}, nil,
),
PowerSupplyCapacityWatts: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "power_supply", "capacity_watts"),
"Power supply capacity in watts",
[]string{"id"}, nil,
),
PowerSupplyInputVoltage: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "power_supply", "input_voltage"),
"Power supply input voltage",
[]string{"id"}, nil,
),
PowerSupplyEfficiencyPercent: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "power_supply", "efficiency_percent"),
"Power supply efficiency in percentage",
[]string{"id"}, nil,
),
PowerControlConsumedWatts: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "power_control", "consumed_watts"),
"Consumption of power control system in watts",
[]string{"id", "name"}, nil,
),
PowerControlCapacityWatts: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "power_control", "capacity_watts"),
"Capacity of power control system in watts",
[]string{"id", "name"}, nil,
),
PowerControlMinConsumedWatts: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "power_control", "min_consumed_watts"),
"Minimum consumption of power control system during the reported interval",
[]string{"id", "name"}, nil,
),
PowerControlMaxConsumedWatts: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "power_control", "max_consumed_watts"),
"Maximum consumption of power control system during the reported interval",
[]string{"id", "name"}, nil,
),
PowerControlAvgConsumedWatts: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "power_control", "avg_consumed_watts"),
"Average consumption of power control system during the reported interval",
[]string{"id", "name"}, nil,
),
PowerControlInterval: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "power_control", "interval_in_minutes"),
"Interval for measurements of power control system",
[]string{"id", "name"}, nil,
),
SelEntry: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "sel", "entry"),
"Entry from the system event log",
[]string{"id", "message", "component", "severity"}, nil,
),
DriveInfo: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "drive", "info"),
"Information about disk drives",
[]string{"id", "manufacturer", "mediatype", "model", "name", "protocol", "serial", "slot"}, nil,
),
DriveHealth: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "drive", "health"),
"Health status for disk drives",
[]string{"id", "status"}, nil,
),
DriveCapacity: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "drive", "capacity_bytes"),
"Capacity of disk drives in bytes",
[]string{"id"}, nil,
),
DriveLifeLeft: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "drive", "life_left_percent"),
"Predicted life left in percent",
[]string{"id"}, nil,
),
MemoryModuleInfo: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "memory_module", "info"),
"Information about memory modules",
[]string{"id", "ecc", "manufacturer", "type", "name", "serial", "rank"}, nil,
),
MemoryModuleHealth: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "memory_module", "health"),
"Health status for memory modules",
[]string{"id", "status"}, nil,
),
MemoryModuleCapacity: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "memory_module", "capacity_bytes"),
"Capacity of memory modules in bytes",
[]string{"id"}, nil,
),
MemoryModuleSpeed: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "memory_module", "speed_mhz"),
"Speed of memory modules in Mhz",
[]string{"id"}, nil,
),
NetworkInterfaceHealth: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "network_interface", "health"),
"Health status for network interfaces",
[]string{"id", "status"}, nil,
),
NetworkPortHealth: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "network_port", "health"),
"Health status for network ports",
[]string{"interface", "id", "status"}, nil,
),
NetworkPortSpeed: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "network_port", "speed_mbps"),
"Link speed of ports in Mbps",
[]string{"interface", "id"}, nil,
),
NetworkPortLinkUp: prometheus.NewDesc(
prometheus.BuildFQName(prefix, "network_port", "link_up"),
"Status of network ports, Up or Down",
[]string{"interface", "id", "status"}, nil,
),
}
collector.builder = new(strings.Builder)
collector.collected = sync.NewCond(new(sync.Mutex))
collector.registry = prometheus.NewRegistry()
collector.registry.Register(collector)
return collector
}
func (collector *Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.ExporterBuildInfo
ch <- collector.ExporterScrapeErrorsTotal
ch <- collector.SystemPowerOn
ch <- collector.SystemHealth
ch <- collector.SystemIndicatorLED
ch <- collector.SystemMemorySize
ch <- collector.SystemCpuCount
ch <- collector.SystemBiosInfo
ch <- collector.SystemMachineInfo
ch <- collector.SensorsTemperature
ch <- collector.SensorsFanHealth
ch <- collector.SensorsFanSpeed
ch <- collector.PowerSupplyHealth
ch <- collector.PowerSupplyOutputWatts
ch <- collector.PowerSupplyInputWatts
ch <- collector.PowerSupplyCapacityWatts
ch <- collector.PowerSupplyInputVoltage
ch <- collector.PowerSupplyEfficiencyPercent
ch <- collector.PowerControlConsumedWatts
ch <- collector.PowerControlCapacityWatts
ch <- collector.PowerControlMinConsumedWatts
ch <- collector.PowerControlMaxConsumedWatts
ch <- collector.PowerControlAvgConsumedWatts
ch <- collector.PowerControlInterval
ch <- collector.SelEntry
ch <- collector.DriveInfo
ch <- collector.DriveHealth
ch <- collector.DriveCapacity
ch <- collector.DriveLifeLeft
ch <- collector.MemoryModuleInfo
ch <- collector.MemoryModuleHealth
ch <- collector.MemoryModuleCapacity
ch <- collector.MemoryModuleSpeed
ch <- collector.NetworkInterfaceHealth
ch <- collector.NetworkPortHealth
ch <- collector.NetworkPortSpeed
ch <- collector.NetworkPortLinkUp
}
func (collector *Collector) Collect(ch chan<- prometheus.Metric) {
var wg sync.WaitGroup
if config.Config.Collect.System {
wg.Add(1)
go func() {
err := collector.client.RefreshSystem(collector, ch)
if err != nil {
collector.errors.Add(1)
}
wg.Done()
}()
}
if config.Config.Collect.Sensors {
wg.Add(1)
go func() {
err := collector.client.RefreshSensors(collector, ch)
if err != nil {
collector.errors.Add(1)
}
wg.Done()
}()
}
if config.Config.Collect.Power {
wg.Add(1)
go func() {
err := collector.client.RefreshPower(collector, ch)
if err != nil {
collector.errors.Add(1)
}
wg.Done()
}()
}
if config.Config.Collect.Network {
wg.Add(1)
go func() {
err := collector.client.RefreshNetwork(collector, ch)
if err != nil {
collector.errors.Add(1)
}
wg.Done()
}()
}
if config.Config.Collect.SEL {
wg.Add(1)
go func() {
err := collector.client.RefreshIdracSel(collector, ch)
if err != nil {
collector.errors.Add(1)
}
wg.Done()
}()
}
if config.Config.Collect.Storage {
wg.Add(1)
go func() {
err := collector.client.RefreshStorage(collector, ch)
if err != nil {
collector.errors.Add(1)
}
wg.Done()
}()
}
if config.Config.Collect.Memory {
wg.Add(1)
go func() {
err := collector.client.RefreshMemory(collector, ch)
if err != nil {
collector.errors.Add(1)
}
wg.Done()
}()
}
wg.Wait()
ch <- prometheus.MustNewConstMetric(collector.ExporterBuildInfo, prometheus.UntypedValue, 1)
ch <- prometheus.MustNewConstMetric(collector.ExporterScrapeErrorsTotal, prometheus.CounterValue, float64(collector.errors.Load()))
}
func (collector *Collector) Gather() (string, error) {
collector.collected.L.Lock()
// If a collection is already in progress wait for it to complete and return the cached data
if collector.collecting {
collector.collected.Wait()
metrics := collector.builder.String()
collector.collected.L.Unlock()
return metrics, nil
}
// Set collecting to true and let other goroutines enter in critical section
collector.collecting = true
collector.collected.L.Unlock()
// Defer set collecting to false and wake waiting goroutines
defer func() {
collector.collected.L.Lock()
collector.collected.Broadcast()
collector.collecting = false
collector.collected.L.Unlock()
}()
// Collect metrics
collector.builder.Reset()
m, err := collector.registry.Gather()
if err != nil {
return "", err
}
for i := range m {
expfmt.MetricFamilyToText(collector.builder, m[i])
}
return collector.builder.String(), nil
}
// Resets an existing collector of the given target
func Reset(target string) {
mu.Lock()
_, ok := collectors[target]
if ok {
delete(collectors, target)
}
mu.Unlock()
}
func GetCollector(target string) (*Collector, error) {
mu.Lock()
collector, ok := collectors[target]
if !ok {
collector = NewCollector()
collectors[target] = collector
}
mu.Unlock()
// Do not act concurrently on the same host
collector.collected.L.Lock()
defer collector.collected.L.Unlock()
// Try to instantiate a new Redfish host
if collector.client == nil {
if collector.retries < config.Config.Retries {
host := config.Config.GetHostCfg(target)
c, err := NewClient(host)
if err != nil {
collector.retries++
return nil, err
} else {
collector.client = c
}
} else {
return nil, fmt.Errorf("host unreachable after %d retries", collector.retries)
}
}
return collector, nil
}