Skip to content

Commit

Permalink
Use C locale when running sadf (#2690)
Browse files Browse the repository at this point in the history
fixes #1911
  • Loading branch information
danielnelson authored Apr 21, 2017
1 parent 1d4843b commit 8a1b070
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ be deprecated eventually.
- [#2671](https://github.com/influxdata/telegraf/issues/2671): The internal input plugin uses the wrong units for `heap_objects`
- [#2684](https://github.com/influxdata/telegraf/pull/2684): Fix ipmi_sensor config is shared between all plugin instances
- [#2450](https://github.com/influxdata/telegraf/issues/2450): Network statistics not collected when system has alias interfaces
- [#1911](https://github.com/influxdata/telegraf/issues/1911): Sysstat plugin needs LANG=C or similar locale

## v1.2.1 [2017-02-01]

Expand Down
26 changes: 26 additions & 0 deletions plugins/inputs/sysstat/sysstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions plugins/inputs/sysstat/sysstat_interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }()
Expand Down

0 comments on commit 8a1b070

Please sign in to comment.