Add configurable timeout setting to smart input (#6241)
This commit is contained in:
committed by
Daniel Nelson
parent
23b86552fd
commit
5473872ac1
@@ -119,6 +119,7 @@ type Smart struct {
|
||||
Excludes []string
|
||||
Devices []string
|
||||
UseSudo bool
|
||||
Timeout internal.Duration
|
||||
}
|
||||
|
||||
var sampleConfig = `
|
||||
@@ -151,8 +152,17 @@ var sampleConfig = `
|
||||
## done and all found will be included except for the
|
||||
## excluded in excludes.
|
||||
# devices = [ "/dev/ada0 -d atacam" ]
|
||||
|
||||
## Timeout for the smartctl command to complete.
|
||||
# timeout = "30s"
|
||||
`
|
||||
|
||||
func NewSmart() *Smart {
|
||||
return &Smart{
|
||||
Timeout: internal.Duration{Duration: time.Second * 30},
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Smart) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
@@ -180,17 +190,17 @@ func (m *Smart) Gather(acc telegraf.Accumulator) error {
|
||||
}
|
||||
|
||||
// Wrap with sudo
|
||||
var runCmd = func(sudo bool, command string, args ...string) ([]byte, error) {
|
||||
var runCmd = func(timeout internal.Duration, sudo bool, command string, args ...string) ([]byte, error) {
|
||||
cmd := exec.Command(command, args...)
|
||||
if sudo {
|
||||
cmd = exec.Command("sudo", append([]string{"-n", command}, args...)...)
|
||||
}
|
||||
return internal.CombinedOutputTimeout(cmd, time.Second*5)
|
||||
return internal.CombinedOutputTimeout(cmd, timeout.Duration)
|
||||
}
|
||||
|
||||
// Scan for S.M.A.R.T. devices
|
||||
func (m *Smart) scan() ([]string, error) {
|
||||
out, err := runCmd(m.UseSudo, m.Path, "--scan")
|
||||
out, err := runCmd(m.Timeout, m.UseSudo, m.Path, "--scan")
|
||||
if err != nil {
|
||||
return []string{}, fmt.Errorf("failed to run command '%s --scan': %s - %s", m.Path, err, string(out))
|
||||
}
|
||||
@@ -226,7 +236,7 @@ func (m *Smart) getAttributes(acc telegraf.Accumulator, devices []string) {
|
||||
wg.Add(len(devices))
|
||||
|
||||
for _, device := range devices {
|
||||
go gatherDisk(acc, m.UseSudo, m.Attributes, m.Path, m.Nocheck, device, &wg)
|
||||
go gatherDisk(acc, m.Timeout, m.UseSudo, m.Attributes, m.Path, m.Nocheck, device, &wg)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
@@ -243,12 +253,12 @@ func exitStatus(err error) (int, error) {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
func gatherDisk(acc telegraf.Accumulator, usesudo, collectAttributes bool, smartctl, nocheck, device string, wg *sync.WaitGroup) {
|
||||
func gatherDisk(acc telegraf.Accumulator, timeout internal.Duration, usesudo, collectAttributes bool, smartctl, nocheck, device string, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
// smartctl 5.41 & 5.42 have are broken regarding handling of --nocheck/-n
|
||||
args := []string{"--info", "--health", "--attributes", "--tolerance=verypermissive", "-n", nocheck, "--format=brief"}
|
||||
args = append(args, strings.Split(device, " ")...)
|
||||
out, e := runCmd(usesudo, smartctl, args...)
|
||||
out, e := runCmd(timeout, usesudo, smartctl, args...)
|
||||
outStr := string(out)
|
||||
|
||||
// Ignore all exit statuses except if it is a command line parse error
|
||||
@@ -436,14 +446,13 @@ func parseTemperature(fields, deviceFields map[string]interface{}, str string) e
|
||||
}
|
||||
|
||||
func init() {
|
||||
m := Smart{}
|
||||
path, _ := exec.LookPath("smartctl")
|
||||
if len(path) > 0 {
|
||||
m.Path = path
|
||||
}
|
||||
m.Nocheck = "standby"
|
||||
|
||||
inputs.Add("smart", func() telegraf.Input {
|
||||
return &m
|
||||
m := NewSmart()
|
||||
path, _ := exec.LookPath("smartctl")
|
||||
if len(path) > 0 {
|
||||
m.Path = path
|
||||
}
|
||||
m.Nocheck = "standby"
|
||||
return m
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user