2016-03-23 15:40:38 +00:00
|
|
|
package ipmi_sensor
|
2016-03-17 15:45:29 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-09 10:45:31 +00:00
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2016-03-17 15:45:29 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-07-03 02:06:57 +00:00
|
|
|
"sync"
|
2016-03-17 15:45:29 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2017-01-09 10:45:31 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-03-17 15:45:29 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
)
|
|
|
|
|
2017-01-09 10:45:31 +00:00
|
|
|
var (
|
|
|
|
execCommand = exec.Command // execCommand is used to mock commands in tests.
|
|
|
|
)
|
|
|
|
|
2016-03-17 15:45:29 +00:00
|
|
|
type Ipmi struct {
|
2018-01-05 23:59:25 +00:00
|
|
|
Path string
|
|
|
|
Privilege string
|
|
|
|
Servers []string
|
|
|
|
Timeout internal.Duration
|
2016-03-17 15:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2017-01-09 10:45:31 +00:00
|
|
|
## optionally specify the path to the ipmitool executable
|
|
|
|
# path = "/usr/bin/ipmitool"
|
2018-01-05 23:59:25 +00:00
|
|
|
##
|
|
|
|
## optionally force session privilege level. Can be CALLBACK, USER, OPERATOR, ADMINISTRATOR
|
|
|
|
# privilege = "ADMINISTRATOR"
|
|
|
|
##
|
2017-01-09 10:45:31 +00:00
|
|
|
## optionally specify one or more servers via a url matching
|
2016-03-17 15:45:29 +00:00
|
|
|
## [username[:password]@][protocol[(address)]]
|
|
|
|
## e.g.
|
|
|
|
## root:passwd@lan(127.0.0.1)
|
|
|
|
##
|
2017-01-09 10:45:31 +00:00
|
|
|
## if no servers are specified, local machine sensor stats will be queried
|
|
|
|
##
|
|
|
|
# servers = ["USERID:PASSW0RD@lan(192.168.1.1)"]
|
2017-05-22 20:41:34 +00:00
|
|
|
|
2017-11-01 00:00:06 +00:00
|
|
|
## Recommended: use metric 'interval' that is a multiple of 'timeout' to avoid
|
2017-05-22 20:41:34 +00:00
|
|
|
## gaps or overlap in pulled data
|
|
|
|
interval = "30s"
|
|
|
|
|
|
|
|
## Timeout for the ipmitool command to complete
|
|
|
|
timeout = "20s"
|
2016-03-17 15:45:29 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (m *Ipmi) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Ipmi) Description() string {
|
2017-01-09 10:45:31 +00:00
|
|
|
return "Read metrics from the bare metal servers via IPMI"
|
2016-03-17 15:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Ipmi) Gather(acc telegraf.Accumulator) error {
|
2017-03-08 16:38:36 +00:00
|
|
|
if len(m.Path) == 0 {
|
2017-01-09 10:45:31 +00:00
|
|
|
return fmt.Errorf("ipmitool not found: verify that ipmitool is installed and that ipmitool is in your PATH")
|
2016-03-17 15:45:29 +00:00
|
|
|
}
|
2017-01-09 10:45:31 +00:00
|
|
|
|
|
|
|
if len(m.Servers) > 0 {
|
2018-07-03 02:06:57 +00:00
|
|
|
wg := sync.WaitGroup{}
|
2017-01-09 10:45:31 +00:00
|
|
|
for _, server := range m.Servers {
|
2018-07-03 02:06:57 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(a telegraf.Accumulator, s string) {
|
|
|
|
defer wg.Done()
|
|
|
|
err := m.parse(a, s)
|
|
|
|
if err != nil {
|
|
|
|
a.AddError(err)
|
|
|
|
}
|
|
|
|
}(acc, server)
|
2017-01-09 10:45:31 +00:00
|
|
|
}
|
2018-07-03 02:06:57 +00:00
|
|
|
wg.Wait()
|
2017-01-09 10:45:31 +00:00
|
|
|
} else {
|
|
|
|
err := m.parse(acc, "")
|
2016-03-17 15:45:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-09 10:45:31 +00:00
|
|
|
func (m *Ipmi) parse(acc telegraf.Accumulator, server string) error {
|
|
|
|
opts := make([]string, 0)
|
|
|
|
hostname := ""
|
|
|
|
if server != "" {
|
2018-01-05 23:59:25 +00:00
|
|
|
conn := NewConnection(server, m.Privilege)
|
2017-01-09 10:45:31 +00:00
|
|
|
hostname = conn.Hostname
|
|
|
|
opts = conn.options()
|
|
|
|
}
|
|
|
|
opts = append(opts, "sdr")
|
2017-03-08 16:38:36 +00:00
|
|
|
cmd := execCommand(m.Path, opts...)
|
2017-05-22 20:41:34 +00:00
|
|
|
out, err := internal.CombinedOutputTimeout(cmd, m.Timeout.Duration)
|
2016-03-17 15:45:29 +00:00
|
|
|
if err != nil {
|
2017-01-09 10:45:31 +00:00
|
|
|
return fmt.Errorf("failed to run command %s: %s - %s", strings.Join(cmd.Args, " "), err, string(out))
|
2016-03-17 15:45:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 15:40:38 +00:00
|
|
|
// each line will look something like
|
|
|
|
// Planar VBAT | 3.05 Volts | ok
|
2017-01-09 10:45:31 +00:00
|
|
|
lines := strings.Split(string(out), "\n")
|
2016-03-17 15:45:29 +00:00
|
|
|
for i := 0; i < len(lines); i++ {
|
|
|
|
vals := strings.Split(lines[i], "|")
|
2016-03-23 15:40:38 +00:00
|
|
|
if len(vals) != 3 {
|
|
|
|
continue
|
|
|
|
}
|
2016-03-17 15:45:29 +00:00
|
|
|
|
2016-03-23 15:40:38 +00:00
|
|
|
tags := map[string]string{
|
2017-01-09 10:45:31 +00:00
|
|
|
"name": transform(vals[0]),
|
|
|
|
}
|
|
|
|
|
|
|
|
// tag the server is we have one
|
|
|
|
if hostname != "" {
|
|
|
|
tags["server"] = hostname
|
2016-03-23 15:40:38 +00:00
|
|
|
}
|
2016-03-17 15:45:29 +00:00
|
|
|
|
2016-03-23 15:40:38 +00:00
|
|
|
fields := make(map[string]interface{})
|
|
|
|
if strings.EqualFold("ok", trim(vals[2])) {
|
|
|
|
fields["status"] = 1
|
|
|
|
} else {
|
|
|
|
fields["status"] = 0
|
|
|
|
}
|
2016-03-17 15:45:29 +00:00
|
|
|
|
2016-03-23 15:40:38 +00:00
|
|
|
val1 := trim(vals[1])
|
|
|
|
|
|
|
|
if strings.Index(val1, " ") > 0 {
|
|
|
|
// split middle column into value and unit
|
|
|
|
valunit := strings.SplitN(val1, " ", 2)
|
|
|
|
fields["value"] = Atofloat(valunit[0])
|
|
|
|
if len(valunit) > 1 {
|
|
|
|
tags["unit"] = transform(valunit[1])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fields["value"] = 0.0
|
2016-03-17 15:45:29 +00:00
|
|
|
}
|
2016-03-23 15:40:38 +00:00
|
|
|
|
|
|
|
acc.AddFields("ipmi_sensor", fields, tags, time.Now())
|
2016-03-17 15:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Atofloat(val string) float64 {
|
|
|
|
f, err := strconv.ParseFloat(val, 64)
|
|
|
|
if err != nil {
|
2016-03-23 15:40:38 +00:00
|
|
|
return 0.0
|
2016-03-17 15:45:29 +00:00
|
|
|
} else {
|
2016-03-23 15:40:38 +00:00
|
|
|
return f
|
2016-03-17 15:45:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func trim(s string) string {
|
|
|
|
return strings.TrimSpace(s)
|
|
|
|
}
|
|
|
|
|
2016-03-23 15:40:38 +00:00
|
|
|
func transform(s string) string {
|
|
|
|
s = trim(s)
|
|
|
|
s = strings.ToLower(s)
|
|
|
|
return strings.Replace(s, " ", "_", -1)
|
|
|
|
}
|
|
|
|
|
2016-03-17 15:45:29 +00:00
|
|
|
func init() {
|
2017-01-09 10:45:31 +00:00
|
|
|
m := Ipmi{}
|
|
|
|
path, _ := exec.LookPath("ipmitool")
|
|
|
|
if len(path) > 0 {
|
2017-03-08 16:38:36 +00:00
|
|
|
m.Path = path
|
2017-01-09 10:45:31 +00:00
|
|
|
}
|
2017-05-22 20:41:34 +00:00
|
|
|
m.Timeout = internal.Duration{Duration: time.Second * 20}
|
2016-03-23 15:40:38 +00:00
|
|
|
inputs.Add("ipmi_sensor", func() telegraf.Input {
|
2017-04-20 00:02:44 +00:00
|
|
|
m := m
|
2017-01-09 10:45:31 +00:00
|
|
|
return &m
|
2016-03-17 15:45:29 +00:00
|
|
|
})
|
|
|
|
}
|