Use C locale when running sadf (#2690)

fixes #1911
This commit is contained in:
Daniel Nelson
2017-04-21 10:55:54 -07:00
committed by GitHub
parent 38e1c1de77
commit da0773151b
3 changed files with 30 additions and 0 deletions

View File

@@ -210,11 +210,37 @@ func (s *Sysstat) collect() error {
return nil
}
func filterEnviron(env []string, prefix string) []string {
newenv := env[:0]
for _, envvar := range env {
if !strings.HasPrefix(envvar, prefix) {
newenv = append(newenv, envvar)
}
}
return newenv
}
// Return the Cmd with its environment configured to use the C locale
func withCLocale(cmd *exec.Cmd) *exec.Cmd {
var env []string
if cmd.Env != nil {
env = cmd.Env
} else {
env = os.Environ()
}
env = filterEnviron(env, "LANG")
env = filterEnviron(env, "LC_")
env = append(env, "LANG=C")
cmd.Env = env
return cmd
}
// parse runs Sadf on the previously saved tmpFile:
// Sadf -p -- -p <option> tmpFile
// and parses the output to add it to the telegraf.Accumulator acc.
func (s *Sysstat) parse(acc telegraf.Accumulator, option string, ts time.Time) error {
cmd := execCommand(s.Sadf, s.sadfOptions(option)...)
cmd = withCLocale(cmd)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err

View File

@@ -15,6 +15,9 @@ import (
// run with -race option, because in that scenario interval between the two
// Gather calls is greater than wantedInterval.
func TestInterval(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test with sleep in short mode.")
}
// overwriting exec commands with mock commands
execCommand = fakeExecCommand
defer func() { execCommand = exec.Command }()