From 1ea18ffd0a5f4311e1c4516cf465d4b65862dd84 Mon Sep 17 00:00:00 2001 From: marcv81 Date: Tue, 12 Jun 2018 07:06:26 +0800 Subject: [PATCH] Add power draw field to nvidia_smi plugin (#4262) --- plugins/inputs/nvidia_smi/README.md | 1 + plugins/inputs/nvidia_smi/nvidia_smi.go | 40 +++++++++++++------- plugins/inputs/nvidia_smi/nvidia_smi_test.go | 4 +- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/plugins/inputs/nvidia_smi/README.md b/plugins/inputs/nvidia_smi/README.md index 84b8527f2..3e8b84ba0 100644 --- a/plugins/inputs/nvidia_smi/README.md +++ b/plugins/inputs/nvidia_smi/README.md @@ -27,6 +27,7 @@ This plugin uses a query on the [`nvidia-smi`](https://developer.nvidia.com/nvid - `memory_free` (integer, KB) - `memory_used` (integer, KB) - `memory_total` (integer, KB) + - `power_draw` (float, W) - `temperature_gpu` (integer, degrees C) - `utilization_gpu` (integer, percentage) - `utilization_memory` (integer, percentage) diff --git a/plugins/inputs/nvidia_smi/nvidia_smi.go b/plugins/inputs/nvidia_smi/nvidia_smi.go index efac941b9..84784b765 100644 --- a/plugins/inputs/nvidia_smi/nvidia_smi.go +++ b/plugins/inputs/nvidia_smi/nvidia_smi.go @@ -16,20 +16,21 @@ import ( var ( measurement = "nvidia_smi" - metrics = "fan.speed,memory.total,memory.used,memory.free,pstate,temperature.gpu,name,uuid,compute_mode,utilization.gpu,utilization.memory,index" + metrics = "fan.speed,memory.total,memory.used,memory.free,pstate,temperature.gpu,name,uuid,compute_mode,utilization.gpu,utilization.memory,index,power.draw" metricNames = [][]string{ - []string{"fan_speed", "field"}, - []string{"memory_total", "field"}, - []string{"memory_used", "field"}, - []string{"memory_free", "field"}, + []string{"fan_speed", "integer"}, + []string{"memory_total", "integer"}, + []string{"memory_used", "integer"}, + []string{"memory_free", "integer"}, []string{"pstate", "tag"}, - []string{"temperature_gpu", "field"}, + []string{"temperature_gpu", "integer"}, []string{"name", "tag"}, []string{"uuid", "tag"}, []string{"compute_mode", "tag"}, - []string{"utilization_gpu", "field"}, - []string{"utilization_memory", "field"}, + []string{"utilization_gpu", "integer"}, + []string{"utilization_memory", "integer"}, []string{"index", "tag"}, + []string{"power_draw", "float"}, } ) @@ -127,7 +128,7 @@ func parseLine(line string) (map[string]string, map[string]interface{}, error) { for i, m := range metricNames { col := strings.TrimSpace(met[i]) - // First handle the tags + // Handle the tags if m[1] == "tag" { tags[m[0]] = col continue @@ -137,12 +138,23 @@ func parseLine(line string) (map[string]string, map[string]interface{}, error) { continue } - // Then parse the integers out of the fields - out, err := strconv.ParseInt(col, 10, 64) - if err != nil { - return tags, fields, err + // Parse the integers + if m[1] == "integer" { + out, err := strconv.ParseInt(col, 10, 64) + if err != nil { + return tags, fields, err + } + fields[m[0]] = out + } + + // Parse the floats + if m[1] == "float" { + out, err := strconv.ParseFloat(col, 64) + if err != nil { + return tags, fields, err + } + fields[m[0]] = out } - fields[m[0]] = out } // Return the tags and fields diff --git a/plugins/inputs/nvidia_smi/nvidia_smi_test.go b/plugins/inputs/nvidia_smi/nvidia_smi_test.go index 573a69b19..87785fe87 100644 --- a/plugins/inputs/nvidia_smi/nvidia_smi_test.go +++ b/plugins/inputs/nvidia_smi/nvidia_smi_test.go @@ -7,7 +7,7 @@ import ( ) func TestParseLineStandard(t *testing.T) { - line := "85, 8114, 553, 7561, P2, 61, GeForce GTX 1070 Ti, GPU-d1911b8a-f5c8-5e66-057c-486561269de8, Default, 100, 93, 1\n" + line := "85, 8114, 553, 7561, P2, 61, GeForce GTX 1070 Ti, GPU-d1911b8a-f5c8-5e66-057c-486561269de8, Default, 100, 93, 1, 0.0\n" tags, fields, err := parseLine(line) if err != nil { t.Fail() @@ -37,7 +37,7 @@ func TestParseLineBad(t *testing.T) { } func TestParseLineNotSupported(t *testing.T) { - line := "[Not Supported], 7606, 0, 7606, P0, 38, Tesla P4, GPU-xxx, Default, 0, 0, 0\n" + line := "[Not Supported], 7606, 0, 7606, P0, 38, Tesla P4, GPU-xxx, Default, 0, 0, 0, 0.0\n" _, fields, err := parseLine(line) require.NoError(t, err) require.Equal(t, nil, fields["fan_speed"])