2016-06-02 17:47:15 +00:00
|
|
|
package logparser
|
|
|
|
|
|
|
|
import (
|
2017-02-01 14:11:39 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-06-02 17:47:15 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/logparser/grok"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStartNoParsers(t *testing.T) {
|
|
|
|
logparser := &LogParserPlugin{
|
|
|
|
FromBeginning: true,
|
|
|
|
Files: []string{"grok/testdata/*.log"},
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
assert.Error(t, logparser.Start(&acc))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGrokParseLogFilesNonExistPattern(t *testing.T) {
|
|
|
|
thisdir := getCurrentDir()
|
|
|
|
p := &grok.Parser{
|
|
|
|
Patterns: []string{"%{FOOBAR}"},
|
|
|
|
CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"},
|
|
|
|
}
|
|
|
|
|
|
|
|
logparser := &LogParserPlugin{
|
|
|
|
FromBeginning: true,
|
|
|
|
Files: []string{thisdir + "grok/testdata/*.log"},
|
|
|
|
GrokParser: p,
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
2017-05-02 21:54:38 +00:00
|
|
|
err := logparser.Start(&acc)
|
|
|
|
assert.Error(t, err)
|
2016-06-02 17:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGrokParseLogFiles(t *testing.T) {
|
|
|
|
thisdir := getCurrentDir()
|
|
|
|
p := &grok.Parser{
|
|
|
|
Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_B}"},
|
|
|
|
CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"},
|
|
|
|
}
|
|
|
|
|
|
|
|
logparser := &LogParserPlugin{
|
|
|
|
FromBeginning: true,
|
|
|
|
Files: []string{thisdir + "grok/testdata/*.log"},
|
|
|
|
GrokParser: p,
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
assert.NoError(t, logparser.Start(&acc))
|
|
|
|
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.Wait(2)
|
|
|
|
|
2016-06-02 17:47:15 +00:00
|
|
|
logparser.Stop()
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t, "logparser_grok",
|
|
|
|
map[string]interface{}{
|
|
|
|
"clientip": "192.168.1.1",
|
|
|
|
"myfloat": float64(1.25),
|
|
|
|
"response_time": int64(5432),
|
|
|
|
"myint": int64(101),
|
|
|
|
},
|
|
|
|
map[string]string{"response_code": "200"})
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t, "logparser_grok",
|
|
|
|
map[string]interface{}{
|
|
|
|
"myfloat": 1.25,
|
|
|
|
"mystring": "mystring",
|
|
|
|
"nomodifier": "nomodifier",
|
|
|
|
},
|
|
|
|
map[string]string{})
|
|
|
|
}
|
|
|
|
|
2017-02-01 14:11:39 +00:00
|
|
|
func TestGrokParseLogFilesAppearLater(t *testing.T) {
|
|
|
|
emptydir, err := ioutil.TempDir("", "TestGrokParseLogFilesAppearLater")
|
|
|
|
defer os.RemoveAll(emptydir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
thisdir := getCurrentDir()
|
|
|
|
p := &grok.Parser{
|
|
|
|
Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_B}"},
|
|
|
|
CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"},
|
|
|
|
}
|
|
|
|
|
|
|
|
logparser := &LogParserPlugin{
|
|
|
|
FromBeginning: true,
|
|
|
|
Files: []string{emptydir + "/*.log"},
|
|
|
|
GrokParser: p,
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
assert.NoError(t, logparser.Start(&acc))
|
|
|
|
|
|
|
|
assert.Equal(t, acc.NFields(), 0)
|
|
|
|
|
2017-06-05 21:45:11 +00:00
|
|
|
_ = os.Symlink(thisdir+"grok/testdata/test_a.log", emptydir+"/test_a.log")
|
2017-04-24 18:13:26 +00:00
|
|
|
assert.NoError(t, acc.GatherError(logparser.Gather))
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.Wait(1)
|
2017-02-01 14:11:39 +00:00
|
|
|
|
|
|
|
logparser.Stop()
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t, "logparser_grok",
|
|
|
|
map[string]interface{}{
|
|
|
|
"clientip": "192.168.1.1",
|
|
|
|
"myfloat": float64(1.25),
|
|
|
|
"response_time": int64(5432),
|
|
|
|
"myint": int64(101),
|
|
|
|
},
|
|
|
|
map[string]string{"response_code": "200"})
|
|
|
|
}
|
|
|
|
|
2016-07-18 13:44:25 +00:00
|
|
|
// Test that test_a.log line gets parsed even though we don't have the correct
|
|
|
|
// pattern available for test_b.log
|
2016-06-02 17:47:15 +00:00
|
|
|
func TestGrokParseLogFilesOneBad(t *testing.T) {
|
|
|
|
thisdir := getCurrentDir()
|
|
|
|
p := &grok.Parser{
|
|
|
|
Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_BAD}"},
|
|
|
|
CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"},
|
|
|
|
}
|
|
|
|
assert.NoError(t, p.Compile())
|
|
|
|
|
|
|
|
logparser := &LogParserPlugin{
|
|
|
|
FromBeginning: true,
|
2016-07-18 13:44:25 +00:00
|
|
|
Files: []string{thisdir + "grok/testdata/test_a.log"},
|
2016-06-02 17:47:15 +00:00
|
|
|
GrokParser: p,
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
2016-07-18 13:44:25 +00:00
|
|
|
acc.SetDebug(true)
|
2016-06-02 17:47:15 +00:00
|
|
|
assert.NoError(t, logparser.Start(&acc))
|
|
|
|
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.Wait(1)
|
2016-06-02 17:47:15 +00:00
|
|
|
logparser.Stop()
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t, "logparser_grok",
|
|
|
|
map[string]interface{}{
|
|
|
|
"clientip": "192.168.1.1",
|
|
|
|
"myfloat": float64(1.25),
|
|
|
|
"response_time": int64(5432),
|
|
|
|
"myint": int64(101),
|
|
|
|
},
|
|
|
|
map[string]string{"response_code": "200"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCurrentDir() string {
|
|
|
|
_, filename, _, _ := runtime.Caller(1)
|
|
|
|
return strings.Replace(filename, "logparser_test.go", "", 1)
|
|
|
|
}
|