Skip to content

Commit 5c99d32

Browse files
butitsnotmejackzampolin
authored andcommitted
Added option to remove all CRs from input stream
Added the option removecr to inputs.exec to remove all carraige returns (CR, ASCII 0x0D, Unicode codepoint \u0D, ^M). The option is boolean and not enabled if not present in the config file. closes #1606 Updated CHANGELOG.md with information about removecr Ran go fmt ./... Moved removal of CRs to internal/internal.go Moved the code to remove carriage returns from plugins/inputs/exec/exec.go to internal/internal.go. Additionally changed the conditional on which it gets applied from using a configuration file option to checking if it is running on Windows. Moved Carriage Return check to correct place Moved the carriage return removal back to the exec plugin. Added unit testing for it. Fixed a bug (removing too many characters). Ran go fmt ./... Reverted CHANGELOG to master Updated Changelog
1 parent 9aebad4 commit 5c99d32

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Features
66

7+
- [#1606](https://github.com/influxdata/telegraf/pull/1606): Remove carraige returns from exec plugin output on Windows
78
- [#1674](https://github.com/influxdata/telegraf/issues/1674): elasticsearch input: configurable timeout.
89
- [#1607](https://github.com/influxdata/telegraf/pull/1607): Massage metric names in Instrumental output plugin
910
- [#1572](https://github.com/influxdata/telegraf/pull/1572): mesos improvements.

plugins/inputs/exec/exec.go

+28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os/exec"
77
"path/filepath"
8+
"runtime"
89
"strings"
910
"sync"
1011
"syscall"
@@ -114,9 +115,36 @@ func (c CommandRunner) Run(
114115
}
115116
}
116117

118+
out = removeCarriageReturns(out)
117119
return out.Bytes(), nil
118120
}
119121

122+
// removeCarriageReturns removes all carriage returns from the input if the
123+
// OS is Windows. It does not return any errors.
124+
func removeCarriageReturns(b bytes.Buffer) bytes.Buffer {
125+
if runtime.GOOS == "windows" {
126+
var buf bytes.Buffer
127+
for {
128+
byt, er := b.ReadBytes(0x0D)
129+
end := len(byt)
130+
if nil == er {
131+
end -= 1
132+
}
133+
if nil != byt {
134+
buf.Write(byt[:end])
135+
} else {
136+
break
137+
}
138+
if nil != er {
139+
break
140+
}
141+
}
142+
b = buf
143+
}
144+
return b
145+
146+
}
147+
120148
func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator, wg *sync.WaitGroup) {
121149
defer wg.Done()
122150

plugins/inputs/exec/exec_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package exec
22

33
import (
4+
"bytes"
45
"fmt"
6+
"runtime"
57
"testing"
68

79
"github.com/influxdata/telegraf"
@@ -46,6 +48,29 @@ cpu,cpu=cpu5,host=foo,datacenter=us-east usage_idle=99,usage_busy=1
4648
cpu,cpu=cpu6,host=foo,datacenter=us-east usage_idle=99,usage_busy=1
4749
`
4850

51+
type CarriageReturnTest struct {
52+
input []byte
53+
output []byte
54+
}
55+
56+
var crTests = []CarriageReturnTest{
57+
{[]byte{0x4c, 0x69, 0x6e, 0x65, 0x20, 0x31, 0x0d, 0x0a, 0x4c, 0x69,
58+
0x6e, 0x65, 0x20, 0x32, 0x0d, 0x0a, 0x4c, 0x69, 0x6e, 0x65,
59+
0x20, 0x33},
60+
[]byte{0x4c, 0x69, 0x6e, 0x65, 0x20, 0x31, 0x0a, 0x4c, 0x69, 0x6e,
61+
0x65, 0x20, 0x32, 0x0a, 0x4c, 0x69, 0x6e, 0x65, 0x20, 0x33}},
62+
{[]byte{0x4c, 0x69, 0x6e, 0x65, 0x20, 0x31, 0x0a, 0x4c, 0x69, 0x6e,
63+
0x65, 0x20, 0x32, 0x0a, 0x4c, 0x69, 0x6e, 0x65, 0x20, 0x33},
64+
[]byte{0x4c, 0x69, 0x6e, 0x65, 0x20, 0x31, 0x0a, 0x4c, 0x69, 0x6e,
65+
0x65, 0x20, 0x32, 0x0a, 0x4c, 0x69, 0x6e, 0x65, 0x20, 0x33}},
66+
{[]byte{0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x6c,
67+
0x6c, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x62, 0x69, 0x67, 0x20,
68+
0x6c, 0x69, 0x6e, 0x65},
69+
[]byte{0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x6c,
70+
0x6c, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x62, 0x69, 0x67, 0x20,
71+
0x6c, 0x69, 0x6e, 0x65}},
72+
}
73+
4974
type runnerMock struct {
5075
out []byte
5176
err error
@@ -217,3 +242,21 @@ func TestExecCommandWithoutGlobAndPath(t *testing.T) {
217242
}
218243
acc.AssertContainsFields(t, "metric", fields)
219244
}
245+
246+
func TestRemoveCarriageReturns(t *testing.T) {
247+
if runtime.GOOS == "windows" {
248+
// Test that all carriage returns are removed
249+
for _, test := range crTests {
250+
b := bytes.NewBuffer(test.input)
251+
out := removeCarriageReturns(*b)
252+
assert.True(t, bytes.Equal(test.output, out.Bytes()))
253+
}
254+
} else {
255+
// Test that the buffer is returned unaltered
256+
for _, test := range crTests {
257+
b := bytes.NewBuffer(test.input)
258+
out := removeCarriageReturns(*b)
259+
assert.True(t, bytes.Equal(test.input, out.Bytes()))
260+
}
261+
}
262+
}

0 commit comments

Comments
 (0)