From 39537ed86ea681f93a150f98a1c943b8484a730d Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Fri, 8 Dec 2017 13:22:41 -0800 Subject: [PATCH] Use device name instead of abs path for devices tag in smart input (#3550) (cherry picked from commit 574034c30173d7cd480ec4abbcaf749d084d0a4a) --- plugins/inputs/smart/README.md | 6 +++--- plugins/inputs/smart/smart.go | 11 +++++++---- plugins/inputs/smart/smart_test.go | 28 ++++++++++++++-------------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/plugins/inputs/smart/README.md b/plugins/inputs/smart/README.md index a81b344a5..6af962793 100644 --- a/plugins/inputs/smart/README.md +++ b/plugins/inputs/smart/README.md @@ -129,7 +129,7 @@ the configuration to execute that. Example output from an _Apple SSD_: ``` -> smart_attribute,serial_no=S1K5NYCD964433,wwn=5002538655584d30,id=199,name=UDMA_CRC_Error_Count,flags=-O-RC-,fail=-,host=mbpro.local,device=/dev/rdisk0 threshold=0i,raw_value=0i,exit_status=0i,value=200i,worst=200i 1502536854000000000 -> smart_attribute,device=/dev/rdisk0,serial_no=S1K5NYCD964433,wwn=5002538655584d30,id=240,name=Unknown_SSD_Attribute,flags=-O---K,fail=-,host=mbpro.local exit_status=0i,value=100i,worst=100i,threshold=0i,raw_value=0i 1502536854000000000 -> smart_device,enabled=Enabled,host=mbpro.local,device=/dev/rdisk0,model=APPLE\ SSD\ SM0512F,serial_no=S1K5NYCD964433,wwn=5002538655584d30,capacity=500277790720 udma_crc_errors=0i,exit_status=0i,health_ok=true,read_error_rate=0i,temp_c=40i 1502536854000000000 +> smart_attribute,serial_no=S1K5NYCD964433,wwn=5002538655584d30,id=199,name=UDMA_CRC_Error_Count,flags=-O-RC-,fail=-,host=mbpro.local,device=rdisk0 threshold=0i,raw_value=0i,exit_status=0i,value=200i,worst=200i 1502536854000000000 +> smart_attribute,device=rdisk0,serial_no=S1K5NYCD964433,wwn=5002538655584d30,id=240,name=Unknown_SSD_Attribute,flags=-O---K,fail=-,host=mbpro.local exit_status=0i,value=100i,worst=100i,threshold=0i,raw_value=0i 1502536854000000000 +> smart_device,enabled=Enabled,host=mbpro.local,device=rdisk0,model=APPLE\ SSD\ SM0512F,serial_no=S1K5NYCD964433,wwn=5002538655584d30,capacity=500277790720 udma_crc_errors=0i,exit_status=0i,health_ok=true,read_error_rate=0i,temp_c=40i 1502536854000000000 ``` diff --git a/plugins/inputs/smart/smart.go b/plugins/inputs/smart/smart.go index a754d1ace..dda115d02 100644 --- a/plugins/inputs/smart/smart.go +++ b/plugins/inputs/smart/smart.go @@ -3,6 +3,7 @@ package smart import ( "fmt" "os/exec" + "path" "regexp" "strconv" "strings" @@ -178,13 +179,13 @@ func exitStatus(err error) (int, error) { return 0, err } -func gatherDisk(acc telegraf.Accumulator, usesudo, attributes bool, path, nockeck, device string, wg *sync.WaitGroup) { +func gatherDisk(acc telegraf.Accumulator, usesudo, attributes bool, smartctl, nockeck, device string, wg *sync.WaitGroup) { defer wg.Done() // smartctl 5.41 & 5.42 have are broken regarding handling of --nocheck/-n args := []string{"--info", "--health", "--attributes", "--tolerance=verypermissive", "-n", nockeck, "--format=brief"} args = append(args, strings.Split(device, " ")...) - cmd := sudo(usesudo, path, args...) + cmd := sudo(usesudo, smartctl, args...) out, e := internal.CombinedOutputTimeout(cmd, time.Second*5) outStr := string(out) @@ -196,7 +197,8 @@ func gatherDisk(acc telegraf.Accumulator, usesudo, attributes bool, path, nockec } device_tags := map[string]string{} - device_tags["device"] = strings.Split(device, " ")[0] + device_node := strings.Split(device, " ")[0] + device_tags["device"] = path.Base(device_node) device_fields := make(map[string]interface{}) device_fields["exit_status"] = exitStatus @@ -240,7 +242,8 @@ func gatherDisk(acc telegraf.Accumulator, usesudo, attributes bool, path, nockec tags := map[string]string{} fields := make(map[string]interface{}) - tags["device"] = strings.Split(device, " ")[0] + device_node := strings.Split(device, " ")[0] + tags["device"] = path.Base(device_node) if serial, ok := device_tags["serial_no"]; ok { tags["serial_no"] = serial diff --git a/plugins/inputs/smart/smart_test.go b/plugins/inputs/smart/smart_test.go index c8e777033..da658f5f9 100644 --- a/plugins/inputs/smart/smart_test.go +++ b/plugins/inputs/smart/smart_test.go @@ -89,7 +89,7 @@ func TestGatherAttributes(t *testing.T) { "exit_status": int(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", "id": "1", @@ -107,7 +107,7 @@ func TestGatherAttributes(t *testing.T) { "exit_status": int(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", "id": "5", @@ -125,7 +125,7 @@ func TestGatherAttributes(t *testing.T) { "exit_status": int(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", "id": "9", @@ -143,7 +143,7 @@ func TestGatherAttributes(t *testing.T) { "exit_status": int(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", "id": "12", @@ -161,7 +161,7 @@ func TestGatherAttributes(t *testing.T) { "exit_status": int(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", "id": "169", @@ -179,7 +179,7 @@ func TestGatherAttributes(t *testing.T) { "exit_status": int(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", "id": "173", @@ -197,7 +197,7 @@ func TestGatherAttributes(t *testing.T) { "exit_status": int(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", "id": "190", @@ -215,7 +215,7 @@ func TestGatherAttributes(t *testing.T) { "exit_status": int(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", "id": "192", @@ -233,7 +233,7 @@ func TestGatherAttributes(t *testing.T) { "exit_status": int(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", "id": "194", @@ -251,7 +251,7 @@ func TestGatherAttributes(t *testing.T) { "exit_status": int(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", "id": "197", @@ -269,7 +269,7 @@ func TestGatherAttributes(t *testing.T) { "exit_status": int(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", "id": "199", @@ -287,7 +287,7 @@ func TestGatherAttributes(t *testing.T) { "exit_status": int(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", "id": "240", @@ -317,7 +317,7 @@ func TestGatherAttributes(t *testing.T) { "udma_crc_errors": int64(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "model": "APPLE SSD SM256E", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30", @@ -363,7 +363,7 @@ func TestGatherNoAttributes(t *testing.T) { "udma_crc_errors": int64(0), }, map[string]string{ - "device": "/dev/ada0", + "device": "ada0", "model": "APPLE SSD SM256E", "serial_no": "S0X5NZBC422720", "wwn": "5002538043584d30",