Fix negative value parsing in impi_sensor input (#7541)

This commit is contained in:
Daniel Nelson 2020-05-19 08:20:29 -07:00 committed by GitHub
parent bf1eb291f2
commit 443ac6df23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 25 deletions

View File

@ -21,7 +21,7 @@ var (
execCommand = exec.Command // execCommand is used to mock commands in tests.
re_v1_parse_line = regexp.MustCompile(`^(?P<name>[^|]*)\|(?P<description>[^|]*)\|(?P<status_code>.*)`)
re_v2_parse_line = regexp.MustCompile(`^(?P<name>[^|]*)\|[^|]+\|(?P<status_code>[^|]*)\|(?P<entity_id>[^|]*)\|(?:(?P<description>[^|]+))?`)
re_v2_parse_description = regexp.MustCompile(`^(?P<analogValue>[0-9.]+)\s(?P<analogUnit>.*)|(?P<status>.+)|^$`)
re_v2_parse_description = regexp.MustCompile(`^(?P<analogValue>-?[0-9.]+)\s(?P<analogUnit>.*)|(?P<status>.+)|^$`)
re_v2_parse_unit = regexp.MustCompile(`^(?P<realAnalogUnit>[^,]+)(?:,\s*(?P<statusDesc>.*))?`)
)

View File

@ -7,6 +7,7 @@ import (
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
@ -664,11 +665,10 @@ func Test_parseV2(t *testing.T) {
measuredAt time.Time
}
tests := []struct {
name string
args args
wantFields map[string]interface{}
wantTags map[string]string
wantErr bool
name string
args args
expected []telegraf.Metric
wantErr bool
}{
{
name: "Test correct V2 parsing with analog value with unit",
@ -677,14 +677,19 @@ func Test_parseV2(t *testing.T) {
cmdOut: []byte("Power Supply 1 | 03h | ok | 10.1 | 110 Watts, Presence detected"),
measuredAt: time.Now(),
},
wantFields: map[string]interface{}{"value": float64(110)},
wantTags: map[string]string{
"name": "power_supply_1",
"status_code": "ok",
"server": "host",
"entity_id": "10.1",
"unit": "watts",
"status_desc": "presence_detected",
expected: []telegraf.Metric{
testutil.MustMetric("ipmi_sensor",
map[string]string{
"name": "power_supply_1",
"status_code": "ok",
"server": "host",
"entity_id": "10.1",
"unit": "watts",
"status_desc": "presence_detected",
},
map[string]interface{}{"value": 110.0},
time.Unix(0, 0),
),
},
wantErr: false,
},
@ -695,26 +700,51 @@ func Test_parseV2(t *testing.T) {
cmdOut: []byte("Intrusion | 73h | ok | 7.1 |"),
measuredAt: time.Now(),
},
wantFields: map[string]interface{}{"value": float64(0)},
wantTags: map[string]string{
"name": "intrusion",
"status_code": "ok",
"server": "host",
"entity_id": "7.1",
"status_desc": "ok",
expected: []telegraf.Metric{
testutil.MustMetric("ipmi_sensor",
map[string]string{
"name": "intrusion",
"status_code": "ok",
"server": "host",
"entity_id": "7.1",
"status_desc": "ok",
},
map[string]interface{}{"value": 0.0},
time.Unix(0, 0),
),
},
wantErr: false,
},
{
name: "parse negative value",
args: args{
hostname: "host",
cmdOut: []byte("DIMM Thrm Mrgn 1 | B0h | ok | 8.1 | -55 degrees C"),
measuredAt: time.Now(),
},
expected: []telegraf.Metric{
testutil.MustMetric("ipmi_sensor",
map[string]string{
"name": "dimm_thrm_mrgn_1",
"status_code": "ok",
"server": "host",
"entity_id": "8.1",
"unit": "degrees_c",
},
map[string]interface{}{"value": -55.0},
time.Unix(0, 0),
),
},
wantErr: false,
},
}
for _, tt := range tests {
var acc testutil.Accumulator
t.Run(tt.name, func(t *testing.T) {
var acc testutil.Accumulator
if err := parseV2(&acc, tt.args.hostname, tt.args.cmdOut, tt.args.measuredAt); (err != nil) != tt.wantErr {
t.Errorf("parseV2() error = %v, wantErr %v", err, tt.wantErr)
}
testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
})
acc.AssertContainsTaggedFields(t, "ipmi_sensor", tt.wantFields, tt.wantTags)
}
}