Skip to content

Commit

Permalink
Add timeout option to sensors input (#4162)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored May 22, 2018
1 parent 795c805 commit daacfc6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 3 additions & 0 deletions plugins/inputs/sensors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ This plugin collects sensor metrics with the `sensors` executable from the lm-se
## Remove numbers from field names.
## If true, a field name like 'temp1_input' will be changed to 'temp_input'.
# remove_numbers = true
## Timeout is the maximum amount of time that the sensors command can run.
# timeout = "5s"
```

### Measurements & Fields:
Expand Down
24 changes: 15 additions & 9 deletions plugins/inputs/sensors/sensors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import (
)

var (
execCommand = exec.Command // execCommand is used to mock commands in tests.
numberRegp = regexp.MustCompile("[0-9]+")
execCommand = exec.Command // execCommand is used to mock commands in tests.
numberRegp = regexp.MustCompile("[0-9]+")
defaultTimeout = internal.Duration{Duration: 5 * time.Second}
)

type Sensors struct {
RemoveNumbers bool `toml:"remove_numbers"`
RemoveNumbers bool `toml:"remove_numbers"`
Timeout internal.Duration `toml:"timeout"`
path string
}

Expand All @@ -35,6 +37,9 @@ func (*Sensors) SampleConfig() string {
## Remove numbers from field names.
## If true, a field name like 'temp1_input' will be changed to 'temp_input'.
# remove_numbers = true
## Timeout is the maximum amount of time that the sensors command can run.
# timeout = "5s"
`

}
Expand All @@ -55,7 +60,7 @@ func (s *Sensors) parse(acc telegraf.Accumulator) error {
fields := map[string]interface{}{}
chip := ""
cmd := execCommand(s.path, "-A", "-u")
out, err := internal.CombinedOutputTimeout(cmd, time.Second*5)
out, err := internal.CombinedOutputTimeout(cmd, s.Timeout.Duration)
if err != nil {
return fmt.Errorf("failed to run command %s: %s - %s", strings.Join(cmd.Args, " "), err, string(out))
}
Expand Down Expand Up @@ -99,9 +104,15 @@ func (s *Sensors) parse(acc telegraf.Accumulator) error {
return nil
}

// snake converts string to snake case
func snake(input string) string {
return strings.ToLower(strings.Replace(strings.TrimSpace(input), " ", "_", -1))
}

func init() {
s := Sensors{
RemoveNumbers: true,
Timeout: defaultTimeout,
}
path, _ := exec.LookPath("sensors")
if len(path) > 0 {
Expand All @@ -111,8 +122,3 @@ func init() {
return &s
})
}

// snake converts string to snake case
func snake(input string) string {
return strings.ToLower(strings.Replace(strings.TrimSpace(input), " ", "_", -1))
}
2 changes: 2 additions & 0 deletions plugins/inputs/sensors/sensors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func TestGatherDefault(t *testing.T) {
s := Sensors{
RemoveNumbers: true,
Timeout: defaultTimeout,
path: "sensors",
}
// overwriting exec commands with mock commands
Expand Down Expand Up @@ -154,6 +155,7 @@ func TestGatherDefault(t *testing.T) {
func TestGatherNotRemoveNumbers(t *testing.T) {
s := Sensors{
RemoveNumbers: false,
Timeout: defaultTimeout,
path: "sensors",
}
// overwriting exec commands with mock commands
Expand Down

0 comments on commit daacfc6

Please sign in to comment.