Add timeout option to sensors input (#4162)

This commit is contained in:
Daniel Nelson 2018-05-22 13:59:59 -07:00 committed by GitHub
parent e32e39cf92
commit 3924e6505a
3 changed files with 20 additions and 9 deletions

View File

@ -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:

View File

@ -19,10 +19,12 @@ import (
var (
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"`
Timeout internal.Duration `toml:"timeout"`
path string
}
@ -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"
`
}
@ -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))
}
@ -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 {
@ -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))
}

View File

@ -14,6 +14,7 @@ import (
func TestGatherDefault(t *testing.T) {
s := Sensors{
RemoveNumbers: true,
Timeout: defaultTimeout,
path: "sensors",
}
// overwriting exec commands with mock commands
@ -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