From 3924e6505a0da6172099241e8d3d9cd9191fce3a Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Tue, 22 May 2018 13:59:59 -0700 Subject: [PATCH] Add timeout option to sensors input (#4162) --- plugins/inputs/sensors/README.md | 3 +++ plugins/inputs/sensors/sensors.go | 24 +++++++++++++++--------- plugins/inputs/sensors/sensors_test.go | 2 ++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/plugins/inputs/sensors/README.md b/plugins/inputs/sensors/README.md index 62497d074399a..9075bda727846 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 1caf6ba5952c1..864da812123fe 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 7e6cac95af4b3..2a24fa6f9212f 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