Handle panic when ipmi_sensor input gets bad input (#4937)

This commit is contained in:
Greg 2018-10-30 15:05:41 -06:00 committed by GitHub
parent 563b6766ce
commit d0e6da5eba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"log"
"os/exec"
"regexp"
"strconv"
@ -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])
}

View File

@ -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])
}
}