Skip to content

Commit

Permalink
Add hex_key parameter for IPMI input plugin connection (#8524)
Browse files Browse the repository at this point in the history
(cherry picked from commit dd09f46)
  • Loading branch information
ivorybilled authored and ssoroka committed Jan 27, 2021
1 parent d012dca commit 813017c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
8 changes: 8 additions & 0 deletions plugins/inputs/ipmi_sensor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions plugins/inputs/ipmi_sensor/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "(")

Expand Down Expand Up @@ -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))
}
Expand Down
40 changes: 38 additions & 2 deletions plugins/inputs/ipmi_sensor/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ipmi_sensor
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type conTest struct {
Expand All @@ -24,6 +24,7 @@ func TestNewConnection(t *testing.T) {
Password: "PASSW0RD",
Interface: "lan",
Privilege: "USER",
HexKey: "0001",
},
},
{
Expand All @@ -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())
}
}
6 changes: 5 additions & 1 deletion plugins/inputs/ipmi_sensor/ipmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
type Ipmi struct {
Path string
Privilege string
HexKey string `toml:"hex_key"`
Servers []string
Timeout internal.Duration
MetricVersion int
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
20 changes: 12 additions & 8 deletions plugins/inputs/ipmi_sensor/ipmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand All @@ -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{}
Expand Down Expand Up @@ -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
Expand All @@ -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{}
Expand Down

0 comments on commit 813017c

Please sign in to comment.