From 8d363c2792036042f17125f3ca6b87ba3a8ad055 Mon Sep 17 00:00:00 2001 From: David Bennett Date: Mon, 7 Dec 2020 16:31:44 -0500 Subject: [PATCH 1/5] Add 'hex-key' config parameter for IPMI input plugin connection. --- plugins/inputs/ipmi_sensor/README.md | 10 ++++- plugins/inputs/ipmi_sensor/connection.go | 7 +++- plugins/inputs/ipmi_sensor/connection_test.go | 40 ++++++++++++++++++- plugins/inputs/ipmi_sensor/ipmi.go | 6 ++- plugins/inputs/ipmi_sensor/ipmi_test.go | 20 ++++++---- 5 files changed, 70 insertions(+), 13 deletions(-) diff --git a/plugins/inputs/ipmi_sensor/README.md b/plugins/inputs/ipmi_sensor/README.md index 0f9faa97f1f3d..8471ebc25d7f2 100644 --- a/plugins/inputs/ipmi_sensor/README.md +++ b/plugins/inputs/ipmi_sensor/README.md @@ -15,8 +15,13 @@ ipmitool sdr elist When one or more servers are specified, the plugin will use the following command to collect remote host sensor stats: +```v +ipmitool -I lan -H SERVER -U USERID -P PASSW0RD - sdr ``` -ipmitool -I lan -H SERVER -U USERID -P PASSW0RD sdr + +Any of the following parameters will be added to the aformentioned query if they're configured: +```v +-y hex_key -L privilege ``` ### Configuration @@ -53,6 +58,9 @@ ipmitool -I lan -H SERVER -U USERID -P PASSW0RD sdr ## Schema Version: (Optional, defaults to version 1) metric_version = 2 + + ## Optionally provide the hex key for the IMPI connection. + # hex_key = "" ``` ### Measurements diff --git a/plugins/inputs/ipmi_sensor/connection.go b/plugins/inputs/ipmi_sensor/connection.go index 7f6a4c3594f61..6a6f6618ad02e 100644 --- a/plugins/inputs/ipmi_sensor/connection.go +++ b/plugins/inputs/ipmi_sensor/connection.go @@ -15,11 +15,13 @@ type Connection struct { Port int Interface string Privilege string + HexKey string } -func NewConnection(server string, privilege string) *Connection { +func NewConnection(server, privilege, hexKey string) *Connection { conn := &Connection{} conn.Privilege = privilege + conn.HexKey = hexKey inx1 := strings.LastIndex(server, "@") inx2 := strings.Index(server, "(") @@ -57,6 +59,9 @@ func (t *Connection) options() []string { "-I", intf, } + if t.HexKey != "" { + options = append(options, "-y", t.HexKey) + } if t.Port != 0 { options = append(options, "-p", strconv.Itoa(t.Port)) } diff --git a/plugins/inputs/ipmi_sensor/connection_test.go b/plugins/inputs/ipmi_sensor/connection_test.go index 74944890f7a0c..21d1957c95126 100644 --- a/plugins/inputs/ipmi_sensor/connection_test.go +++ b/plugins/inputs/ipmi_sensor/connection_test.go @@ -3,7 +3,7 @@ package ipmi_sensor import ( "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type conTest struct { @@ -24,6 +24,7 @@ func TestNewConnection(t *testing.T) { Password: "PASSW0RD", Interface: "lan", Privilege: "USER", + HexKey: "0001", }, }, { @@ -34,11 +35,46 @@ func TestNewConnection(t *testing.T) { Password: "PASS:!@#$%^&*(234)_+W0RD", Interface: "lan", Privilege: "USER", + HexKey: "0001", }, }, } for _, v := range testData { - assert.Equal(t, v.con, NewConnection(v.addr, "USER")) + require.EqualValues(t, v.con, NewConnection(v.addr, "USER", "0001")) + } +} + +func TestGetCommandOptions(t *testing.T) { + testData := []struct { + connection *Connection + options []string + }{ + { + &Connection{ + Hostname: "192.168.1.1", + Username: "user", + Password: "password", + Interface: "lan", + Privilege: "USER", + HexKey: "0001", + }, + []string{"-H", "192.168.1.1", "-U", "user", "-P", "password", "-I", "lan", "-y", "0001", "-L", "USER"}, + }, + { + &Connection{ + Hostname: "192.168.1.1", + Username: "user", + Password: "password", + Interface: "lan", + Privilege: "USER", + HexKey: "", + }, + []string{"-H", "192.168.1.1", "-U", "user", "-P", "password", "-I", "lan", "-L", "USER"}, + }, + } + + for _, data := range testData { + require.EqualValues(t, data.options, data.connection.options()) } } diff --git a/plugins/inputs/ipmi_sensor/ipmi.go b/plugins/inputs/ipmi_sensor/ipmi.go index fb53e1bc746fe..5572a195b2c29 100644 --- a/plugins/inputs/ipmi_sensor/ipmi.go +++ b/plugins/inputs/ipmi_sensor/ipmi.go @@ -29,6 +29,7 @@ var ( type Ipmi struct { Path string Privilege string + HexKey string `toml:"hex_key"` Servers []string Timeout internal.Duration MetricVersion int @@ -65,6 +66,9 @@ var sampleConfig = ` ## Schema Version: (Optional, defaults to version 1) metric_version = 2 + + ## Optionally provide the hex key for the IMPI connection. + # hex_key = "" ` // SampleConfig returns the documentation about the sample configuration @@ -110,7 +114,7 @@ func (m *Ipmi) parse(acc telegraf.Accumulator, server string) error { opts := make([]string, 0) hostname := "" if server != "" { - conn := NewConnection(server, m.Privilege) + conn := NewConnection(server, m.Privilege, m.HexKey) hostname = conn.Hostname opts = conn.options() } diff --git a/plugins/inputs/ipmi_sensor/ipmi_test.go b/plugins/inputs/ipmi_sensor/ipmi_test.go index bd5e02c196e76..81139ef40ee94 100644 --- a/plugins/inputs/ipmi_sensor/ipmi_test.go +++ b/plugins/inputs/ipmi_sensor/ipmi_test.go @@ -10,7 +10,6 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -20,7 +19,9 @@ func TestGather(t *testing.T) { Path: "ipmitool", Privilege: "USER", Timeout: internal.Duration{Duration: time.Second * 5}, + HexKey: "1234567F", } + // overwriting exec commands with mock commands execCommand = fakeExecCommand var acc testutil.Accumulator @@ -29,11 +30,12 @@ func TestGather(t *testing.T) { require.NoError(t, err) - assert.Equal(t, acc.NFields(), 262, "non-numeric measurements should be ignored") + require.EqualValues(t, acc.NFields(), 262, "non-numeric measurements should be ignored") - conn := NewConnection(i.Servers[0], i.Privilege) - assert.Equal(t, "USERID", conn.Username) - assert.Equal(t, "lan", conn.Interface) + conn := NewConnection(i.Servers[0], i.Privilege, i.HexKey) + require.EqualValues(t, "USERID", conn.Username) + require.EqualValues(t, "lan", conn.Interface) + require.EqualValues(t, "1234567F", conn.HexKey) var testsWithServer = []struct { fields map[string]interface{} @@ -388,6 +390,7 @@ func TestGatherV2(t *testing.T) { Privilege: "USER", Timeout: internal.Duration{Duration: time.Second * 5}, MetricVersion: 2, + HexKey: "0000000F", } // overwriting exec commands with mock commands execCommand = fakeExecCommandV2 @@ -397,9 +400,10 @@ func TestGatherV2(t *testing.T) { require.NoError(t, err) - conn := NewConnection(i.Servers[0], i.Privilege) - assert.Equal(t, "USERID", conn.Username) - assert.Equal(t, "lan", conn.Interface) + conn := NewConnection(i.Servers[0], i.Privilege, i.HexKey) + require.EqualValues(t, "USERID", conn.Username) + require.EqualValues(t, "lan", conn.Interface) + require.EqualValues(t, "0000000F", conn.HexKey) var testsWithServer = []struct { fields map[string]interface{} From a873833a57d0c9f203def56a74b2130b52e26ba9 Mon Sep 17 00:00:00 2001 From: David Bennett <71459415+Jagularr@users.noreply.github.com> Date: Mon, 7 Dec 2020 16:34:48 -0500 Subject: [PATCH 2/5] Update README.md fixing readme typo --- plugins/inputs/ipmi_sensor/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inputs/ipmi_sensor/README.md b/plugins/inputs/ipmi_sensor/README.md index 8471ebc25d7f2..5f8b58c512f30 100644 --- a/plugins/inputs/ipmi_sensor/README.md +++ b/plugins/inputs/ipmi_sensor/README.md @@ -16,7 +16,7 @@ ipmitool sdr elist When one or more servers are specified, the plugin will use the following command to collect remote host sensor stats: ```v -ipmitool -I lan -H SERVER -U USERID -P PASSW0RD - sdr +ipmitool -I lan -H SERVER -U USERID -P PASSW0RD sdr ``` Any of the following parameters will be added to the aformentioned query if they're configured: From 6050a7695a33f6e78648ec1545588e6a7697ab00 Mon Sep 17 00:00:00 2001 From: David Bennett <71459415+Jagularr@users.noreply.github.com> Date: Mon, 7 Dec 2020 16:36:03 -0500 Subject: [PATCH 3/5] Update README.md fixing typo (again) --- plugins/inputs/ipmi_sensor/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/ipmi_sensor/README.md b/plugins/inputs/ipmi_sensor/README.md index 5f8b58c512f30..f620b93cb659e 100644 --- a/plugins/inputs/ipmi_sensor/README.md +++ b/plugins/inputs/ipmi_sensor/README.md @@ -15,12 +15,12 @@ ipmitool sdr elist When one or more servers are specified, the plugin will use the following command to collect remote host sensor stats: -```v +``` ipmitool -I lan -H SERVER -U USERID -P PASSW0RD sdr ``` Any of the following parameters will be added to the aformentioned query if they're configured: -```v +``` -y hex_key -L privilege ``` From 02e5b7a6729f8bf4e6d8178a173569c82d736849 Mon Sep 17 00:00:00 2001 From: David Bennett <71459415+Jagularr@users.noreply.github.com> Date: Tue, 15 Dec 2020 10:50:22 -0500 Subject: [PATCH 4/5] Update plugins/inputs/ipmi_sensor/connection.go Co-authored-by: Sven Rebhan <36194019+srebhan@users.noreply.github.com> --- plugins/inputs/ipmi_sensor/connection.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/inputs/ipmi_sensor/connection.go b/plugins/inputs/ipmi_sensor/connection.go index 6a6f6618ad02e..1b6965d673e65 100644 --- a/plugins/inputs/ipmi_sensor/connection.go +++ b/plugins/inputs/ipmi_sensor/connection.go @@ -19,9 +19,10 @@ type Connection struct { } func NewConnection(server, privilege, hexKey string) *Connection { - conn := &Connection{} - conn.Privilege = privilege - conn.HexKey = hexKey + conn := &Connection{ + Privilege: privilege, + HexKey: hexKey, + } inx1 := strings.LastIndex(server, "@") inx2 := strings.Index(server, "(") From 54138336902cbd006cc45b6998d4422cbd3ecab2 Mon Sep 17 00:00:00 2001 From: David Bennett <71459415+Jagularr@users.noreply.github.com> Date: Tue, 15 Dec 2020 15:46:06 -0500 Subject: [PATCH 5/5] Update connection.go update formatting --- plugins/inputs/ipmi_sensor/connection.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inputs/ipmi_sensor/connection.go b/plugins/inputs/ipmi_sensor/connection.go index 1b6965d673e65..69ae04b78cf9f 100644 --- a/plugins/inputs/ipmi_sensor/connection.go +++ b/plugins/inputs/ipmi_sensor/connection.go @@ -21,7 +21,7 @@ type Connection struct { func NewConnection(server, privilege, hexKey string) *Connection { conn := &Connection{ Privilege: privilege, - HexKey: hexKey, + HexKey: hexKey, } inx1 := strings.LastIndex(server, "@") inx2 := strings.Index(server, "(")