Skip to content

Commit

Permalink
Handle panic when ipmi_sensor input gets bad input (influxdata#4937)
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton authored and otherpirate committed Mar 15, 2019
1 parent 310636c commit 44e544a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion plugins/inputs/ipmi_sensor/ipmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"log"
"os/exec"
"regexp"
"strconv"
Expand Down Expand Up @@ -228,7 +229,12 @@ func parseV2(acc telegraf.Accumulator, hostname string, cmdOut []byte, measured_
func extractFieldsFromRegex(re *regexp.Regexp, input string) map[string]string {
submatches := re.FindStringSubmatch(input)
results := make(map[string]string)
for i, name := range re.SubexpNames() {
subexpNames := re.SubexpNames()
if len(subexpNames) > len(submatches) {
log.Printf("D! No matches found in '%s'", input)
return results
}
for i, name := range subexpNames {
if name != input && name != "" && input != "" {
results[name] = trim(submatches[i])
}
Expand Down
38 changes: 38 additions & 0 deletions plugins/inputs/ipmi_sensor/ipmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,41 @@ Power Supply 1 | 03h | ok | 10.1 | 110 Watts, Presence detected
}
os.Exit(0)
}

func TestExtractFields(t *testing.T) {
v1Data := `Ambient Temp | 20 degrees C | ok
Altitude | 80 feet | ok
Avg Power | 210 Watts | ok
Planar 3.3V | 3.29 Volts | ok
Planar 5V | 4.90 Volts | ok
Planar 12V | 12.04 Volts | ok
B | 0x00 | ok
Unable to send command: Invalid argument
ECC Corr Err | Not Readable | ns
Unable to send command: Invalid argument
ECC Uncorr Err | Not Readable | ns
Unable to send command: Invalid argument
`

v2Data := `SEL | 72h | ns | 7.1 | No Reading
Intrusion | 73h | ok | 7.1 |
Fan1 | 30h | ok | 7.1 | 5040 RPM
Inlet Temp | 04h | ok | 7.1 | 25 degrees C
USB Cable Pres | 50h | ok | 7.1 | Connected
Unable to send command: Invalid argument
Current 1 | 6Ah | ok | 10.1 | 7.20 Amps
Unable to send command: Invalid argument
Power Supply 1 | 03h | ok | 10.1 | 110 Watts, Presence detected
`

tests := []string{
v1Data,
v2Data,
}

for i := range tests {
t.Logf("Checking v%d data...", i+1)
extractFieldsFromRegex(re_v1_parse_line, tests[i])
extractFieldsFromRegex(re_v2_parse_line, tests[i])
}
}

0 comments on commit 44e544a

Please sign in to comment.