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 795c8057ad
commit daacfc6368
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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. ## Remove numbers from field names.
## If true, a field name like 'temp1_input' will be changed to 'temp_input'. ## If true, a field name like 'temp1_input' will be changed to 'temp_input'.
# remove_numbers = true # remove_numbers = true
## Timeout is the maximum amount of time that the sensors command can run.
# timeout = "5s"
``` ```
### Measurements & Fields: ### Measurements & Fields:

View File

@ -17,12 +17,14 @@ import (
) )
var ( var (
execCommand = exec.Command // execCommand is used to mock commands in tests. execCommand = exec.Command // execCommand is used to mock commands in tests.
numberRegp = regexp.MustCompile("[0-9]+") numberRegp = regexp.MustCompile("[0-9]+")
defaultTimeout = internal.Duration{Duration: 5 * time.Second}
) )
type Sensors struct { type Sensors struct {
RemoveNumbers bool `toml:"remove_numbers"` RemoveNumbers bool `toml:"remove_numbers"`
Timeout internal.Duration `toml:"timeout"`
path string path string
} }
@ -35,6 +37,9 @@ func (*Sensors) SampleConfig() string {
## Remove numbers from field names. ## Remove numbers from field names.
## If true, a field name like 'temp1_input' will be changed to 'temp_input'. ## If true, a field name like 'temp1_input' will be changed to 'temp_input'.
# remove_numbers = true # 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{}{} fields := map[string]interface{}{}
chip := "" chip := ""
cmd := execCommand(s.path, "-A", "-u") 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 { if err != nil {
return fmt.Errorf("failed to run command %s: %s - %s", strings.Join(cmd.Args, " "), err, string(out)) 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 return nil
} }
// snake converts string to snake case
func snake(input string) string {
return strings.ToLower(strings.Replace(strings.TrimSpace(input), " ", "_", -1))
}
func init() { func init() {
s := Sensors{ s := Sensors{
RemoveNumbers: true, RemoveNumbers: true,
Timeout: defaultTimeout,
} }
path, _ := exec.LookPath("sensors") path, _ := exec.LookPath("sensors")
if len(path) > 0 { if len(path) > 0 {
@ -111,8 +122,3 @@ func init() {
return &s 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) { func TestGatherDefault(t *testing.T) {
s := Sensors{ s := Sensors{
RemoveNumbers: true, RemoveNumbers: true,
Timeout: defaultTimeout,
path: "sensors", path: "sensors",
} }
// overwriting exec commands with mock commands // overwriting exec commands with mock commands
@ -154,6 +155,7 @@ func TestGatherDefault(t *testing.T) {
func TestGatherNotRemoveNumbers(t *testing.T) { func TestGatherNotRemoveNumbers(t *testing.T) {
s := Sensors{ s := Sensors{
RemoveNumbers: false, RemoveNumbers: false,
Timeout: defaultTimeout,
path: "sensors", path: "sensors",
} }
// overwriting exec commands with mock commands // overwriting exec commands with mock commands