2016-06-24 08:18:02 +00:00
|
|
|
// +build linux
|
2016-01-15 12:21:49 +00:00
|
|
|
|
2016-01-14 23:03:39 +00:00
|
|
|
package sensors
|
|
|
|
|
|
|
|
import (
|
2016-06-24 08:18:02 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2016-01-14 23:03:39 +00:00
|
|
|
"strings"
|
2016-06-24 08:18:02 +00:00
|
|
|
"time"
|
2016-01-14 23:03:39 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-06-24 08:18:02 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2016-01-14 23:03:39 +00:00
|
|
|
)
|
|
|
|
|
2016-06-24 08:18:02 +00:00
|
|
|
var (
|
|
|
|
execCommand = exec.Command // execCommand is used to mock commands in tests.
|
|
|
|
numberRegp = regexp.MustCompile("[0-9]+")
|
|
|
|
)
|
|
|
|
|
2016-01-14 23:03:39 +00:00
|
|
|
type Sensors struct {
|
2016-06-24 08:18:02 +00:00
|
|
|
RemoveNumbers bool `toml:"remove_numbers"`
|
|
|
|
path string
|
2016-01-14 23:03:39 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:18:02 +00:00
|
|
|
func (*Sensors) Description() string {
|
|
|
|
return "Monitor sensors, requires lm-sensors package"
|
2016-01-14 23:03:39 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:18:02 +00:00
|
|
|
func (*Sensors) SampleConfig() string {
|
|
|
|
return `
|
|
|
|
## Remove numbers from field names.
|
|
|
|
## If true, a field name like 'temp1_input' will be changed to 'temp_input'.
|
|
|
|
# remove_numbers = true
|
2016-01-14 23:03:39 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (s *Sensors) Gather(acc telegraf.Accumulator) error {
|
2016-06-24 08:18:02 +00:00
|
|
|
if len(s.path) == 0 {
|
|
|
|
return errors.New("sensors not found: verify that lm-sensors package is installed and that sensors is in your PATH")
|
|
|
|
}
|
2016-01-14 23:03:39 +00:00
|
|
|
|
2016-06-24 08:18:02 +00:00
|
|
|
return s.parse(acc)
|
|
|
|
}
|
2016-01-14 23:03:39 +00:00
|
|
|
|
2016-06-24 08:18:02 +00:00
|
|
|
// parse forks the command:
|
|
|
|
// sensors -u -A
|
|
|
|
// and parses the output to add it to the telegraf.Accumulator.
|
|
|
|
func (s *Sensors) parse(acc telegraf.Accumulator) error {
|
|
|
|
tags := map[string]string{}
|
|
|
|
fields := map[string]interface{}{}
|
|
|
|
chip := ""
|
|
|
|
cmd := execCommand(s.path, "-A", "-u")
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
if len(line) == 0 {
|
|
|
|
acc.AddFields("sensors", fields, tags)
|
|
|
|
chip = ""
|
|
|
|
tags = map[string]string{}
|
|
|
|
fields = map[string]interface{}{}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(chip) == 0 {
|
|
|
|
chip = line
|
|
|
|
tags["chip"] = chip
|
|
|
|
continue
|
|
|
|
}
|
2016-09-12 09:30:35 +00:00
|
|
|
if !strings.HasPrefix(line, " ") {
|
2016-06-24 08:18:02 +00:00
|
|
|
if len(tags) > 1 {
|
|
|
|
acc.AddFields("sensors", fields, tags)
|
2016-01-14 23:03:39 +00:00
|
|
|
}
|
2016-06-24 08:18:02 +00:00
|
|
|
fields = map[string]interface{}{}
|
|
|
|
tags = map[string]string{
|
|
|
|
"chip": chip,
|
|
|
|
"feature": strings.TrimRight(snake(line), ":"),
|
2016-01-14 23:03:39 +00:00
|
|
|
}
|
2016-06-24 08:18:02 +00:00
|
|
|
} else {
|
|
|
|
splitted := strings.Split(line, ":")
|
|
|
|
fieldName := strings.TrimSpace(splitted[0])
|
|
|
|
if s.RemoveNumbers {
|
|
|
|
fieldName = numberRegp.ReplaceAllString(fieldName, "")
|
2016-01-14 23:03:39 +00:00
|
|
|
}
|
2016-06-24 08:18:02 +00:00
|
|
|
fieldValue, err := strconv.ParseFloat(strings.TrimSpace(splitted[1]), 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fields[fieldName] = fieldValue
|
2016-01-14 23:03:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:18:02 +00:00
|
|
|
acc.AddFields("sensors", fields, tags)
|
2016-01-14 23:03:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-06-24 08:18:02 +00:00
|
|
|
s := Sensors{
|
|
|
|
RemoveNumbers: true,
|
|
|
|
}
|
|
|
|
path, _ := exec.LookPath("sensors")
|
|
|
|
if len(path) > 0 {
|
|
|
|
s.path = path
|
|
|
|
}
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("sensors", func() telegraf.Input {
|
2016-06-24 08:18:02 +00:00
|
|
|
return &s
|
2016-01-14 23:03:39 +00:00
|
|
|
})
|
|
|
|
}
|
2016-06-24 08:18:02 +00:00
|
|
|
|
|
|
|
// snake converts string to snake case
|
|
|
|
func snake(input string) string {
|
2016-09-12 09:30:35 +00:00
|
|
|
return strings.ToLower(strings.Replace(strings.TrimSpace(input), " ", "_", -1))
|
2016-06-24 08:18:02 +00:00
|
|
|
}
|