diff --git a/plugins/inputs/ipmi_sensor/README.md b/plugins/inputs/ipmi_sensor/README.md index 0f9faa97f1f3d..f620b93cb659e 100644 --- a/plugins/inputs/ipmi_sensor/README.md +++ b/plugins/inputs/ipmi_sensor/README.md @@ -19,6 +19,11 @@ When one or more servers are specified, the plugin will use the following comman 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: +``` +-y hex_key -L privilege +``` + ### Configuration ```toml @@ -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..69ae04b78cf9f 100644 --- a/plugins/inputs/ipmi_sensor/connection.go +++ b/plugins/inputs/ipmi_sensor/connection.go @@ -15,11 +15,14 @@ type Connection struct { Port int Interface string Privilege string + HexKey string } -func NewConnection(server string, privilege string) *Connection { - conn := &Connection{} - conn.Privilege = privilege +func NewConnection(server, privilege, hexKey string) *Connection { + conn := &Connection{ + Privilege: privilege, + HexKey: hexKey, + } inx1 := strings.LastIndex(server, "@") inx2 := strings.Index(server, "(") @@ -57,6 +60,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{}