Skip to content

Commit 337897e

Browse files
glintonotherpirate
authored andcommitted
Handle panic when ipmi_sensor input gets bad input (influxdata#4937)
1 parent f18faaa commit 337897e

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

plugins/inputs/ipmi_sensor/ipmi.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"fmt"
7+
"log"
78
"os/exec"
89
"regexp"
910
"strconv"
@@ -228,7 +229,12 @@ func parseV2(acc telegraf.Accumulator, hostname string, cmdOut []byte, measured_
228229
func extractFieldsFromRegex(re *regexp.Regexp, input string) map[string]string {
229230
submatches := re.FindStringSubmatch(input)
230231
results := make(map[string]string)
231-
for i, name := range re.SubexpNames() {
232+
subexpNames := re.SubexpNames()
233+
if len(subexpNames) > len(submatches) {
234+
log.Printf("D! No matches found in '%s'", input)
235+
return results
236+
}
237+
for i, name := range subexpNames {
232238
if name != input && name != "" && input != "" {
233239
results[name] = trim(submatches[i])
234240
}

plugins/inputs/ipmi_sensor/ipmi_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,41 @@ Power Supply 1 | 03h | ok | 10.1 | 110 Watts, Presence detected
572572
}
573573
os.Exit(0)
574574
}
575+
576+
func TestExtractFields(t *testing.T) {
577+
v1Data := `Ambient Temp | 20 degrees C | ok
578+
Altitude | 80 feet | ok
579+
Avg Power | 210 Watts | ok
580+
Planar 3.3V | 3.29 Volts | ok
581+
Planar 5V | 4.90 Volts | ok
582+
Planar 12V | 12.04 Volts | ok
583+
B | 0x00 | ok
584+
Unable to send command: Invalid argument
585+
ECC Corr Err | Not Readable | ns
586+
Unable to send command: Invalid argument
587+
ECC Uncorr Err | Not Readable | ns
588+
Unable to send command: Invalid argument
589+
`
590+
591+
v2Data := `SEL | 72h | ns | 7.1 | No Reading
592+
Intrusion | 73h | ok | 7.1 |
593+
Fan1 | 30h | ok | 7.1 | 5040 RPM
594+
Inlet Temp | 04h | ok | 7.1 | 25 degrees C
595+
USB Cable Pres | 50h | ok | 7.1 | Connected
596+
Unable to send command: Invalid argument
597+
Current 1 | 6Ah | ok | 10.1 | 7.20 Amps
598+
Unable to send command: Invalid argument
599+
Power Supply 1 | 03h | ok | 10.1 | 110 Watts, Presence detected
600+
`
601+
602+
tests := []string{
603+
v1Data,
604+
v2Data,
605+
}
606+
607+
for i := range tests {
608+
t.Logf("Checking v%d data...", i+1)
609+
extractFieldsFromRegex(re_v1_parse_line, tests[i])
610+
extractFieldsFromRegex(re_v2_parse_line, tests[i])
611+
}
612+
}

0 commit comments

Comments
 (0)