diff --git a/plugins/inputs/sensors/README.md b/plugins/inputs/sensors/README.md index 62497d074..9075bda72 100644 --- a/plugins/inputs/sensors/README.md +++ b/plugins/inputs/sensors/README.md @@ -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: diff --git a/plugins/inputs/sensors/sensors.go b/plugins/inputs/sensors/sensors.go index 1caf6ba59..864da8121 100644 --- a/plugins/inputs/sensors/sensors.go +++ b/plugins/inputs/sensors/sensors.go @@ -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 } @@ -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)) -} diff --git a/plugins/inputs/sensors/sensors_test.go b/plugins/inputs/sensors/sensors_test.go index 7e6cac95a..2a24fa6f9 100644 --- a/plugins/inputs/sensors/sensors_test.go +++ b/plugins/inputs/sensors/sensors_test.go @@ -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