forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for non-transparent framing of syslog messages (influxdat…
- Loading branch information
1 parent
931d1e9
commit f1c5247
Showing
10 changed files
with
598 additions
and
117 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package syslog | ||
|
||
import ( | ||
"github.com/influxdata/telegraf/internal" | ||
"github.com/influxdata/telegraf/testutil" | ||
"time" | ||
) | ||
|
||
var ( | ||
pki = testutil.NewPKI("../../../testutil/pki") | ||
) | ||
|
||
type testCasePacket struct { | ||
name string | ||
data []byte | ||
wantBestEffort *testutil.Metric | ||
wantStrict *testutil.Metric | ||
werr bool | ||
} | ||
|
||
type testCaseStream struct { | ||
name string | ||
data []byte | ||
wantBestEffort []testutil.Metric | ||
wantStrict []testutil.Metric | ||
werr int // how many errors we expect in the strict mode? | ||
} | ||
|
||
func newUDPSyslogReceiver(address string, bestEffort bool) *Syslog { | ||
return &Syslog{ | ||
Address: address, | ||
now: func() time.Time { | ||
return defaultTime | ||
}, | ||
BestEffort: bestEffort, | ||
Separator: "_", | ||
} | ||
} | ||
|
||
func newTCPSyslogReceiver(address string, keepAlive *internal.Duration, maxConn int, bestEffort bool, f Framing) *Syslog { | ||
d := &internal.Duration{ | ||
Duration: defaultReadTimeout, | ||
} | ||
s := &Syslog{ | ||
Address: address, | ||
now: func() time.Time { | ||
return defaultTime | ||
}, | ||
Framing: f, | ||
ReadTimeout: d, | ||
BestEffort: bestEffort, | ||
Separator: "_", | ||
} | ||
if keepAlive != nil { | ||
s.KeepAlivePeriod = keepAlive | ||
} | ||
if maxConn > 0 { | ||
s.MaxConnections = maxConn | ||
} | ||
|
||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package syslog | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Framing represents the framing technique we expect the messages to come. | ||
type Framing int | ||
|
||
const ( | ||
// OctetCounting indicates the transparent framing technique for syslog transport. | ||
OctetCounting Framing = iota | ||
// NonTransparent indicates the non-transparent framing technique for syslog transport. | ||
NonTransparent | ||
) | ||
|
||
func (f Framing) String() string { | ||
switch f { | ||
case OctetCounting: | ||
return "OCTET-COUNTING" | ||
case NonTransparent: | ||
return "NON-TRANSPARENT" | ||
} | ||
return "" | ||
} | ||
|
||
// UnmarshalTOML implements ability to unmarshal framing from TOML files. | ||
func (f *Framing) UnmarshalTOML(data []byte) (err error) { | ||
return f.UnmarshalText(data) | ||
} | ||
|
||
// UnmarshalText implements encoding.TextUnmarshaler | ||
func (f *Framing) UnmarshalText(data []byte) (err error) { | ||
s := string(data) | ||
switch strings.ToUpper(s) { | ||
case `OCTET-COUNTING`: | ||
fallthrough | ||
case `"OCTET-COUNTING"`: | ||
fallthrough | ||
case `'OCTET-COUNTING'`: | ||
*f = OctetCounting | ||
return | ||
|
||
case `NON-TRANSPARENT`: | ||
fallthrough | ||
case `"NON-TRANSPARENT"`: | ||
fallthrough | ||
case `'NON-TRANSPARENT'`: | ||
*f = NonTransparent | ||
return | ||
} | ||
*f = -1 | ||
return fmt.Errorf("unknown framing") | ||
} | ||
|
||
// MarshalText implements encoding.TextMarshaler | ||
func (f Framing) MarshalText() ([]byte, error) { | ||
s := f.String() | ||
if s != "" { | ||
return []byte(s), nil | ||
} | ||
return nil, fmt.Errorf("unknown framing") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package syslog | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestFraming(t *testing.T) { | ||
var f1 Framing | ||
f1.UnmarshalTOML([]byte(`"non-transparent"`)) | ||
assert.Equal(t, NonTransparent, f1) | ||
|
||
var f2 Framing | ||
f2.UnmarshalTOML([]byte(`non-transparent`)) | ||
assert.Equal(t, NonTransparent, f2) | ||
|
||
var f3 Framing | ||
f3.UnmarshalTOML([]byte(`'non-transparent'`)) | ||
assert.Equal(t, NonTransparent, f3) | ||
|
||
var f4 Framing | ||
f4.UnmarshalTOML([]byte(`"octet-counting"`)) | ||
assert.Equal(t, OctetCounting, f4) | ||
|
||
var f5 Framing | ||
f5.UnmarshalTOML([]byte(`octet-counting`)) | ||
assert.Equal(t, OctetCounting, f5) | ||
|
||
var f6 Framing | ||
f6.UnmarshalTOML([]byte(`'octet-counting'`)) | ||
assert.Equal(t, OctetCounting, f6) | ||
|
||
var f7 Framing | ||
err := f7.UnmarshalTOML([]byte(`nope`)) | ||
assert.Equal(t, Framing(-1), f7) | ||
assert.Error(t, err) | ||
} |
Oops, something went wrong.