From 443ac6df23578b183e40abbe124a67dca45e4cc3 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Tue, 19 May 2020 08:20:29 -0700 Subject: [PATCH] Fix negative value parsing in impi_sensor input (#7541) --- plugins/inputs/ipmi_sensor/ipmi.go | 2 +- plugins/inputs/ipmi_sensor/ipmi_test.go | 78 +++++++++++++++++-------- 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/plugins/inputs/ipmi_sensor/ipmi.go b/plugins/inputs/ipmi_sensor/ipmi.go index 9ac842b89..fb53e1bc7 100644 --- a/plugins/inputs/ipmi_sensor/ipmi.go +++ b/plugins/inputs/ipmi_sensor/ipmi.go @@ -21,7 +21,7 @@ var ( execCommand = exec.Command // execCommand is used to mock commands in tests. re_v1_parse_line = regexp.MustCompile(`^(?P[^|]*)\|(?P[^|]*)\|(?P.*)`) re_v2_parse_line = regexp.MustCompile(`^(?P[^|]*)\|[^|]+\|(?P[^|]*)\|(?P[^|]*)\|(?:(?P[^|]+))?`) - re_v2_parse_description = regexp.MustCompile(`^(?P[0-9.]+)\s(?P.*)|(?P.+)|^$`) + re_v2_parse_description = regexp.MustCompile(`^(?P-?[0-9.]+)\s(?P.*)|(?P.+)|^$`) re_v2_parse_unit = regexp.MustCompile(`^(?P[^,]+)(?:,\s*(?P.*))?`) ) diff --git a/plugins/inputs/ipmi_sensor/ipmi_test.go b/plugins/inputs/ipmi_sensor/ipmi_test.go index 9d448435d..bd5e02c19 100644 --- a/plugins/inputs/ipmi_sensor/ipmi_test.go +++ b/plugins/inputs/ipmi_sensor/ipmi_test.go @@ -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) } }