From b6e7ac967594f350a0473a455fc5f69537459e14 Mon Sep 17 00:00:00 2001 From: Stefan Kuntz Date: Sat, 11 Feb 2017 20:12:24 +0100 Subject: [PATCH 1/2] added user privilege level setting to IPMI sensors --- plugins/inputs/ipmi_sensor/README.md | 5 ++++- plugins/inputs/ipmi_sensor/connection.go | 8 ++++++-- plugins/inputs/ipmi_sensor/ipmi.go | 20 +++++++++++--------- plugins/inputs/ipmi_sensor/ipmi_test.go | 9 +++++---- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/plugins/inputs/ipmi_sensor/README.md b/plugins/inputs/ipmi_sensor/README.md index 3a75d0c655569..2e26e9d7262dd 100644 --- a/plugins/inputs/ipmi_sensor/README.md +++ b/plugins/inputs/ipmi_sensor/README.md @@ -34,7 +34,10 @@ The `server` tag will be made available when retrieving stats from remote server [[inputs.ipmi_sensor]] ## optionally specify the path to the ipmitool executable # path = "/usr/bin/ipmitool" - # + ## + ## optionally force session privilege level. Can be CALLBACK, USER, OPERATOR, ADMINISTRATOR + # privilege = "ADMINISTRATOR" + ## ## optionally specify one or more servers via a url matching ## [username[:password]@][protocol[(address)]] ## e.g. diff --git a/plugins/inputs/ipmi_sensor/connection.go b/plugins/inputs/ipmi_sensor/connection.go index 432b4aa02aad3..bf1fcb0519176 100644 --- a/plugins/inputs/ipmi_sensor/connection.go +++ b/plugins/inputs/ipmi_sensor/connection.go @@ -14,10 +14,12 @@ type Connection struct { Password string Port int Interface string + Privilege string } -func NewConnection(server string) *Connection { +func NewConnection(server string, privilege string) *Connection { conn := &Connection{} + conn.Privilege = privilege inx1 := strings.Index(server, "@") inx2 := strings.Index(server, "(") inx3 := strings.Index(server, ")") @@ -59,7 +61,9 @@ func (t *Connection) options() []string { if t.Port != 0 { options = append(options, "-p", strconv.Itoa(t.Port)) } - + if t.Privilege != "" { + options = append(options, "-L", t.Privilege) + } return options } diff --git a/plugins/inputs/ipmi_sensor/ipmi.go b/plugins/inputs/ipmi_sensor/ipmi.go index b2389a67538f4..5e0ef9c666dce 100644 --- a/plugins/inputs/ipmi_sensor/ipmi.go +++ b/plugins/inputs/ipmi_sensor/ipmi.go @@ -17,14 +17,18 @@ var ( ) type Ipmi struct { - path string - Servers []string + Path string + Privilege string + Servers []string } var sampleConfig = ` ## optionally specify the path to the ipmitool executable # path = "/usr/bin/ipmitool" - # + ## + ## optionally force session privilege level. Can be CALLBACK, USER, OPERATOR, ADMINISTRATOR + # privilege = "ADMINISTRATOR" + ## ## optionally specify one or more servers via a url matching ## [username[:password]@][protocol[(address)]] ## e.g. @@ -44,7 +48,7 @@ func (m *Ipmi) Description() string { } func (m *Ipmi) Gather(acc telegraf.Accumulator) error { - if len(m.path) == 0 { + if len(m.Path) == 0 { return fmt.Errorf("ipmitool not found: verify that ipmitool is installed and that ipmitool is in your PATH") } @@ -68,15 +72,13 @@ func (m *Ipmi) Gather(acc telegraf.Accumulator) error { func (m *Ipmi) parse(acc telegraf.Accumulator, server string) error { opts := make([]string, 0) hostname := "" - if server != "" { - conn := NewConnection(server) + conn := NewConnection(server, m.Privilege) hostname = conn.Hostname opts = conn.options() } - opts = append(opts, "sdr") - cmd := execCommand(m.path, opts...) + cmd := execCommand(m.Path, opts...) out, err := internal.CombinedOutputTimeout(cmd, time.Second*5) if err != nil { return fmt.Errorf("failed to run command %s: %s - %s", strings.Join(cmd.Args, " "), err, string(out)) @@ -149,7 +151,7 @@ func init() { m := Ipmi{} path, _ := exec.LookPath("ipmitool") if len(path) > 0 { - m.path = path + m.Path = path } inputs.Add("ipmi_sensor", func() telegraf.Input { return &m diff --git a/plugins/inputs/ipmi_sensor/ipmi_test.go b/plugins/inputs/ipmi_sensor/ipmi_test.go index 94dc066c851ad..6fdc73acf56c5 100644 --- a/plugins/inputs/ipmi_sensor/ipmi_test.go +++ b/plugins/inputs/ipmi_sensor/ipmi_test.go @@ -13,8 +13,9 @@ import ( func TestGather(t *testing.T) { i := &Ipmi{ - Servers: []string{"USERID:PASSW0RD@lan(192.168.1.1)"}, - path: "ipmitool", + Servers: []string{"USERID:PASSW0RD@lan(192.168.1.1)"}, + Path: "ipmitool", + Privilege: "USER", } // overwriting exec commands with mock commands execCommand = fakeExecCommand @@ -26,7 +27,7 @@ func TestGather(t *testing.T) { assert.Equal(t, acc.NFields(), 266, "non-numeric measurements should be ignored") - conn := NewConnection(i.Servers[0]) + conn := NewConnection(i.Servers[0], i.Privilege) assert.Equal(t, "USERID", conn.Username) assert.Equal(t, "lan", conn.Interface) @@ -118,7 +119,7 @@ func TestGather(t *testing.T) { } i = &Ipmi{ - path: "ipmitool", + Path: "ipmitool", } err = i.Gather(&acc) From 33623726ab12696866344dec475b0f6aedf3bf30 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Fri, 5 Jan 2018 15:21:29 -0800 Subject: [PATCH 2/2] Fix unittest --- plugins/inputs/ipmi_sensor/connection_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/ipmi_sensor/connection_test.go b/plugins/inputs/ipmi_sensor/connection_test.go index 13a62061daa6a..74944890f7a0c 100644 --- a/plugins/inputs/ipmi_sensor/connection_test.go +++ b/plugins/inputs/ipmi_sensor/connection_test.go @@ -23,6 +23,7 @@ func TestNewConnection(t *testing.T) { Username: "USERID", Password: "PASSW0RD", Interface: "lan", + Privilege: "USER", }, }, { @@ -32,11 +33,12 @@ func TestNewConnection(t *testing.T) { Username: "USERID", Password: "PASS:!@#$%^&*(234)_+W0RD", Interface: "lan", + Privilege: "USER", }, }, } for _, v := range testData { - assert.Equal(t, v.con, NewConnection(v.addr)) + assert.Equal(t, v.con, NewConnection(v.addr, "USER")) } }