forked from danjpar/ipmitool_exporter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
collector.go
987 lines (893 loc) · 24.9 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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
package main
import (
"bufio"
"bytes"
"math"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"syscall"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
const (
namespace = "ipmi"
targetLocal = ""
)
var (
fruBoardDateRegex = regexp.MustCompile(`\sBoard\sMfg\sDate\s*:\s*(?P<value>.*)`)
ipmiCurrentPowerRegex = regexp.MustCompile(`^Chassis\s*Power\s*is\s*(?P<value>on|off*)`)
ipSourceRegex = regexp.MustCompile(`^IP\sAddress\sSource\s*:\s*(?P<value>.*)`)
macAddressRegex = regexp.MustCompile(`^MAC\sAddress\s*:\s*(?P<value>.*)`)
defaultGatewayRegex = regexp.MustCompile(`^Default\sGateway\sIP\s*:\s*(?P<value>.*)`)
vlanIDRegex = regexp.MustCompile(`^802.1q\sVLAN\sID\s*:\s*(?P<value>.*)`)
vlanPriorityRegex = regexp.MustCompile(`^802.1q\sVLAN\sPriority\s*:\s*(?P<value>.*)`)
subnetMaskRegex = regexp.MustCompile(`^Subnet\sMask\s*:\s*(?P<value>.*)`)
firmwareRevRegex = regexp.MustCompile(`^Firmware\sRevision\s*:\s*(?P<value>.*)`)
ipmiVersionRegex = regexp.MustCompile(`^IPMI\sVersion\s*:\s*(?P<value>.*)`)
manufacturerRegex = regexp.MustCompile(`^Manufacturer\sName\s*:\s*(?P<value>.*)`)
dcmiAvgPowerRegex = regexp.MustCompile(`^\s*Average\spower\sreading\sover\ssample\speriod:\s*(?P<value>.*) Watts`)
dcmiInstaPowerRegex = regexp.MustCompile(`^\s*Instantaneous\spower\sreading:\s*(?P<value>.*) Watts`)
dcmiMinPowerRegex = regexp.MustCompile(`^\s*Minimum\sduring\ssampling\speriod:\s*(?P<value>.*) Watts`)
dcmiMaxPowerRegex = regexp.MustCompile(`^\s*Maximum\sduring\ssampling\speriod:\s*(?P<value>.*) Watts`)
)
type fruData struct {
Name string
Value string
}
type lanData struct {
Name string
Value string
}
type sensorData struct {
Name string
Value float64
Type string
State string
}
type dcmiPowerData struct {
Name string
Value float64
}
type fwumData struct {
Name string
Value float64
}
type bmcData struct {
Name string
Value string
}
type collector struct {
target string
module string
config *SafeConfig
}
type ipmiTarget struct {
host string
config IPMIConfig
}
var (
sensorStateDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "sensor", "state"),
"Indicates the severity of the state reported by an IPMI sensor (0=ok, 1=critical, 2=non-recoverable, 3=non-critical, 4=not-specified).",
[]string{"name", "type"},
nil,
)
sensorValueDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "sensor", "value"),
"Generic data read from an IPMI sensor of unknown type, relying on labels for context.",
[]string{"name", "type"},
nil,
)
chassisIntrusionDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "chassis_int", "value"),
"State of Chassis Intrusion.",
[]string{"name"},
nil,
)
chassisIntrusionStateDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "chassis_int", "state"),
"Reported state of a Chassis Intrusion (0=ok, 1=intrusion).",
[]string{"name"},
nil,
)
chassisPowerDeviceDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "chassis_power_dev", "value"),
"Chassis Power Supply device status (0=missing, 1=present).",
[]string{"name"},
nil,
)
chassisPowerDeviceStateDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "chassis_power_dev", "state"),
"Reported state of a Power Supply (0=missing, 1=present).",
[]string{"name"},
nil,
)
chassisPowerStateDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "power", "state"),
"Reported Chassis Power State (0=off, 1=on).",
[]string{"name"},
nil,
)
fanSpeedDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "fan_speed", "rpm"),
"Fan speed in rotations per minute.",
[]string{"name"},
nil,
)
fanSpeedStateDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "fan_speed", "state"),
"Reported state of a fan speed sensor (0=ok, 1=critical, 2=non-recoverable, 3=non-critical, 4=not-specified).",
[]string{"name"},
nil,
)
temperatureDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "temperature", "celsius"),
"Temperature reading in degree Celsius.",
[]string{"name"},
nil,
)
temperatureStateDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "temperature", "state"),
"Reported state of a temperature sensor (0=ok, 1=critical, 2=non-recoverable, 3=non-critical, 4=not-specified).",
[]string{"name"},
nil,
)
voltageDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "voltage", "volts"),
"Voltage reading in Volts.",
[]string{"name"},
nil,
)
voltageStateDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "voltage", "state"),
"Reported state of a voltage sensor (0=ok, 1=critical, 2=non-recoverable, 3=non-critical, 4=not-specified).",
[]string{"name"},
nil,
)
currentDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "current", "amperes"),
"Current reading in Amperes.",
[]string{"name"},
nil,
)
currentStateDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "current", "state"),
"Reported state of a current sensor (0=ok, 1=critical, 2=non-recoverable, 3=non-critical, 4=not-specified).",
[]string{"name"},
nil,
)
powerDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "power", "watts"),
"Power reading in Watts.",
[]string{"name"},
nil,
)
powerStateDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "sensor_power", "state"),
"Reported state of a power sensor (1=ok, 0=critical).",
[]string{"name"},
nil,
)
powerConsumptionDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "dcmi", "power_consumption_watts"),
"Current power consumption in Watts.",
[]string{"name"},
nil,
)
fwumInfo = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "fwum", "info"),
"Constant metric with value '1' providing details about the BMC.",
[]string{"firmware_revision", "manufacturer_id"},
nil,
)
bmcInfo = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "bmc", "info"),
"Constant metric with value '1' providing details about the BMC.",
[]string{"name", "value"},
nil,
)
fruInfo = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "fru", "info"),
"Constant metric with value '1' providing details from FRU.",
[]string{"name", "value"},
nil,
)
lanInfo = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "lan", "info"),
"Constant metric with value '1' providing details from LAN.",
[]string{"name", "value"},
nil,
)
upDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"'1' if a scrape of the IPMI device was successful, '0' otherwise.",
[]string{"collector"},
nil,
)
durationDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "scrape_duration", "seconds"),
"Returns how long the scrape took to complete in seconds.",
nil,
nil,
)
)
func ipmitoolConfig(config IPMIConfig) []string {
var args []string
if config.Privilege != "" {
args = append(args, "-L", config.Privilege)
}
if config.User != "" {
args = append(args, "-U", config.User)
}
if config.Password != "" {
args = append(args, "-P", config.Password)
}
if config.Timeout != 0 {
args = append(args, "-N", strconv.FormatInt(config.Timeout, 36))
}
return args
}
func ipmitoolOutput(target ipmiTarget, command string) (string, error) {
var cmdCommand []string
cmdConfig := ipmitoolConfig(target.config)
switch command {
case "sensor":
cmdCommand = append(cmdCommand, "sensor", "list")
case "fru":
cmdCommand = append(cmdCommand, "fru", "list")
case "power":
cmdCommand = append(cmdCommand, "power", "status")
case "fwum":
cmdCommand = append(cmdCommand, "fwum", "info")
case "bmc":
cmdCommand = append(cmdCommand, "bmc", "info")
case "lan":
cmdCommand = append(cmdCommand, "lan", "print")
case "dcmi-power":
cmdCommand = append(cmdCommand, "dcmi", "power", "reading", "1_min")
default:
log.Errorf("Unknown ipmitool command: '%s'\n", command)
cmdCommand = append(cmdCommand, "")
}
if target.host != "" {
cmdConfig = append(cmdConfig, "-H", target.host)
}
cmdConfig = append(cmdConfig, cmdCommand...)
cmd := exec.Command("ipmitool", cmdConfig...)
var outBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &outBuf
err := cmd.Run()
if err != nil {
if command == "fwum" {
// Because fwum return exit code 1 even if everything is OK.
// Be carefull with it and properly check command output later
// Yes, i know that next piece of code sucks, but can`t do anything
// with it right now
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
log.Debugf("Exit status of FWUM %d, but it was suppressed", status.ExitStatus())
}
}
} else {
log.Errorf("Error while calling %s for %s: %s", command, targetName(target.host), cmd)
//log.Fatal(err)
}
}
return outBuf.String(), err
}
func splitSensorOutput(impitoolOutput string) ([]sensorData, error) {
var result []sensorData
scanner := bufio.NewScanner(strings.NewReader(impitoolOutput))
var err error
for scanner.Scan() {
var data sensorData
line := scanner.Text()
if len(line) > 0 {
trimmedL := strings.ReplaceAll(line, " ", "")
splittedL := strings.Split(trimmedL, "|")
data.Name = splittedL[0]
valueS := splittedL[1]
convValueS, convErr := strconv.ParseUint(valueS, 0, 64)
if valueS != "na" && convErr != nil {
data.Value, err = strconv.ParseFloat(valueS, 64)
if err != nil {
continue
}
} else if valueS != "na" && convErr == nil {
data.Value = float64(convValueS)
} else {
data.Value = math.NaN()
}
data.Type = splittedL[2]
data.State = splittedL[3]
result = append(result, data)
}
}
return result, err
}
func splitDcmiPowerOutput(impitoolOutput string) ([]dcmiPowerData, error) {
var result []dcmiPowerData
scanner := bufio.NewScanner(strings.NewReader(impitoolOutput))
var err error
for scanner.Scan() {
var data dcmiPowerData
line := scanner.Text()
if len(line) > 0 {
dcmiAvgPower := dcmiAvgPowerRegex.FindStringSubmatch(line)
if dcmiAvgPower != nil {
for i, name := range dcmiAvgPowerRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "Avg power consumption"
data.Value, err = strconv.ParseFloat(dcmiAvgPower[i], 64)
if err != nil {
continue
}
result = append(result, data)
}
}
dcmiMinPower := dcmiMinPowerRegex.FindStringSubmatch(line)
if dcmiMinPower != nil {
for i, name := range dcmiMinPowerRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "Min power consumption"
data.Value, err = strconv.ParseFloat(dcmiMinPower[i], 64)
if err != nil {
continue
}
result = append(result, data)
}
}
dcmiMaxPower := dcmiMaxPowerRegex.FindStringSubmatch(line)
if dcmiMaxPower != nil {
for i, name := range dcmiMaxPowerRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "Max power consumption"
data.Value, err = strconv.ParseFloat(dcmiMaxPower[i], 64)
if err != nil {
continue
}
result = append(result, data)
}
}
dcmiInstaPower := dcmiInstaPowerRegex.FindStringSubmatch(line)
if dcmiInstaPower != nil {
for i, name := range dcmiInstaPowerRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "Instantaneous power consumption"
data.Value, err = strconv.ParseFloat(dcmiInstaPower[i], 64)
if err != nil {
continue
}
result = append(result, data)
}
}
}
}
return result, err
}
func splitFwumOutput(impitoolOutput string) ([]fwumData, error) {
var result []fwumData
scanner := bufio.NewScanner(strings.NewReader(impitoolOutput))
var err error
for scanner.Scan() {
var data fwumData
line := scanner.Text()
trimmedL := strings.ReplaceAll(line, " ", "")
re := regexp.MustCompile(`:`)
sanitizedL := re.FindStringSubmatch(trimmedL)
if sanitizedL != nil {
splittedL := strings.Split(trimmedL, ":")
data.Name = splittedL[0]
data.Value, err = strconv.ParseFloat(splittedL[1], 64)
if err != nil {
return result, err
}
}
result = append(result, data)
}
return result, err
}
func splitBmcOutput(impitoolOutput string) ([]bmcData, error) {
var result []bmcData
scanner := bufio.NewScanner(strings.NewReader(impitoolOutput))
var err error
for scanner.Scan() {
var data bmcData
line := scanner.Text()
if len(line) > 0 {
firmwareRev := firmwareRevRegex.FindStringSubmatch(line)
if firmwareRev != nil {
for i, name := range firmwareRevRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "FirmwareRevision"
data.Value = firmwareRev[i]
result = append(result, data)
break
}
continue
}
ipmiVersion := ipmiVersionRegex.FindStringSubmatch(line)
if ipmiVersion != nil {
for i, name := range ipmiVersionRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "IPMIVersion"
data.Value = ipmiVersion[i]
result = append(result, data)
break
}
continue
}
manufacturer := manufacturerRegex.FindStringSubmatch(line)
if manufacturer != nil {
for i, name := range manufacturerRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "Manufacturer"
data.Value = manufacturer[i]
result = append(result, data)
break
}
break
}
}
}
return result, err
}
func splitFruOutput(impitoolOutput string) ([]fruData, error) {
var result []fruData
scanner := bufio.NewScanner(strings.NewReader(impitoolOutput))
var err error
for scanner.Scan() {
var data fruData
line := scanner.Text()
if len(line) > 0 {
boardDate := fruBoardDateRegex.FindStringSubmatch(line)
if boardDate != nil {
for i, name := range fruBoardDateRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "BoardMfgDate"
data.Value = boardDate[i]
result = append(result, data)
break
}
continue
}
trimmedL := strings.ReplaceAll(line, " ", "")
splittedL := strings.Split(trimmedL, ":")
data.Name = splittedL[0]
data.Value = splittedL[1]
result = append(result, data)
}
}
return result, err
}
func splitLANOutput(impitoolOutput string) ([]lanData, error) {
var result []lanData
scanner := bufio.NewScanner(strings.NewReader(impitoolOutput))
var err error
for scanner.Scan() {
var data lanData
line := scanner.Text()
if len(line) > 0 {
ipSource := ipSourceRegex.FindStringSubmatch(line)
if ipSource != nil {
for i, name := range ipSourceRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "IPSource"
data.Value = strings.ReplaceAll(ipSource[i], " ", "")
result = append(result, data)
break
}
continue
}
subnetMask := subnetMaskRegex.FindStringSubmatch(line)
if subnetMask != nil {
for i, name := range subnetMaskRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "SubnetMask"
data.Value = subnetMask[i]
result = append(result, data)
break
}
continue
}
macMatch := macAddressRegex.FindStringSubmatch(line)
if macMatch != nil {
for i, name := range macAddressRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "MACAddress"
data.Value = macMatch[i]
result = append(result, data)
break
}
continue
}
defGateway := defaultGatewayRegex.FindStringSubmatch(line)
if defGateway != nil {
for i, name := range defaultGatewayRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "DefaultGateway"
data.Value = defGateway[i]
result = append(result, data)
break
}
continue
}
vlanID := vlanIDRegex.FindStringSubmatch(line)
if vlanID != nil {
for i, name := range vlanIDRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "VLANID"
data.Value = vlanID[i]
result = append(result, data)
break
}
continue
}
vlanPriority := vlanPriorityRegex.FindStringSubmatch(line)
if vlanPriority != nil {
for i, name := range vlanPriorityRegex.SubexpNames() {
if name != "value" {
continue
}
data.Name = "VLANPriority"
data.Value = vlanPriority[i]
result = append(result, data)
break
}
break
}
}
}
return result, err
}
func getChassisPowerState(ipmitoolOutput string) (int, error) {
scanner := bufio.NewScanner(strings.NewReader(ipmitoolOutput))
var err error
for scanner.Scan() {
line := scanner.Text()
if len(line) > 0 {
value := ipmiCurrentPowerRegex.FindStringSubmatch(line)[1]
if value == "on" {
return 1, err
}
}
}
return 0, err
}
// Describe implements Prometheus.Collector.
func (c collector) Describe(ch chan<- *prometheus.Desc) {
ch <- sensorStateDesc
ch <- sensorValueDesc
ch <- fanSpeedDesc
ch <- temperatureDesc
ch <- powerConsumptionDesc
ch <- upDesc
ch <- durationDesc
ch <- chassisPowerDeviceDesc
ch <- chassisIntrusionDesc
}
func collectTypedSensor(ch chan<- prometheus.Metric, desc, stateDesc *prometheus.Desc, state float64, data sensorData) {
ch <- prometheus.MustNewConstMetric(
desc,
prometheus.GaugeValue,
data.Value,
data.Name,
)
ch <- prometheus.MustNewConstMetric(
stateDesc,
prometheus.GaugeValue,
state,
data.Name,
)
}
func collectGenericSensor(ch chan<- prometheus.Metric, state float64, data sensorData) {
ch <- prometheus.MustNewConstMetric(
sensorValueDesc,
prometheus.GaugeValue,
data.Value,
data.Name,
data.Type,
)
ch <- prometheus.MustNewConstMetric(
sensorStateDesc,
prometheus.GaugeValue,
state,
data.Name,
data.Type,
)
}
func collectSensorMonitoring(ch chan<- prometheus.Metric, target ipmiTarget) (int, error) {
output, err := ipmitoolOutput(target, "sensor")
if err != nil {
log.Errorf("Failed to collect ipmitool sensor data from %s: %s", targetName(target.host), err)
return 0, err
}
results, err := splitSensorOutput(output)
if err != nil {
log.Errorf("Failed to parse ipmitool sensor data from %s: %s", targetName(target.host), err)
return 0, err
}
for _, data := range results {
var state float64
switch data.State {
case "ok":
state = 0
case "cr":
state = 1
case "nr":
state = 2
case "nc":
state = 3
case "ns":
state = 4
case "0x0000":
state = 0
case "0x0100":
state = 1
case "na":
state = math.NaN()
default:
log.Errorf("Unknown sensor state: '%s'\n", data.State)
state = math.NaN()
}
switch data.Type {
case "RPM":
collectTypedSensor(ch, fanSpeedDesc, fanSpeedStateDesc, state, data)
case "degrees C":
collectTypedSensor(ch, temperatureDesc, temperatureStateDesc, state, data)
case "Ampers":
collectTypedSensor(ch, currentDesc, currentStateDesc, state, data)
case "Volts":
collectTypedSensor(ch, voltageDesc, voltageStateDesc, state, data)
case "Watts":
collectTypedSensor(ch, powerDesc, powerStateDesc, state, data)
case "discrete":
if res, err := regexp.MatchString("ChassisIntru", data.Name); res {
if err != nil {
// TODO log error
collectTypedSensor(ch, chassisIntrusionDesc, chassisIntrusionStateDesc, state, data)
} else {
collectTypedSensor(ch, chassisIntrusionDesc, chassisIntrusionStateDesc, state, data)
}
} else if res, err := regexp.MatchString(`PS\dStatus*`, data.Name); res {
if err != nil {
// TODO log error
collectTypedSensor(ch, chassisPowerDeviceDesc, chassisPowerDeviceStateDesc, state, data)
} else {
collectTypedSensor(ch, chassisPowerDeviceDesc, chassisPowerDeviceStateDesc, state, data)
}
}
default:
collectGenericSensor(ch, state, data)
}
}
return 1, nil
}
func collectFRUInfo(ch chan<- prometheus.Metric, target ipmiTarget) (int, error) {
output, err := ipmitoolOutput(target, "fru")
if err != nil {
log.Debugf("Failed to collect ipmitool fru data from %s: %s", targetName(target.host), err)
return 0, err
}
results, err := splitFruOutput(output)
if err != nil {
log.Errorf("Failed to parse ipmitool fru data from %s: %s", targetName(target.host), err)
return 0, err
}
for _, data := range results {
ch <- prometheus.MustNewConstMetric(
fruInfo,
prometheus.GaugeValue,
1,
data.Name, data.Value,
)
}
return 1, nil
}
func collectLANInfo(ch chan<- prometheus.Metric, target ipmiTarget) (int, error) {
output, err := ipmitoolOutput(target, "lan")
if err != nil {
log.Debugf("Failed to collect ipmitool lan data from %s: %s", targetName(target.host), err)
return 0, err
}
results, err := splitLANOutput(output)
if err != nil {
log.Errorf("Failed to parse ipmitool lan data from %s: %s", targetName(target.host), err)
return 0, err
}
for _, data := range results {
ch <- prometheus.MustNewConstMetric(
lanInfo,
prometheus.GaugeValue,
1,
data.Name, data.Value,
)
}
return 1, nil
}
func collectBmcInfo(ch chan<- prometheus.Metric, target ipmiTarget) (int, error) {
output, err := ipmitoolOutput(target, "bmc")
if err != nil {
log.Debugf("Failed to collect ipmtool bmc data from %s: %s", targetName(target.host), err)
return 0, err
}
results, err := splitBmcOutput(output)
if err != nil {
log.Errorf("Failed to collect ipmtool bmc data from %s: %s", targetName(target.host), err)
return 0, err
}
for _, data := range results {
ch <- prometheus.MustNewConstMetric(
bmcInfo,
prometheus.GaugeValue,
1,
data.Name, data.Value,
)
}
return 1, nil
}
func collectDcmiPowerInfo(ch chan<- prometheus.Metric, target ipmiTarget) (int, error) {
output, err := ipmitoolOutput(target, "dcmi-power")
if err != nil {
log.Debugf("Failed to collect ipmtool dcmi power data from %s: %s", targetName(target.host), err)
return 0, err
}
results, err := splitDcmiPowerOutput(output)
if err != nil {
log.Errorf("Failed to collect ipmtool dcmi power data from %s: %s", targetName(target.host), err)
return 0, err
}
for _, data := range results {
ch <- prometheus.MustNewConstMetric(
powerConsumptionDesc,
prometheus.GaugeValue,
data.Value,
data.Name,
)
}
return 1, nil
}
func collectFwumInfo(ch chan<- prometheus.Metric, target ipmiTarget) (int, error) {
output, _ := ipmitoolOutput(target, "fwum")
// Then fwum collector will work without exit code 1 -- uncomment this error check:
// if err != nil {
// log.Debugf("Failed to collect ipmtool fwum data from %s: %s", targetName(target.host), err)
// return 0, err
// }
results, err := splitFwumOutput(output)
if err != nil {
log.Errorf("Failed to collect ipmtool fwum data from %s: %s", targetName(target.host), err)
return 0, err
}
var firmwareRevision, manufacturerID string
for _, data := range results {
switch data.Name {
case "FirmwareRevision":
firmwareRevision = strconv.FormatFloat(data.Value, 'f', 6, 64)
case "ManufacturerId":
manufacturerID = strconv.FormatFloat(data.Value, 'f', 6, 64)
}
}
ch <- prometheus.MustNewConstMetric(
fwumInfo,
prometheus.GaugeValue,
1,
firmwareRevision, manufacturerID,
)
return 1, nil
}
func collectPowerState(ch chan<- prometheus.Metric, target ipmiTarget) (int, error) {
output, err := ipmitoolOutput(target, "power")
if err != nil {
log.Debugf("Failed to collect ipmtool power data from %s: %s", targetName(target.host), err)
return 0, err
}
result, err := getChassisPowerState(output)
if err != nil {
log.Errorf("Failed to collect ipmtool power data from %s: %s", targetName(target.host), err)
return 0, err
}
ch <- prometheus.MustNewConstMetric(
chassisPowerStateDesc,
prometheus.GaugeValue,
float64(result),
"PowerState",
)
return 1, nil
}
func markCollectorUp(ch chan<- prometheus.Metric, name string, up int) {
ch <- prometheus.MustNewConstMetric(
upDesc,
prometheus.GaugeValue,
float64(up),
name,
)
}
// Collect implements Prometheus.Collector.
func (c collector) Collect(ch chan<- prometheus.Metric) {
start := time.Now()
defer func() {
duration := time.Since(start).Seconds()
log.Debugf("Scrape of target %s took %f seconds.", targetName(c.target), duration)
ch <- prometheus.MustNewConstMetric(
durationDesc,
prometheus.GaugeValue,
duration,
)
}()
config := c.config.ConfigForTarget(c.target, c.module)
target := ipmiTarget{
host: c.target,
config: config,
}
for _, collector := range config.Collectors {
var up int
log.Debugf("Running collector: %s", collector)
switch collector {
case "sensor":
up, _ = collectSensorMonitoring(ch, target)
case "fru":
up, _ = collectFRUInfo(ch, target)
case "lan":
up, _ = collectLANInfo(ch, target)
case "bmc":
up, _ = collectBmcInfo(ch, target)
case "fwum":
up, _ = collectFwumInfo(ch, target)
case "dcmi-power":
up, _ = collectDcmiPowerInfo(ch, target)
}
markCollectorUp(ch, collector, up)
}
collectPowerState(ch, target)
}
func contains(s []int64, elm int64) bool {
for _, a := range s {
if a == elm {
return true
}
}
return false
}
func escapePassword(password string) string {
return strings.Replace(password, "#", "\\#", -1)
}
func targetName(target string) string {
if targetIsLocal(target) {
return "[local]"
}
return target
}
func targetIsLocal(target string) bool {
return target == targetLocal
}