Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle panic when ipmi_sensor input gets bad input #4937

Merged
merged 4 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
}
}