2016-02-06 00:36:35 +00:00
|
|
|
package parsers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
2017-04-12 17:41:26 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/collectd"
|
2018-01-08 23:11:36 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/dropwizard"
|
2016-02-06 00:36:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/graphite"
|
2018-06-21 22:56:20 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/grok"
|
2016-02-06 00:36:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/influx"
|
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/json"
|
2016-02-25 04:32:22 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/nagios"
|
2016-03-18 00:01:01 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/value"
|
2016-02-06 00:36:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ParserInput is an interface for input plugins that are able to parse
|
|
|
|
// arbitrary data formats.
|
|
|
|
type ParserInput interface {
|
|
|
|
// SetParser sets the parser function for the interface
|
|
|
|
SetParser(parser Parser)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser is an interface defining functions that a parser plugin must satisfy.
|
|
|
|
type Parser interface {
|
|
|
|
// Parse takes a byte buffer separated by newlines
|
|
|
|
// ie, `cpu.usage.idle 90\ncpu.usage.busy 10`
|
|
|
|
// and parses it into telegraf metrics
|
2018-05-14 18:00:03 +00:00
|
|
|
//
|
|
|
|
// Must be thread-safe.
|
2016-02-06 00:36:35 +00:00
|
|
|
Parse(buf []byte) ([]telegraf.Metric, error)
|
|
|
|
|
|
|
|
// ParseLine takes a single string metric
|
|
|
|
// ie, "cpu.usage.idle 90"
|
|
|
|
// and parses it into a telegraf metric.
|
2018-05-14 18:00:03 +00:00
|
|
|
//
|
|
|
|
// Must be thread-safe.
|
2016-02-06 00:36:35 +00:00
|
|
|
ParseLine(line string) (telegraf.Metric, error)
|
2016-02-09 22:03:46 +00:00
|
|
|
|
|
|
|
// SetDefaultTags tells the parser to add all of the given tags
|
|
|
|
// to each parsed metric.
|
|
|
|
// NOTE: do _not_ modify the map after you've passed it here!!
|
|
|
|
SetDefaultTags(tags map[string]string)
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config is a struct that covers the data types needed for all parser types,
|
|
|
|
// and can be used to instantiate _any_ of the parsers.
|
|
|
|
type Config struct {
|
2016-02-25 04:32:22 +00:00
|
|
|
// Dataformat can be one of: json, influx, graphite, value, nagios
|
2016-02-06 00:36:35 +00:00
|
|
|
DataFormat string
|
|
|
|
|
|
|
|
// Separator only applied to Graphite data.
|
|
|
|
Separator string
|
|
|
|
// Templates only apply to Graphite data.
|
|
|
|
Templates []string
|
|
|
|
|
|
|
|
// TagKeys only apply to JSON data
|
|
|
|
TagKeys []string
|
2016-03-18 00:01:01 +00:00
|
|
|
// MetricName applies to JSON & value. This will be the name of the measurement.
|
2016-02-06 00:36:35 +00:00
|
|
|
MetricName string
|
|
|
|
|
2017-04-12 17:41:26 +00:00
|
|
|
// Authentication file for collectd
|
|
|
|
CollectdAuthFile string
|
|
|
|
// One of none (default), sign, or encrypt
|
|
|
|
CollectdSecurityLevel string
|
|
|
|
// Dataset specification for collectd
|
|
|
|
CollectdTypesDB []string
|
|
|
|
|
2016-03-18 00:01:01 +00:00
|
|
|
// DataType only applies to value, this will be the type to parse value to
|
|
|
|
DataType string
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
// DefaultTags are the default tags that will be added to all parsed metrics.
|
|
|
|
DefaultTags map[string]string
|
2018-01-08 23:11:36 +00:00
|
|
|
|
|
|
|
// an optional json path containing the metric registry object
|
|
|
|
// if left empty, the whole json object is parsed as a metric registry
|
|
|
|
DropwizardMetricRegistryPath string
|
|
|
|
// an optional json path containing the default time of the metrics
|
|
|
|
// if left empty, the processing time is used
|
|
|
|
DropwizardTimePath string
|
|
|
|
// time format to use for parsing the time field
|
|
|
|
// defaults to time.RFC3339
|
|
|
|
DropwizardTimeFormat string
|
|
|
|
// an optional json path pointing to a json object with tag key/value pairs
|
|
|
|
// takes precedence over DropwizardTagPathsMap
|
|
|
|
DropwizardTagsPath string
|
|
|
|
// an optional map containing tag names as keys and json paths to retrieve the tag values from as values
|
|
|
|
// used if TagsPath is empty or doesn't return any tags
|
|
|
|
DropwizardTagPathsMap map[string]string
|
2018-06-21 22:56:20 +00:00
|
|
|
|
|
|
|
//grok patterns
|
|
|
|
Patterns []string
|
|
|
|
NamedPatterns []string
|
|
|
|
CustomPatterns string
|
|
|
|
CustomPatternFiles []string
|
2018-06-25 16:53:35 +00:00
|
|
|
TimeZone string
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewParser returns a Parser interface based on the given config.
|
|
|
|
func NewParser(config *Config) (Parser, error) {
|
|
|
|
var err error
|
|
|
|
var parser Parser
|
|
|
|
switch config.DataFormat {
|
|
|
|
case "json":
|
|
|
|
parser, err = NewJSONParser(config.MetricName,
|
|
|
|
config.TagKeys, config.DefaultTags)
|
2016-03-18 00:01:01 +00:00
|
|
|
case "value":
|
|
|
|
parser, err = NewValueParser(config.MetricName,
|
|
|
|
config.DataType, config.DefaultTags)
|
2016-02-06 00:36:35 +00:00
|
|
|
case "influx":
|
|
|
|
parser, err = NewInfluxParser()
|
2016-02-25 04:32:22 +00:00
|
|
|
case "nagios":
|
|
|
|
parser, err = NewNagiosParser()
|
2016-02-06 00:36:35 +00:00
|
|
|
case "graphite":
|
|
|
|
parser, err = NewGraphiteParser(config.Separator,
|
|
|
|
config.Templates, config.DefaultTags)
|
2017-04-12 17:41:26 +00:00
|
|
|
case "collectd":
|
|
|
|
parser, err = NewCollectdParser(config.CollectdAuthFile,
|
|
|
|
config.CollectdSecurityLevel, config.CollectdTypesDB)
|
2018-01-08 23:11:36 +00:00
|
|
|
case "dropwizard":
|
2018-05-14 18:00:03 +00:00
|
|
|
parser, err = NewDropwizardParser(
|
|
|
|
config.DropwizardMetricRegistryPath,
|
|
|
|
config.DropwizardTimePath,
|
|
|
|
config.DropwizardTimeFormat,
|
|
|
|
config.DropwizardTagsPath,
|
|
|
|
config.DropwizardTagPathsMap,
|
|
|
|
config.DefaultTags,
|
|
|
|
config.Separator,
|
|
|
|
config.Templates)
|
2018-06-26 21:19:38 +00:00
|
|
|
|
2018-06-21 22:56:20 +00:00
|
|
|
case "grok":
|
|
|
|
parser, err = NewGrokParser(
|
|
|
|
config.MetricName,
|
|
|
|
config.Patterns,
|
|
|
|
config.NamedPatterns,
|
|
|
|
config.CustomPatterns,
|
2018-06-25 16:53:35 +00:00
|
|
|
config.CustomPatternFiles,
|
|
|
|
config.TimeZone)
|
2016-02-06 00:36:35 +00:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Invalid data format: %s", config.DataFormat)
|
|
|
|
}
|
|
|
|
return parser, err
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:56:20 +00:00
|
|
|
func NewGrokParser(metricName string,
|
|
|
|
patterns []string,
|
|
|
|
nPatterns []string,
|
|
|
|
cPatterns string,
|
2018-06-25 16:53:35 +00:00
|
|
|
cPatternFiles []string, tZone string) (Parser, error) {
|
2018-06-21 22:56:20 +00:00
|
|
|
parser := grok.Parser{
|
|
|
|
Measurement: metricName,
|
|
|
|
Patterns: patterns,
|
|
|
|
NamedPatterns: nPatterns,
|
|
|
|
CustomPatterns: cPatterns,
|
|
|
|
CustomPatternFiles: cPatternFiles,
|
2018-06-25 16:53:35 +00:00
|
|
|
Timezone: tZone,
|
2018-06-21 22:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parser.Compile()
|
|
|
|
return &parser, nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
func NewJSONParser(
|
|
|
|
metricName string,
|
|
|
|
tagKeys []string,
|
|
|
|
defaultTags map[string]string,
|
|
|
|
) (Parser, error) {
|
|
|
|
parser := &json.JSONParser{
|
|
|
|
MetricName: metricName,
|
|
|
|
TagKeys: tagKeys,
|
|
|
|
DefaultTags: defaultTags,
|
|
|
|
}
|
|
|
|
return parser, nil
|
|
|
|
}
|
|
|
|
|
2016-02-25 04:32:22 +00:00
|
|
|
func NewNagiosParser() (Parser, error) {
|
|
|
|
return &nagios.NagiosParser{}, nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
func NewInfluxParser() (Parser, error) {
|
2018-03-28 00:30:51 +00:00
|
|
|
handler := influx.NewMetricHandler()
|
|
|
|
return influx.NewParser(handler), nil
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGraphiteParser(
|
|
|
|
separator string,
|
|
|
|
templates []string,
|
|
|
|
defaultTags map[string]string,
|
|
|
|
) (Parser, error) {
|
|
|
|
return graphite.NewGraphiteParser(separator, templates, defaultTags)
|
|
|
|
}
|
2016-03-18 00:01:01 +00:00
|
|
|
|
|
|
|
func NewValueParser(
|
|
|
|
metricName string,
|
|
|
|
dataType string,
|
|
|
|
defaultTags map[string]string,
|
|
|
|
) (Parser, error) {
|
|
|
|
return &value.ValueParser{
|
|
|
|
MetricName: metricName,
|
|
|
|
DataType: dataType,
|
|
|
|
DefaultTags: defaultTags,
|
|
|
|
}, nil
|
|
|
|
}
|
2017-04-12 17:41:26 +00:00
|
|
|
|
|
|
|
func NewCollectdParser(
|
|
|
|
authFile string,
|
|
|
|
securityLevel string,
|
|
|
|
typesDB []string,
|
|
|
|
) (Parser, error) {
|
|
|
|
return collectd.NewCollectdParser(authFile, securityLevel, typesDB)
|
|
|
|
}
|
2018-01-08 23:11:36 +00:00
|
|
|
|
|
|
|
func NewDropwizardParser(
|
|
|
|
metricRegistryPath string,
|
|
|
|
timePath string,
|
|
|
|
timeFormat string,
|
|
|
|
tagsPath string,
|
|
|
|
tagPathsMap map[string]string,
|
|
|
|
defaultTags map[string]string,
|
|
|
|
separator string,
|
|
|
|
templates []string,
|
|
|
|
|
|
|
|
) (Parser, error) {
|
2018-05-14 18:00:03 +00:00
|
|
|
parser := dropwizard.NewParser()
|
|
|
|
parser.MetricRegistryPath = metricRegistryPath
|
|
|
|
parser.TimePath = timePath
|
|
|
|
parser.TimeFormat = timeFormat
|
|
|
|
parser.TagsPath = tagsPath
|
|
|
|
parser.TagPathsMap = tagPathsMap
|
|
|
|
parser.DefaultTags = defaultTags
|
|
|
|
err := parser.SetTemplates(separator, templates)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-01-08 23:11:36 +00:00
|
|
|
}
|
|
|
|
return parser, err
|
|
|
|
}
|