Skip to content

Commit

Permalink
Add device, serial_no, and wwn tags to synthetic attributes (influxda…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored and Mathieu Lecarme committed Apr 17, 2020
1 parent 719bdc4 commit 7eded1a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 11 deletions.
21 changes: 12 additions & 9 deletions plugins/inputs/smart/smart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
59 changes: 57 additions & 2 deletions plugins/inputs/smart/smart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7eded1a

Please sign in to comment.