Skip to content

Commit

Permalink
Fix negative value parsing in impi_sensor input (#7541)
Browse files Browse the repository at this point in the history
(cherry picked from commit 443ac6d)
  • Loading branch information
danielnelson committed May 19, 2020
1 parent f60d710 commit 0cc74c7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion plugins/inputs/ipmi_sensor/ipmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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>.*))?`)
)

Expand Down
78 changes: 54 additions & 24 deletions plugins/inputs/ipmi_sensor/ipmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -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,
},
Expand All @@ -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)
}
}

0 comments on commit 0cc74c7

Please sign in to comment.