2016-06-02 17:47:15 +00:00
|
|
|
package grok
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-08-11 01:11:32 +00:00
|
|
|
"bytes"
|
2016-06-02 17:47:15 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2016-11-22 12:51:57 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2019-03-05 19:07:39 +00:00
|
|
|
"github.com/vjeantet/grok"
|
2016-06-02 17:47:15 +00:00
|
|
|
)
|
|
|
|
|
2016-07-22 16:17:50 +00:00
|
|
|
var timeLayouts = map[string]string{
|
2016-06-02 17:47:15 +00:00
|
|
|
"ts-ansic": "Mon Jan _2 15:04:05 2006",
|
|
|
|
"ts-unix": "Mon Jan _2 15:04:05 MST 2006",
|
|
|
|
"ts-ruby": "Mon Jan 02 15:04:05 -0700 2006",
|
|
|
|
"ts-rfc822": "02 Jan 06 15:04 MST",
|
|
|
|
"ts-rfc822z": "02 Jan 06 15:04 -0700", // RFC822 with numeric zone
|
|
|
|
"ts-rfc850": "Monday, 02-Jan-06 15:04:05 MST",
|
|
|
|
"ts-rfc1123": "Mon, 02 Jan 2006 15:04:05 MST",
|
|
|
|
"ts-rfc1123z": "Mon, 02 Jan 2006 15:04:05 -0700", // RFC1123 with numeric zone
|
|
|
|
"ts-rfc3339": "2006-01-02T15:04:05Z07:00",
|
|
|
|
"ts-rfc3339nano": "2006-01-02T15:04:05.999999999Z07:00",
|
|
|
|
"ts-httpd": "02/Jan/2006:15:04:05 -0700",
|
2019-10-05 00:38:48 +00:00
|
|
|
// These four are not exactly "layouts", but they are special cases that
|
2016-07-22 16:17:50 +00:00
|
|
|
// will get handled in the ParseLine function.
|
2019-10-05 00:38:48 +00:00
|
|
|
"ts-epoch": "EPOCH",
|
|
|
|
"ts-epochnano": "EPOCH_NANO",
|
|
|
|
"ts-epochmilli": "EPOCH_MILLI",
|
|
|
|
"ts-syslog": "SYSLOG_TIMESTAMP",
|
|
|
|
"ts": "GENERIC_TIMESTAMP", // try parsing all known timestamp layouts.
|
2016-06-02 17:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2018-08-17 20:45:22 +00:00
|
|
|
MEASUREMENT = "measurement"
|
2016-07-22 16:17:50 +00:00
|
|
|
INT = "int"
|
|
|
|
TAG = "tag"
|
|
|
|
FLOAT = "float"
|
|
|
|
STRING = "string"
|
|
|
|
DURATION = "duration"
|
|
|
|
DROP = "drop"
|
|
|
|
EPOCH = "EPOCH"
|
2019-10-05 00:38:48 +00:00
|
|
|
EPOCH_MILLI = "EPOCH_MILLI"
|
2016-07-22 16:17:50 +00:00
|
|
|
EPOCH_NANO = "EPOCH_NANO"
|
2018-05-23 23:37:14 +00:00
|
|
|
SYSLOG_TIMESTAMP = "SYSLOG_TIMESTAMP"
|
2016-07-22 16:17:50 +00:00
|
|
|
GENERIC_TIMESTAMP = "GENERIC_TIMESTAMP"
|
2016-06-02 17:47:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-07-22 16:17:50 +00:00
|
|
|
// matches named captures that contain a modifier.
|
2016-06-02 17:47:15 +00:00
|
|
|
// ie,
|
|
|
|
// %{NUMBER:bytes:int}
|
|
|
|
// %{IPORHOST:clientip:tag}
|
|
|
|
// %{HTTPDATE:ts1:ts-http}
|
|
|
|
// %{HTTPDATE:ts2:ts-"02 Jan 06 15:04"}
|
2016-07-22 16:17:50 +00:00
|
|
|
modifierRe = regexp.MustCompile(`%{\w+:(\w+):(ts-".+"|t?s?-?\w+)}`)
|
2016-06-02 17:47:15 +00:00
|
|
|
// matches a plain pattern name. ie, %{NUMBER}
|
|
|
|
patternOnlyRe = regexp.MustCompile(`%{(\w+)}`)
|
|
|
|
)
|
|
|
|
|
2017-06-05 21:45:11 +00:00
|
|
|
// Parser is the primary struct to handle and grok-patterns defined in the config toml
|
2016-06-02 17:47:15 +00:00
|
|
|
type Parser struct {
|
2016-07-18 13:44:25 +00:00
|
|
|
Patterns []string
|
|
|
|
// namedPatterns is a list of internally-assigned names to the patterns
|
|
|
|
// specified by the user in Patterns.
|
|
|
|
// They will look like:
|
|
|
|
// GROK_INTERNAL_PATTERN_0, GROK_INTERNAL_PATTERN_1, etc.
|
2018-07-14 06:22:59 +00:00
|
|
|
NamedPatterns []string
|
2016-06-02 17:47:15 +00:00
|
|
|
CustomPatterns string
|
|
|
|
CustomPatternFiles []string
|
2016-07-18 11:27:46 +00:00
|
|
|
Measurement string
|
2018-07-14 06:22:59 +00:00
|
|
|
DefaultTags map[string]string
|
2016-06-02 17:47:15 +00:00
|
|
|
|
2017-06-05 21:45:11 +00:00
|
|
|
// Timezone is an optional component to help render log dates to
|
|
|
|
// your chosen zone.
|
|
|
|
// Default: "" which renders UTC
|
|
|
|
// Options are as follows:
|
|
|
|
// 1. Local -- interpret based on machine localtime
|
|
|
|
// 2. "America/Chicago" -- Unix TZ values like those found in https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
|
|
// 3. UTC -- or blank/unspecified, will return timestamp in UTC
|
|
|
|
Timezone string
|
|
|
|
loc *time.Location
|
|
|
|
|
2019-02-27 01:35:57 +00:00
|
|
|
// UniqueTimestamp when set to "disable", timestamp will not incremented if there is a duplicate.
|
|
|
|
UniqueTimestamp string
|
|
|
|
|
2016-06-02 17:47:15 +00:00
|
|
|
// typeMap is a map of patterns -> capture name -> modifier,
|
|
|
|
// ie, {
|
|
|
|
// "%{TESTLOG}":
|
|
|
|
// {
|
|
|
|
// "bytes": "int",
|
|
|
|
// "clientip": "tag"
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
typeMap map[string]map[string]string
|
|
|
|
// tsMap is a map of patterns -> capture name -> timestamp layout.
|
|
|
|
// ie, {
|
|
|
|
// "%{TESTLOG}":
|
|
|
|
// {
|
|
|
|
// "httptime": "02/Jan/2006:15:04:05 -0700"
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
tsMap map[string]map[string]string
|
|
|
|
// patterns is a map of all of the parsed patterns from CustomPatterns
|
|
|
|
// and CustomPatternFiles.
|
|
|
|
// ie, {
|
|
|
|
// "DURATION": "%{NUMBER}[nuµm]?s"
|
|
|
|
// "RESPONSE_CODE": "%{NUMBER:rc:tag}"
|
|
|
|
// }
|
|
|
|
patterns map[string]string
|
2016-07-22 16:17:50 +00:00
|
|
|
// foundTsLayouts is a slice of timestamp patterns that have been found
|
|
|
|
// in the log lines. This slice gets updated if the user uses the generic
|
|
|
|
// 'ts' modifier for timestamps. This slice is checked first for matches,
|
|
|
|
// so that previously-matched layouts get priority over all other timestamp
|
|
|
|
// layouts.
|
|
|
|
foundTsLayouts []string
|
2016-06-02 17:47:15 +00:00
|
|
|
|
2018-05-23 23:37:14 +00:00
|
|
|
timeFunc func() time.Time
|
2016-06-02 17:47:15 +00:00
|
|
|
g *grok.Grok
|
|
|
|
tsModder *tsModder
|
|
|
|
}
|
|
|
|
|
2017-06-05 21:45:11 +00:00
|
|
|
// Compile is a bound method to Parser which will process the options for our parser
|
2016-06-02 17:47:15 +00:00
|
|
|
func (p *Parser) Compile() error {
|
|
|
|
p.typeMap = make(map[string]map[string]string)
|
|
|
|
p.tsMap = make(map[string]map[string]string)
|
|
|
|
p.patterns = make(map[string]string)
|
|
|
|
p.tsModder = &tsModder{}
|
|
|
|
var err error
|
|
|
|
p.g, err = grok.NewWithConfig(&grok.Config{NamedCapturesOnly: true})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-27 01:35:57 +00:00
|
|
|
if p.UniqueTimestamp == "" {
|
|
|
|
p.UniqueTimestamp = "auto"
|
|
|
|
}
|
|
|
|
|
2016-07-18 13:44:25 +00:00
|
|
|
// Give Patterns fake names so that they can be treated as named
|
|
|
|
// "custom patterns"
|
2018-07-14 06:22:59 +00:00
|
|
|
p.NamedPatterns = make([]string, 0, len(p.Patterns))
|
2016-07-18 13:44:25 +00:00
|
|
|
for i, pattern := range p.Patterns {
|
2018-04-11 01:16:21 +00:00
|
|
|
pattern = strings.TrimSpace(pattern)
|
2017-05-02 21:54:38 +00:00
|
|
|
if pattern == "" {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-18 13:44:25 +00:00
|
|
|
name := fmt.Sprintf("GROK_INTERNAL_PATTERN_%d", i)
|
|
|
|
p.CustomPatterns += "\n" + name + " " + pattern + "\n"
|
2018-07-14 06:22:59 +00:00
|
|
|
p.NamedPatterns = append(p.NamedPatterns, "%{"+name+"}")
|
2017-05-02 21:54:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 06:22:59 +00:00
|
|
|
if len(p.NamedPatterns) == 0 {
|
2017-05-02 21:54:38 +00:00
|
|
|
return fmt.Errorf("pattern required")
|
2016-07-18 13:44:25 +00:00
|
|
|
}
|
2016-06-02 17:47:15 +00:00
|
|
|
|
2016-07-18 13:44:25 +00:00
|
|
|
// Combine user-supplied CustomPatterns with DEFAULT_PATTERNS and parse
|
|
|
|
// them together as the same type of pattern.
|
|
|
|
p.CustomPatterns = DEFAULT_PATTERNS + p.CustomPatterns
|
2016-06-02 17:47:15 +00:00
|
|
|
if len(p.CustomPatterns) != 0 {
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(p.CustomPatterns))
|
|
|
|
p.addCustomPatterns(scanner)
|
|
|
|
}
|
|
|
|
|
2016-07-18 13:44:25 +00:00
|
|
|
// Parse any custom pattern files supplied.
|
2016-06-02 17:47:15 +00:00
|
|
|
for _, filename := range p.CustomPatternFiles {
|
2017-06-05 21:45:11 +00:00
|
|
|
file, fileErr := os.Open(filename)
|
|
|
|
if fileErr != nil {
|
|
|
|
return fileErr
|
2016-06-02 17:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(bufio.NewReader(file))
|
|
|
|
p.addCustomPatterns(scanner)
|
|
|
|
}
|
|
|
|
|
2017-06-05 21:45:11 +00:00
|
|
|
p.loc, err = time.LoadLocation(p.Timezone)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("W! improper timezone supplied (%s), setting loc to UTC", p.Timezone)
|
|
|
|
p.loc, _ = time.LoadLocation("UTC")
|
|
|
|
}
|
|
|
|
|
2018-05-23 23:37:14 +00:00
|
|
|
if p.timeFunc == nil {
|
|
|
|
p.timeFunc = time.Now
|
|
|
|
}
|
|
|
|
|
2016-06-02 17:47:15 +00:00
|
|
|
return p.compileCustomPatterns()
|
|
|
|
}
|
|
|
|
|
2017-06-05 21:45:11 +00:00
|
|
|
// ParseLine is the primary function to process individual lines, returning the metrics
|
2016-06-02 17:47:15 +00:00
|
|
|
func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
|
|
|
|
var err error
|
2016-07-22 16:17:50 +00:00
|
|
|
// values are the parsed fields from the log line
|
2016-06-02 17:47:15 +00:00
|
|
|
var values map[string]string
|
|
|
|
// the matching pattern string
|
|
|
|
var patternName string
|
2018-07-14 06:22:59 +00:00
|
|
|
for _, pattern := range p.NamedPatterns {
|
2016-06-02 17:47:15 +00:00
|
|
|
if values, err = p.g.Parse(pattern, line); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(values) != 0 {
|
|
|
|
patternName = pattern
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(values) == 0 {
|
2017-04-14 20:47:43 +00:00
|
|
|
log.Printf("D! Grok no match found for: %q", line)
|
2016-06-02 17:47:15 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
tags := make(map[string]string)
|
2018-07-14 06:22:59 +00:00
|
|
|
|
|
|
|
//add default tags
|
|
|
|
for k, v := range p.DefaultTags {
|
|
|
|
tags[k] = v
|
|
|
|
}
|
|
|
|
|
2016-06-02 17:47:15 +00:00
|
|
|
timestamp := time.Now()
|
|
|
|
for k, v := range values {
|
|
|
|
if k == "" || v == "" {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-22 16:17:50 +00:00
|
|
|
// t is the modifier of the field
|
2016-06-02 17:47:15 +00:00
|
|
|
var t string
|
|
|
|
// check if pattern has some modifiers
|
|
|
|
if types, ok := p.typeMap[patternName]; ok {
|
|
|
|
t = types[k]
|
|
|
|
}
|
|
|
|
// if we didn't find a modifier, check if we have a timestamp layout
|
|
|
|
if t == "" {
|
|
|
|
if ts, ok := p.tsMap[patternName]; ok {
|
|
|
|
// check if the modifier is a timestamp layout
|
|
|
|
if layout, ok := ts[k]; ok {
|
|
|
|
t = layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we didn't find a type OR timestamp modifier, assume string
|
|
|
|
if t == "" {
|
|
|
|
t = STRING
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t {
|
2018-08-17 20:45:22 +00:00
|
|
|
case MEASUREMENT:
|
|
|
|
p.Measurement = v
|
2016-06-02 17:47:15 +00:00
|
|
|
case INT:
|
2019-09-23 17:26:17 +00:00
|
|
|
iv, err := strconv.ParseInt(v, 0, 64)
|
2016-06-02 17:47:15 +00:00
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Error parsing %s to int: %s", v, err)
|
2016-06-02 17:47:15 +00:00
|
|
|
} else {
|
|
|
|
fields[k] = iv
|
|
|
|
}
|
|
|
|
case FLOAT:
|
|
|
|
fv, err := strconv.ParseFloat(v, 64)
|
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Error parsing %s to float: %s", v, err)
|
2016-06-02 17:47:15 +00:00
|
|
|
} else {
|
|
|
|
fields[k] = fv
|
|
|
|
}
|
|
|
|
case DURATION:
|
|
|
|
d, err := time.ParseDuration(v)
|
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Error parsing %s to duration: %s", v, err)
|
2016-06-02 17:47:15 +00:00
|
|
|
} else {
|
|
|
|
fields[k] = int64(d)
|
|
|
|
}
|
|
|
|
case TAG:
|
|
|
|
tags[k] = v
|
|
|
|
case STRING:
|
2019-03-22 21:02:15 +00:00
|
|
|
fields[k] = v
|
2016-07-22 16:17:50 +00:00
|
|
|
case EPOCH:
|
2017-10-19 23:36:32 +00:00
|
|
|
parts := strings.SplitN(v, ".", 2)
|
|
|
|
if len(parts) == 0 {
|
|
|
|
log.Printf("E! Error parsing %s to timestamp: %s", v, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
sec, err := strconv.ParseInt(parts[0], 10, 64)
|
2016-06-02 17:47:15 +00:00
|
|
|
if err != nil {
|
2017-10-19 23:36:32 +00:00
|
|
|
log.Printf("E! Error parsing %s to timestamp: %s", v, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ts := time.Unix(sec, 0)
|
|
|
|
|
|
|
|
if len(parts) == 2 {
|
|
|
|
padded := fmt.Sprintf("%-9s", parts[1])
|
|
|
|
nsString := strings.Replace(padded[:9], " ", "0", -1)
|
|
|
|
nanosec, err := strconv.ParseInt(nsString, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("E! Error parsing %s to timestamp: %s", v, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ts = ts.Add(time.Duration(nanosec) * time.Nanosecond)
|
2016-06-02 17:47:15 +00:00
|
|
|
}
|
2017-10-19 23:36:32 +00:00
|
|
|
timestamp = ts
|
2019-10-05 00:38:48 +00:00
|
|
|
case EPOCH_MILLI:
|
|
|
|
ms, err := strconv.ParseInt(v, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("E! Error parsing %s to int: %s", v, err)
|
|
|
|
} else {
|
|
|
|
timestamp = time.Unix(0, ms*int64(time.Millisecond))
|
|
|
|
}
|
2016-07-22 16:17:50 +00:00
|
|
|
case EPOCH_NANO:
|
2016-06-02 17:47:15 +00:00
|
|
|
iv, err := strconv.ParseInt(v, 10, 64)
|
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Error parsing %s to int: %s", v, err)
|
2016-06-02 17:47:15 +00:00
|
|
|
} else {
|
|
|
|
timestamp = time.Unix(0, iv)
|
|
|
|
}
|
2018-05-23 23:37:14 +00:00
|
|
|
case SYSLOG_TIMESTAMP:
|
2018-06-30 01:05:46 +00:00
|
|
|
ts, err := time.ParseInLocation(time.Stamp, v, p.loc)
|
2018-05-23 23:37:14 +00:00
|
|
|
if err == nil {
|
|
|
|
if ts.Year() == 0 {
|
|
|
|
ts = ts.AddDate(timestamp.Year(), 0, 0)
|
|
|
|
}
|
|
|
|
timestamp = ts
|
|
|
|
} else {
|
|
|
|
log.Printf("E! Error parsing %s to time layout [%s]: %s", v, t, err)
|
|
|
|
}
|
2016-07-22 16:17:50 +00:00
|
|
|
case GENERIC_TIMESTAMP:
|
|
|
|
var foundTs bool
|
|
|
|
// first try timestamp layouts that we've already found
|
|
|
|
for _, layout := range p.foundTsLayouts {
|
2017-06-05 21:45:11 +00:00
|
|
|
ts, err := time.ParseInLocation(layout, v, p.loc)
|
2016-07-22 16:17:50 +00:00
|
|
|
if err == nil {
|
|
|
|
timestamp = ts
|
|
|
|
foundTs = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we haven't found a timestamp layout yet, try all timestamp
|
|
|
|
// layouts.
|
|
|
|
if !foundTs {
|
|
|
|
for _, layout := range timeLayouts {
|
2017-06-05 21:45:11 +00:00
|
|
|
ts, err := time.ParseInLocation(layout, v, p.loc)
|
2016-07-22 16:17:50 +00:00
|
|
|
if err == nil {
|
|
|
|
timestamp = ts
|
|
|
|
foundTs = true
|
|
|
|
p.foundTsLayouts = append(p.foundTsLayouts, layout)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we still haven't found a timestamp layout, log it and we will
|
|
|
|
// just use time.Now()
|
|
|
|
if !foundTs {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Error parsing timestamp [%s], could not find any "+
|
2016-07-22 16:17:50 +00:00
|
|
|
"suitable time layouts.", v)
|
|
|
|
}
|
2016-06-02 17:47:15 +00:00
|
|
|
case DROP:
|
|
|
|
// goodbye!
|
|
|
|
default:
|
2018-06-21 21:19:15 +00:00
|
|
|
v = strings.Replace(v, ",", ".", -1)
|
2017-06-05 21:45:11 +00:00
|
|
|
ts, err := time.ParseInLocation(t, v, p.loc)
|
2016-06-02 17:47:15 +00:00
|
|
|
if err == nil {
|
2018-08-30 02:11:13 +00:00
|
|
|
if ts.Year() == 0 {
|
|
|
|
ts = ts.AddDate(timestamp.Year(), 0, 0)
|
|
|
|
}
|
2016-06-02 17:47:15 +00:00
|
|
|
timestamp = ts
|
|
|
|
} else {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Error parsing %s to time layout [%s]: %s", v, t, err)
|
2016-06-02 17:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 01:35:57 +00:00
|
|
|
if p.UniqueTimestamp != "auto" {
|
|
|
|
return metric.New(p.Measurement, tags, fields, timestamp)
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
return metric.New(p.Measurement, tags, fields, p.tsModder.tsMod(timestamp))
|
2016-06-02 17:47:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 06:22:59 +00:00
|
|
|
func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
|
|
|
|
|
2018-08-11 01:11:32 +00:00
|
|
|
metrics := make([]telegraf.Metric, 0)
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(bytes.NewReader(buf))
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2018-07-14 06:22:59 +00:00
|
|
|
m, err := p.ParseLine(line)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-11 01:11:32 +00:00
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-07-14 06:22:59 +00:00
|
|
|
metrics = append(metrics, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return metrics, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) SetDefaultTags(tags map[string]string) {
|
|
|
|
p.DefaultTags = tags
|
|
|
|
}
|
|
|
|
|
2016-06-02 17:47:15 +00:00
|
|
|
func (p *Parser) addCustomPatterns(scanner *bufio.Scanner) {
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
if len(line) > 0 && line[0] != '#' {
|
|
|
|
names := strings.SplitN(line, " ", 2)
|
|
|
|
p.patterns[names[0]] = names[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) compileCustomPatterns() error {
|
|
|
|
var err error
|
|
|
|
// check if the pattern contains a subpattern that is already defined
|
|
|
|
// replace it with the subpattern for modifier inheritance.
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
for name, pattern := range p.patterns {
|
|
|
|
subNames := patternOnlyRe.FindAllStringSubmatch(pattern, -1)
|
|
|
|
for _, subName := range subNames {
|
|
|
|
if subPattern, ok := p.patterns[subName[1]]; ok {
|
|
|
|
pattern = strings.Replace(pattern, subName[0], subPattern, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.patterns[name] = pattern
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if pattern contains modifiers. Parse them out if it does.
|
|
|
|
for name, pattern := range p.patterns {
|
2016-07-22 16:17:50 +00:00
|
|
|
if modifierRe.MatchString(pattern) {
|
2016-06-02 17:47:15 +00:00
|
|
|
// this pattern has modifiers, so parse out the modifiers
|
|
|
|
pattern, err = p.parseTypedCaptures(name, pattern)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.patterns[name] = pattern
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.g.AddPatternsFromMap(p.patterns)
|
|
|
|
}
|
|
|
|
|
2016-07-22 16:17:50 +00:00
|
|
|
// parseTypedCaptures parses the capture modifiers, and then deletes the
|
|
|
|
// modifier from the line so that it is a valid "grok" pattern again.
|
2016-06-02 17:47:15 +00:00
|
|
|
// ie,
|
|
|
|
// %{NUMBER:bytes:int} => %{NUMBER:bytes} (stores %{NUMBER}->bytes->int)
|
|
|
|
// %{IPORHOST:clientip:tag} => %{IPORHOST:clientip} (stores %{IPORHOST}->clientip->tag)
|
|
|
|
func (p *Parser) parseTypedCaptures(name, pattern string) (string, error) {
|
2016-07-22 16:17:50 +00:00
|
|
|
matches := modifierRe.FindAllStringSubmatch(pattern, -1)
|
2016-06-02 17:47:15 +00:00
|
|
|
|
|
|
|
// grab the name of the capture pattern
|
|
|
|
patternName := "%{" + name + "}"
|
|
|
|
// create type map for this pattern
|
|
|
|
p.typeMap[patternName] = make(map[string]string)
|
|
|
|
p.tsMap[patternName] = make(map[string]string)
|
|
|
|
|
|
|
|
// boolean to verify that each pattern only has a single ts- data type.
|
|
|
|
hasTimestamp := false
|
|
|
|
for _, match := range matches {
|
|
|
|
// regex capture 1 is the name of the capture
|
2016-07-22 16:17:50 +00:00
|
|
|
// regex capture 2 is the modifier of the capture
|
|
|
|
if strings.HasPrefix(match[2], "ts") {
|
2016-06-02 17:47:15 +00:00
|
|
|
if hasTimestamp {
|
|
|
|
return pattern, fmt.Errorf("logparser pattern compile error: "+
|
|
|
|
"Each pattern is allowed only one named "+
|
|
|
|
"timestamp data type. pattern: %s", pattern)
|
|
|
|
}
|
2016-07-22 16:17:50 +00:00
|
|
|
if layout, ok := timeLayouts[match[2]]; ok {
|
|
|
|
// built-in time format
|
|
|
|
p.tsMap[patternName][match[1]] = layout
|
2016-06-02 17:47:15 +00:00
|
|
|
} else {
|
2016-07-22 16:17:50 +00:00
|
|
|
// custom time format
|
2016-06-02 17:47:15 +00:00
|
|
|
p.tsMap[patternName][match[1]] = strings.TrimSuffix(strings.TrimPrefix(match[2], `ts-"`), `"`)
|
|
|
|
}
|
|
|
|
hasTimestamp = true
|
|
|
|
} else {
|
|
|
|
p.typeMap[patternName][match[1]] = match[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
// the modifier is not a valid part of a "grok" pattern, so remove it
|
|
|
|
// from the pattern.
|
|
|
|
pattern = strings.Replace(pattern, ":"+match[2]+"}", "}", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pattern, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// tsModder is a struct for incrementing identical timestamps of log lines
|
|
|
|
// so that we don't push identical metrics that will get overwritten.
|
|
|
|
type tsModder struct {
|
|
|
|
dupe time.Time
|
|
|
|
last time.Time
|
|
|
|
incr time.Duration
|
|
|
|
incrn time.Duration
|
|
|
|
rollover time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// tsMod increments the given timestamp one unit more from the previous
|
|
|
|
// duplicate timestamp.
|
|
|
|
// the increment unit is determined as the next smallest time unit below the
|
|
|
|
// most significant time unit of ts.
|
|
|
|
// ie, if the input is at ms precision, it will increment it 1µs.
|
|
|
|
func (t *tsModder) tsMod(ts time.Time) time.Time {
|
2018-08-30 02:11:13 +00:00
|
|
|
if ts.IsZero() {
|
|
|
|
return ts
|
|
|
|
}
|
2016-06-02 17:47:15 +00:00
|
|
|
defer func() { t.last = ts }()
|
|
|
|
// don't mod the time if we don't need to
|
|
|
|
if t.last.IsZero() || ts.IsZero() {
|
|
|
|
t.incrn = 0
|
|
|
|
t.rollover = 0
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
if !ts.Equal(t.last) && !ts.Equal(t.dupe) {
|
|
|
|
t.incr = 0
|
|
|
|
t.incrn = 0
|
|
|
|
t.rollover = 0
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
if ts.Equal(t.last) {
|
|
|
|
t.dupe = ts
|
|
|
|
}
|
|
|
|
|
|
|
|
if ts.Equal(t.dupe) && t.incr == time.Duration(0) {
|
|
|
|
tsNano := ts.UnixNano()
|
|
|
|
|
|
|
|
d := int64(10)
|
|
|
|
counter := 1
|
|
|
|
for {
|
|
|
|
a := tsNano % d
|
|
|
|
if a > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
d = d * 10
|
|
|
|
counter++
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case counter <= 6:
|
|
|
|
t.incr = time.Nanosecond
|
|
|
|
case counter <= 9:
|
|
|
|
t.incr = time.Microsecond
|
|
|
|
case counter > 9:
|
|
|
|
t.incr = time.Millisecond
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.incrn++
|
|
|
|
if t.incrn == 999 && t.incr > time.Nanosecond {
|
|
|
|
t.rollover = t.incr * t.incrn
|
|
|
|
t.incrn = 1
|
|
|
|
t.incr = t.incr / 1000
|
|
|
|
if t.incr < time.Nanosecond {
|
|
|
|
t.incr = time.Nanosecond
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ts.Add(t.incr*t.incrn + t.rollover)
|
|
|
|
}
|