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"
|
2020-01-03 00:14:32 +00:00
|
|
|
"time"
|
2016-06-02 17:47:15 +00:00
|
|
|
|
2020-01-03 00:14:32 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-06-02 17:47:15 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-03 00:14:32 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-06-02 17:47:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStartNoParsers(t *testing.T) {
|
|
|
|
logparser := &LogParserPlugin{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-06-02 17:47:15 +00:00
|
|
|
FromBeginning: true,
|
2018-12-11 00:14:20 +00:00
|
|
|
Files: []string{"testdata/*.log"},
|
2016-06-02 17:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
assert.Error(t, logparser.Start(&acc))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGrokParseLogFilesNonExistPattern(t *testing.T) {
|
|
|
|
thisdir := getCurrentDir()
|
|
|
|
|
|
|
|
logparser := &LogParserPlugin{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-06-02 17:47:15 +00:00
|
|
|
FromBeginning: true,
|
2018-12-11 00:14:20 +00:00
|
|
|
Files: []string{thisdir + "testdata/*.log"},
|
2018-07-14 06:22:59 +00:00
|
|
|
GrokConfig: GrokConfig{
|
|
|
|
Patterns: []string{"%{FOOBAR}"},
|
2018-12-11 00:14:20 +00:00
|
|
|
CustomPatternFiles: []string{thisdir + "testdata/test-patterns"},
|
2018-07-14 06:22:59 +00:00
|
|
|
},
|
2016-06-02 17:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
logparser := &LogParserPlugin{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2018-07-14 06:22:59 +00:00
|
|
|
GrokConfig: GrokConfig{
|
|
|
|
MeasurementName: "logparser_grok",
|
2019-10-05 00:38:48 +00:00
|
|
|
Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_B}", "%{TEST_LOG_C}"},
|
2018-12-11 00:14:20 +00:00
|
|
|
CustomPatternFiles: []string{thisdir + "testdata/test-patterns"},
|
2018-07-14 06:22:59 +00:00
|
|
|
},
|
2016-06-02 17:47:15 +00:00
|
|
|
FromBeginning: true,
|
2018-12-11 00:14:20 +00:00
|
|
|
Files: []string{thisdir + "testdata/*.log"},
|
2016-06-02 17:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
2020-01-03 00:14:32 +00:00
|
|
|
require.NoError(t, logparser.Start(&acc))
|
|
|
|
acc.Wait(3)
|
2017-03-24 19:03:36 +00:00
|
|
|
|
2016-06-02 17:47:15 +00:00
|
|
|
logparser.Stop()
|
|
|
|
|
2020-01-03 00:14:32 +00:00
|
|
|
expected := []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"logparser_grok",
|
|
|
|
map[string]string{
|
|
|
|
"response_code": "200",
|
|
|
|
"path": thisdir + "testdata/test_a.log",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"clientip": "192.168.1.1",
|
|
|
|
"myfloat": float64(1.25),
|
|
|
|
"response_time": int64(5432),
|
|
|
|
"myint": int64(101),
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
testutil.MustMetric(
|
|
|
|
"logparser_grok",
|
|
|
|
map[string]string{
|
|
|
|
"path": thisdir + "testdata/test_b.log",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"myfloat": 1.25,
|
|
|
|
"mystring": "mystring",
|
|
|
|
"nomodifier": "nomodifier",
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
testutil.MustMetric(
|
|
|
|
"logparser_grok",
|
|
|
|
map[string]string{
|
|
|
|
"path": thisdir + "testdata/test_c.log",
|
|
|
|
"response_code": "200",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"clientip": "192.168.1.1",
|
|
|
|
"myfloat": 1.25,
|
|
|
|
"myint": 101,
|
|
|
|
"response_time": 5432,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
}
|
2016-06-02 17:47:15 +00:00
|
|
|
|
2020-01-03 00:14:32 +00:00
|
|
|
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(),
|
|
|
|
testutil.IgnoreTime(), testutil.SortMetrics())
|
2016-06-02 17:47:15 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
logparser := &LogParserPlugin{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2017-02-01 14:11:39 +00:00
|
|
|
FromBeginning: true,
|
|
|
|
Files: []string{emptydir + "/*.log"},
|
2018-07-14 06:22:59 +00:00
|
|
|
GrokConfig: GrokConfig{
|
|
|
|
MeasurementName: "logparser_grok",
|
|
|
|
Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_B}"},
|
2018-12-11 00:14:20 +00:00
|
|
|
CustomPatternFiles: []string{thisdir + "testdata/test-patterns"},
|
2018-07-14 06:22:59 +00:00
|
|
|
},
|
2017-02-01 14:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
assert.NoError(t, logparser.Start(&acc))
|
|
|
|
|
|
|
|
assert.Equal(t, acc.NFields(), 0)
|
|
|
|
|
2018-12-11 00:14:20 +00:00
|
|
|
_ = os.Symlink(thisdir+"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),
|
|
|
|
},
|
2017-08-07 23:16:31 +00:00
|
|
|
map[string]string{
|
|
|
|
"response_code": "200",
|
|
|
|
"path": emptydir + "/test_a.log",
|
|
|
|
})
|
2017-02-01 14:11:39 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
logparser := &LogParserPlugin{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-06-02 17:47:15 +00:00
|
|
|
FromBeginning: true,
|
2018-12-11 00:14:20 +00:00
|
|
|
Files: []string{thisdir + "testdata/test_a.log"},
|
2018-07-14 06:22:59 +00:00
|
|
|
GrokConfig: GrokConfig{
|
|
|
|
MeasurementName: "logparser_grok",
|
|
|
|
Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_BAD}"},
|
2018-12-11 00:14:20 +00:00
|
|
|
CustomPatternFiles: []string{thisdir + "testdata/test-patterns"},
|
2018-07-14 06:22:59 +00:00
|
|
|
},
|
2016-06-02 17:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
},
|
2017-08-07 23:16:31 +00:00
|
|
|
map[string]string{
|
|
|
|
"response_code": "200",
|
2018-12-11 00:14:20 +00:00
|
|
|
"path": thisdir + "testdata/test_a.log",
|
2017-08-07 23:16:31 +00:00
|
|
|
})
|
2016-06-02 17:47:15 +00:00
|
|
|
}
|
|
|
|
|
2019-10-05 00:38:48 +00:00
|
|
|
func TestGrokParseLogFiles_TimestampInEpochMilli(t *testing.T) {
|
|
|
|
thisdir := getCurrentDir()
|
|
|
|
|
|
|
|
logparser := &LogParserPlugin{
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
GrokConfig: GrokConfig{
|
|
|
|
MeasurementName: "logparser_grok",
|
|
|
|
Patterns: []string{"%{TEST_LOG_C}"},
|
|
|
|
CustomPatternFiles: []string{thisdir + "testdata/test-patterns"},
|
|
|
|
},
|
|
|
|
FromBeginning: true,
|
|
|
|
Files: []string{thisdir + "testdata/test_c.log"},
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
acc.SetDebug(true)
|
|
|
|
assert.NoError(t, logparser.Start(&acc))
|
|
|
|
acc.Wait(1)
|
|
|
|
|
|
|
|
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",
|
|
|
|
"path": thisdir + "testdata/test_c.log",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 17:47:15 +00:00
|
|
|
func getCurrentDir() string {
|
|
|
|
_, filename, _, _ := runtime.Caller(1)
|
|
|
|
return strings.Replace(filename, "logparser_test.go", "", 1)
|
|
|
|
}
|