diff --git a/plugins/inputs/smart/smart.go b/plugins/inputs/smart/smart.go index b606b6f38..93d4a0076 100644 --- a/plugins/inputs/smart/smart.go +++ b/plugins/inputs/smart/smart.go @@ -253,18 +253,21 @@ func gatherDisk(acc telegraf.Accumulator, usesudo, collectAttributes bool, smart tags := map[string]string{} fields := make(map[string]interface{}) + if collectAttributes { + deviceNode := strings.Split(device, " ")[0] + tags["device"] = path.Base(deviceNode) + + if serial, ok := deviceTags["serial_no"]; ok { + tags["serial_no"] = serial + } + if wwn, ok := deviceTags["wwn"]; ok { + tags["wwn"] = wwn + } + } + attr := attribute.FindStringSubmatch(line) if len(attr) > 1 { if collectAttributes { - deviceNode := strings.Split(device, " ")[0] - tags["device"] = path.Base(deviceNode) - - if serial, ok := deviceTags["serial_no"]; ok { - tags["serial_no"] = serial - } - if wwn, ok := deviceTags["wwn"]; ok { - tags["wwn"] = wwn - } tags["id"] = attr[1] tags["name"] = attr[2] tags["flags"] = attr[3] diff --git a/plugins/inputs/smart/smart_test.go b/plugins/inputs/smart/smart_test.go index 525d99e3b..c801c7a61 100644 --- a/plugins/inputs/smart/smart_test.go +++ b/plugins/inputs/smart/smart_test.go @@ -4,7 +4,9 @@ import ( "errors" "sync" "testing" + "time" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -443,8 +445,61 @@ func TestGatherNvme(t *testing.T) { wg.Add(1) gatherDisk(acc, true, true, "", "", "", wg) - assert.Equal(t, 6, acc.NFields(), "Wrong number of fields gathered") - assert.Equal(t, uint64(4), acc.NMetrics(), "Wrong number of metrics gathered") + + expected := []telegraf.Metric{ + testutil.MustMetric("smart_device", + map[string]string{ + "device": ".", + "model": "TS128GMTE850", + "serial_no": "D704940282?", + }, + map[string]interface{}{ + "exit_status": 0, + "health_ok": true, + "temp_c": 38, + }, + time.Now(), + ), + testutil.MustMetric("smart_attribute", + map[string]string{ + "device": ".", + "id": "9", + "name": "Power_On_Hours", + "serial_no": "D704940282?", + }, + map[string]interface{}{ + "raw_value": 6038, + }, + time.Now(), + ), + testutil.MustMetric("smart_attribute", + map[string]string{ + "device": ".", + "id": "12", + "name": "Power_Cycle_Count", + "serial_no": "D704940282?", + }, + map[string]interface{}{ + "raw_value": 472, + }, + time.Now(), + ), + testutil.MustMetric("smart_attribute", + map[string]string{ + "device": ".", + "id": "194", + "name": "Temperature_Celsius", + "serial_no": "D704940282?", + }, + map[string]interface{}{ + "raw_value": 38, + }, + time.Now(), + ), + } + + testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), + testutil.SortMetrics(), testutil.IgnoreTime()) } // smartctl output