2015-11-24 21:22:11 +00:00
|
|
|
package config
|
2015-04-01 16:34:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-11-13 23:14:07 +00:00
|
|
|
"log"
|
2015-10-19 07:09:36 +00:00
|
|
|
"path/filepath"
|
2015-04-01 16:34:32 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-11-26 01:42:07 +00:00
|
|
|
"github.com/influxdb/telegraf/internal"
|
2015-08-25 23:59:12 +00:00
|
|
|
"github.com/influxdb/telegraf/outputs"
|
2015-05-22 23:45:14 +00:00
|
|
|
"github.com/influxdb/telegraf/plugins"
|
2015-11-24 21:22:11 +00:00
|
|
|
|
2015-04-01 16:34:32 +00:00
|
|
|
"github.com/naoina/toml"
|
|
|
|
"github.com/naoina/toml/ast"
|
2015-12-01 14:15:28 +00:00
|
|
|
|
|
|
|
"github.com/influxdb/influxdb/client/v2"
|
2015-04-01 16:34:32 +00:00
|
|
|
)
|
|
|
|
|
2015-08-11 16:34:00 +00:00
|
|
|
// Config specifies the URL/user/password for the database that telegraf
|
2015-08-04 14:58:32 +00:00
|
|
|
// will be logging to, as well as all the plugins that the user has
|
|
|
|
// specified
|
2015-04-01 16:34:32 +00:00
|
|
|
type Config struct {
|
2015-11-24 21:22:11 +00:00
|
|
|
Tags map[string]string
|
|
|
|
PluginFilters []string
|
|
|
|
OutputFilters []string
|
2015-08-11 20:02:04 +00:00
|
|
|
|
2015-11-26 01:42:07 +00:00
|
|
|
Agent *AgentConfig
|
2015-11-24 21:22:11 +00:00
|
|
|
Plugins []*RunningPlugin
|
|
|
|
Outputs []*RunningOutput
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 23:28:11 +00:00
|
|
|
func NewConfig() *Config {
|
|
|
|
c := &Config{
|
2015-11-26 01:42:07 +00:00
|
|
|
// Agent defaults:
|
|
|
|
Agent: &AgentConfig{
|
|
|
|
Interval: internal.Duration{10 * time.Second},
|
|
|
|
RoundInterval: true,
|
|
|
|
FlushInterval: internal.Duration{10 * time.Second},
|
|
|
|
FlushRetries: 2,
|
|
|
|
FlushJitter: internal.Duration{5 * time.Second},
|
|
|
|
},
|
|
|
|
|
2015-11-24 21:22:11 +00:00
|
|
|
Tags: make(map[string]string),
|
|
|
|
Plugins: make([]*RunningPlugin, 0),
|
|
|
|
Outputs: make([]*RunningOutput, 0),
|
|
|
|
PluginFilters: make([]string, 0),
|
|
|
|
OutputFilters: make([]string, 0),
|
2015-11-23 23:28:11 +00:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2015-11-26 01:42:07 +00:00
|
|
|
type AgentConfig struct {
|
|
|
|
// Interval at which to gather information
|
|
|
|
Interval internal.Duration
|
|
|
|
|
|
|
|
// RoundInterval rounds collection interval to 'interval'.
|
|
|
|
// ie, if Interval=10s then always collect on :00, :10, :20, etc.
|
|
|
|
RoundInterval bool
|
|
|
|
|
|
|
|
// Interval at which to flush data
|
|
|
|
FlushInterval internal.Duration
|
|
|
|
|
|
|
|
// FlushRetries is the number of times to retry each data flush
|
|
|
|
FlushRetries int
|
|
|
|
|
|
|
|
// FlushJitter tells
|
|
|
|
FlushJitter internal.Duration
|
|
|
|
|
|
|
|
// TODO(cam): Remove UTC and Precision parameters, they are no longer
|
|
|
|
// valid for the agent config. Leaving them here for now for backwards-
|
|
|
|
// compatability
|
|
|
|
UTC bool `toml:"utc"`
|
|
|
|
Precision string
|
|
|
|
|
|
|
|
// Option for running in debug mode
|
|
|
|
Debug bool
|
|
|
|
Hostname string
|
|
|
|
}
|
|
|
|
|
2015-08-12 21:22:55 +00:00
|
|
|
// TagFilter is the name of a tag, and the values on which to filter
|
2015-08-10 19:41:20 +00:00
|
|
|
type TagFilter struct {
|
|
|
|
Name string
|
|
|
|
Filter []string
|
|
|
|
}
|
|
|
|
|
2015-11-24 21:22:11 +00:00
|
|
|
type RunningOutput struct {
|
|
|
|
Name string
|
|
|
|
Output outputs.Output
|
2015-12-01 14:15:28 +00:00
|
|
|
Config *OutputConfig
|
2015-11-24 21:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RunningPlugin struct {
|
|
|
|
Name string
|
|
|
|
Plugin plugins.Plugin
|
|
|
|
Config *PluginConfig
|
|
|
|
}
|
|
|
|
|
2015-12-01 14:15:28 +00:00
|
|
|
// Filter containing drop/pass and tagdrop/tagpass rules
|
|
|
|
type Filter struct {
|
2015-05-20 05:19:32 +00:00
|
|
|
Drop []string
|
|
|
|
Pass []string
|
|
|
|
|
2015-08-10 19:41:20 +00:00
|
|
|
TagDrop []TagFilter
|
|
|
|
TagPass []TagFilter
|
|
|
|
|
2015-12-01 14:15:28 +00:00
|
|
|
IsActive bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// PluginConfig containing a name, interval, and filter
|
|
|
|
type PluginConfig struct {
|
|
|
|
Name string
|
|
|
|
Filter Filter
|
2015-05-20 05:19:32 +00:00
|
|
|
Interval time.Duration
|
|
|
|
}
|
|
|
|
|
2015-12-01 14:15:28 +00:00
|
|
|
// OutputConfig containing name and filter
|
|
|
|
type OutputConfig struct {
|
|
|
|
Name string
|
|
|
|
Filter Filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter returns filtered slice of client.Points based on whether filters
|
|
|
|
// are active for this RunningOutput.
|
|
|
|
func (ro *RunningOutput) FilterPoints(points []*client.Point) []*client.Point {
|
|
|
|
if !ro.Config.Filter.IsActive {
|
|
|
|
return points
|
|
|
|
}
|
|
|
|
|
|
|
|
var filteredPoints []*client.Point
|
|
|
|
for i := range points {
|
|
|
|
if !ro.Config.Filter.ShouldPass(points[i].Name()) || !ro.Config.Filter.ShouldTagsPass(points[i].Tags()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
filteredPoints = append(filteredPoints, points[i])
|
|
|
|
}
|
|
|
|
return filteredPoints
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// ShouldPass returns true if the metric should pass, false if should drop
|
2015-12-01 14:15:28 +00:00
|
|
|
// based on the drop/pass filter parameters
|
|
|
|
func (f Filter) ShouldPass(measurement string) bool {
|
|
|
|
if f.Pass != nil {
|
|
|
|
for _, pat := range f.Pass {
|
2015-05-29 20:25:48 +00:00
|
|
|
if strings.HasPrefix(measurement, pat) {
|
2015-05-20 05:19:32 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-01 14:15:28 +00:00
|
|
|
if f.Drop != nil {
|
|
|
|
for _, pat := range f.Drop {
|
2015-05-29 20:25:48 +00:00
|
|
|
if strings.HasPrefix(measurement, pat) {
|
2015-05-20 05:19:32 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2015-11-24 01:00:54 +00:00
|
|
|
return true
|
|
|
|
}
|
2015-05-20 05:19:32 +00:00
|
|
|
|
2015-11-24 01:00:54 +00:00
|
|
|
// ShouldTagsPass returns true if the metric should pass, false if should drop
|
2015-12-01 14:15:28 +00:00
|
|
|
// based on the tagdrop/tagpass filter parameters
|
|
|
|
func (f Filter) ShouldTagsPass(tags map[string]string) bool {
|
|
|
|
if f.TagPass != nil {
|
|
|
|
for _, pat := range f.TagPass {
|
2015-08-10 19:41:20 +00:00
|
|
|
if tagval, ok := tags[pat.Name]; ok {
|
|
|
|
for _, filter := range pat.Filter {
|
|
|
|
if filter == tagval {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-01 14:15:28 +00:00
|
|
|
if f.TagDrop != nil {
|
|
|
|
for _, pat := range f.TagDrop {
|
2015-08-10 19:41:20 +00:00
|
|
|
if tagval, ok := tags[pat.Name]; ok {
|
|
|
|
for _, filter := range pat.Filter {
|
|
|
|
if filter == tagval {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-05-20 05:19:32 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-11-24 21:22:11 +00:00
|
|
|
// Plugins returns a list of strings of the configured plugins.
|
|
|
|
func (c *Config) PluginNames() []string {
|
|
|
|
var name []string
|
|
|
|
for _, plugin := range c.Plugins {
|
|
|
|
name = append(name, plugin.Name)
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
2015-11-24 21:22:11 +00:00
|
|
|
return name
|
2015-08-07 20:31:25 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 21:22:11 +00:00
|
|
|
// Outputs returns a list of strings of the configured plugins.
|
|
|
|
func (c *Config) OutputNames() []string {
|
|
|
|
var name []string
|
|
|
|
for _, output := range c.Outputs {
|
|
|
|
name = append(name, output.Name)
|
2015-11-24 01:00:54 +00:00
|
|
|
}
|
2015-11-24 21:22:11 +00:00
|
|
|
return name
|
2015-11-24 01:00:54 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// ListTags returns a string of tags specified in the config,
|
|
|
|
// line-protocol style
|
2015-04-01 16:34:32 +00:00
|
|
|
func (c *Config) ListTags() string {
|
|
|
|
var tags []string
|
|
|
|
|
|
|
|
for k, v := range c.Tags {
|
|
|
|
tags = append(tags, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(tags)
|
|
|
|
|
|
|
|
return strings.Join(tags, " ")
|
|
|
|
}
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-05-22 23:45:14 +00:00
|
|
|
var header = `# Telegraf configuration
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-05-22 23:45:14 +00:00
|
|
|
# Telegraf is entirely plugin driven. All metrics are gathered from the
|
2015-05-18 22:10:11 +00:00
|
|
|
# declared plugins.
|
|
|
|
|
|
|
|
# Even if a plugin has no configuration, it must be declared in here
|
|
|
|
# to be active. Declaring a plugin means just specifying the name
|
2015-05-22 23:26:32 +00:00
|
|
|
# as a section with no variables. To deactivate a plugin, comment
|
|
|
|
# out the name and any variables.
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-05-22 23:45:14 +00:00
|
|
|
# Use 'telegraf -config telegraf.toml -test' to see what metrics a config
|
2015-05-18 22:51:11 +00:00
|
|
|
# file would generate.
|
|
|
|
|
2015-05-18 23:11:45 +00:00
|
|
|
# One rule that plugins conform to is wherever a connection string
|
2015-05-18 23:08:22 +00:00
|
|
|
# can be passed, the values '' and 'localhost' are treated specially.
|
|
|
|
# They indicate to the plugin to use their own builtin configuration to
|
|
|
|
# connect to the local system.
|
|
|
|
|
2015-05-21 16:45:09 +00:00
|
|
|
# NOTE: The configuration has a few required parameters. They are marked
|
|
|
|
# with 'required'. Be sure to edit those to make this configuration work.
|
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
# Tags can also be specified via a normal map, but only one form at a time:
|
|
|
|
[tags]
|
2015-10-15 21:53:29 +00:00
|
|
|
# dc = "us-east-1"
|
2015-05-22 23:26:32 +00:00
|
|
|
|
2015-09-02 16:30:44 +00:00
|
|
|
# Configuration for telegraf agent
|
2015-08-25 23:59:12 +00:00
|
|
|
[agent]
|
2015-10-15 21:53:29 +00:00
|
|
|
# Default data collection interval for all plugins
|
|
|
|
interval = "10s"
|
2015-10-21 20:05:27 +00:00
|
|
|
# Rounds collection interval to 'interval'
|
|
|
|
# ie, if interval="10s" then always collect on :00, :10, :20, etc.
|
|
|
|
round_interval = true
|
|
|
|
|
2015-10-23 17:23:08 +00:00
|
|
|
# Default data flushing interval for all outputs. You should not set this below
|
|
|
|
# interval. Maximum flush_interval will be flush_interval + flush_jitter
|
2015-10-16 22:13:32 +00:00
|
|
|
flush_interval = "10s"
|
2015-10-23 17:23:08 +00:00
|
|
|
# Jitter the flush interval by a random amount. This is primarily to avoid
|
|
|
|
# large write spikes for users running a large number of telegraf instances.
|
|
|
|
# ie, a jitter of 5s and interval 10s means flushes will happen every 10-15s
|
|
|
|
flush_jitter = "0s"
|
2015-10-21 20:05:27 +00:00
|
|
|
|
|
|
|
# Run telegraf in debug mode
|
2015-10-15 21:53:29 +00:00
|
|
|
debug = false
|
|
|
|
# Override default hostname, if empty use os.Hostname()
|
|
|
|
hostname = ""
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-26 17:02:10 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
###############################################################################
|
|
|
|
# OUTPUTS #
|
|
|
|
###############################################################################
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
[outputs]
|
|
|
|
`
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-09-24 18:06:11 +00:00
|
|
|
var pluginHeader = `
|
2015-05-20 05:19:32 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
###############################################################################
|
|
|
|
# PLUGINS #
|
|
|
|
###############################################################################
|
2015-11-20 02:08:02 +00:00
|
|
|
|
|
|
|
[plugins]
|
2015-05-18 22:10:11 +00:00
|
|
|
`
|
|
|
|
|
2015-09-24 18:06:11 +00:00
|
|
|
var servicePluginHeader = `
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# SERVICE PLUGINS #
|
|
|
|
###############################################################################
|
|
|
|
`
|
|
|
|
|
2015-09-22 01:38:57 +00:00
|
|
|
// PrintSampleConfig prints the sample config
|
|
|
|
func PrintSampleConfig(pluginFilters []string, outputFilters []string) {
|
2015-05-18 22:10:11 +00:00
|
|
|
fmt.Printf(header)
|
|
|
|
|
2015-09-24 18:06:11 +00:00
|
|
|
// Filter outputs
|
2015-08-25 23:59:12 +00:00
|
|
|
var onames []string
|
|
|
|
for oname := range outputs.Outputs {
|
2015-09-22 01:38:57 +00:00
|
|
|
if len(outputFilters) == 0 || sliceContains(oname, outputFilters) {
|
|
|
|
onames = append(onames, oname)
|
|
|
|
}
|
2015-05-18 22:10:11 +00:00
|
|
|
}
|
2015-08-25 23:59:12 +00:00
|
|
|
sort.Strings(onames)
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-09-24 18:06:11 +00:00
|
|
|
// Print Outputs
|
2015-08-25 23:59:12 +00:00
|
|
|
for _, oname := range onames {
|
|
|
|
creator := outputs.Outputs[oname]
|
|
|
|
output := creator()
|
2015-11-24 21:22:11 +00:00
|
|
|
printConfig(oname, output, "outputs")
|
2015-08-25 23:59:12 +00:00
|
|
|
}
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-09-24 18:06:11 +00:00
|
|
|
// Filter plugins
|
2015-08-25 23:59:12 +00:00
|
|
|
var pnames []string
|
|
|
|
for pname := range plugins.Plugins {
|
2015-09-22 01:38:57 +00:00
|
|
|
if len(pluginFilters) == 0 || sliceContains(pname, pluginFilters) {
|
|
|
|
pnames = append(pnames, pname)
|
|
|
|
}
|
2015-08-25 23:59:12 +00:00
|
|
|
}
|
|
|
|
sort.Strings(pnames)
|
|
|
|
|
2015-09-24 18:06:11 +00:00
|
|
|
// Print Plugins
|
|
|
|
fmt.Printf(pluginHeader)
|
|
|
|
servPlugins := make(map[string]plugins.ServicePlugin)
|
2015-08-25 23:59:12 +00:00
|
|
|
for _, pname := range pnames {
|
|
|
|
creator := plugins.Plugins[pname]
|
|
|
|
plugin := creator()
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-09-24 18:06:11 +00:00
|
|
|
switch p := plugin.(type) {
|
|
|
|
case plugins.ServicePlugin:
|
|
|
|
servPlugins[pname] = p
|
|
|
|
continue
|
2015-05-18 22:10:11 +00:00
|
|
|
}
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2015-11-24 21:22:11 +00:00
|
|
|
printConfig(pname, plugin, "plugins")
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print Service Plugins
|
|
|
|
fmt.Printf(servicePluginHeader)
|
|
|
|
for name, plugin := range servPlugins {
|
2015-11-24 21:22:11 +00:00
|
|
|
printConfig(name, plugin, "plugins")
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 20:24:51 +00:00
|
|
|
type printer interface {
|
|
|
|
Description() string
|
|
|
|
SampleConfig() string
|
|
|
|
}
|
|
|
|
|
2015-11-24 21:22:11 +00:00
|
|
|
func printConfig(name string, p printer, op string) {
|
|
|
|
fmt.Printf("\n# %s\n[[%s.%s]]", p.Description(), op, name)
|
2015-10-22 20:24:51 +00:00
|
|
|
config := p.SampleConfig()
|
2015-09-24 18:06:11 +00:00
|
|
|
if config == "" {
|
2015-10-15 21:53:29 +00:00
|
|
|
fmt.Printf("\n # no configuration\n")
|
2015-09-24 18:06:11 +00:00
|
|
|
} else {
|
|
|
|
fmt.Printf(config)
|
2015-05-18 22:10:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-24 20:52:46 +00:00
|
|
|
|
2015-09-22 01:38:57 +00:00
|
|
|
func sliceContains(name string, list []string) bool {
|
|
|
|
for _, b := range list {
|
|
|
|
if b == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-08-24 20:52:46 +00:00
|
|
|
// PrintPluginConfig prints the config usage of a single plugin.
|
|
|
|
func PrintPluginConfig(name string) error {
|
|
|
|
if creator, ok := plugins.Plugins[name]; ok {
|
2015-11-24 21:22:11 +00:00
|
|
|
printConfig(name, creator(), "plugins")
|
2015-08-24 20:52:46 +00:00
|
|
|
} else {
|
|
|
|
return errors.New(fmt.Sprintf("Plugin %s not found", name))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-17 04:47:13 +00:00
|
|
|
|
2015-10-22 20:24:51 +00:00
|
|
|
// PrintOutputConfig prints the config usage of a single output.
|
|
|
|
func PrintOutputConfig(name string) error {
|
|
|
|
if creator, ok := outputs.Outputs[name]; ok {
|
2015-11-24 21:22:11 +00:00
|
|
|
printConfig(name, creator(), "outputs")
|
2015-10-22 20:24:51 +00:00
|
|
|
} else {
|
|
|
|
return errors.New(fmt.Sprintf("Output %s not found", name))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-19 07:09:36 +00:00
|
|
|
func (c *Config) LoadDirectory(path string) error {
|
|
|
|
directoryEntries, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, entry := range directoryEntries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := entry.Name()
|
2015-11-26 01:42:07 +00:00
|
|
|
if len(name) < 6 || name[len(name)-5:] != ".conf" {
|
2015-10-19 07:09:36 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-11-23 23:28:11 +00:00
|
|
|
err := c.LoadConfig(filepath.Join(path, name))
|
2015-10-19 07:09:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-24 21:22:11 +00:00
|
|
|
// LoadConfig loads the given config file and applies it to c
|
2015-11-23 23:28:11 +00:00
|
|
|
func (c *Config) LoadConfig(path string) error {
|
2015-10-17 04:47:13 +00:00
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
2015-11-23 23:28:11 +00:00
|
|
|
return err
|
2015-10-17 04:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tbl, err := toml.Parse(data)
|
|
|
|
if err != nil {
|
2015-11-23 23:28:11 +00:00
|
|
|
return err
|
2015-10-17 04:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, val := range tbl.Fields {
|
2015-11-13 23:14:07 +00:00
|
|
|
subTable, ok := val.(*ast.Table)
|
2015-10-17 04:47:13 +00:00
|
|
|
if !ok {
|
2015-11-23 23:28:11 +00:00
|
|
|
return errors.New("invalid configuration")
|
2015-10-17 04:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "agent":
|
2015-11-26 01:42:07 +00:00
|
|
|
if err = toml.UnmarshalTable(subTable, c.Agent); err != nil {
|
|
|
|
log.Printf("Could not parse [agent] config\n")
|
|
|
|
return err
|
|
|
|
}
|
2015-10-17 04:47:13 +00:00
|
|
|
case "tags":
|
2015-11-13 23:14:07 +00:00
|
|
|
if err = toml.UnmarshalTable(subTable, c.Tags); err != nil {
|
|
|
|
log.Printf("Could not parse [tags] config\n")
|
2015-11-23 23:28:11 +00:00
|
|
|
return err
|
2015-10-17 04:47:13 +00:00
|
|
|
}
|
|
|
|
case "outputs":
|
2015-11-13 23:14:07 +00:00
|
|
|
for outputName, outputVal := range subTable.Fields {
|
|
|
|
switch outputSubTable := outputVal.(type) {
|
|
|
|
case *ast.Table:
|
2015-11-24 21:22:11 +00:00
|
|
|
if err = c.addOutput(outputName, outputSubTable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-13 23:14:07 +00:00
|
|
|
case []*ast.Table:
|
2015-11-24 21:22:11 +00:00
|
|
|
for _, t := range outputSubTable {
|
|
|
|
if err = c.addOutput(outputName, t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-13 23:14:07 +00:00
|
|
|
}
|
|
|
|
default:
|
2015-11-23 23:28:11 +00:00
|
|
|
return fmt.Errorf("Unsupported config format: %s",
|
2015-11-13 23:14:07 +00:00
|
|
|
outputName)
|
2015-10-17 04:47:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-20 02:08:02 +00:00
|
|
|
case "plugins":
|
|
|
|
for pluginName, pluginVal := range subTable.Fields {
|
|
|
|
switch pluginSubTable := pluginVal.(type) {
|
|
|
|
case *ast.Table:
|
2015-11-24 21:22:11 +00:00
|
|
|
if err = c.addPlugin(pluginName, pluginSubTable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-20 02:08:02 +00:00
|
|
|
case []*ast.Table:
|
2015-11-24 21:22:11 +00:00
|
|
|
for _, t := range pluginSubTable {
|
|
|
|
if err = c.addPlugin(pluginName, t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-20 02:08:02 +00:00
|
|
|
}
|
|
|
|
default:
|
2015-11-23 23:28:11 +00:00
|
|
|
return fmt.Errorf("Unsupported config format: %s",
|
2015-11-20 02:08:02 +00:00
|
|
|
pluginName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Assume it's a plugin for legacy config file support if no other
|
|
|
|
// identifiers are present
|
2015-10-17 04:47:13 +00:00
|
|
|
default:
|
2015-11-24 21:22:11 +00:00
|
|
|
if err = c.addPlugin(name, subTable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-17 04:47:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-24 21:22:11 +00:00
|
|
|
|
|
|
|
func (c *Config) addOutput(name string, table *ast.Table) error {
|
|
|
|
if len(c.OutputFilters) > 0 && !sliceContains(name, c.OutputFilters) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
creator, ok := outputs.Outputs[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Undefined but requested output: %s", name)
|
|
|
|
}
|
2015-12-01 14:15:28 +00:00
|
|
|
output := creator()
|
2015-11-24 21:22:11 +00:00
|
|
|
|
2015-12-01 14:15:28 +00:00
|
|
|
outputConfig, err := buildOutput(name, table)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := toml.UnmarshalTable(table, output); err != nil {
|
2015-11-24 21:22:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ro := &RunningOutput{
|
|
|
|
Name: name,
|
2015-12-01 14:15:28 +00:00
|
|
|
Output: output,
|
|
|
|
Config: outputConfig,
|
2015-11-24 21:22:11 +00:00
|
|
|
}
|
|
|
|
c.Outputs = append(c.Outputs, ro)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) addPlugin(name string, table *ast.Table) error {
|
|
|
|
if len(c.PluginFilters) > 0 && !sliceContains(name, c.PluginFilters) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
creator, ok := plugins.Plugins[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Undefined but requested plugin: %s", name)
|
|
|
|
}
|
|
|
|
plugin := creator()
|
|
|
|
|
2015-12-01 14:15:28 +00:00
|
|
|
pluginConfig, err := buildPlugin(name, table)
|
2015-11-24 21:22:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-01 14:15:28 +00:00
|
|
|
|
|
|
|
if err := toml.UnmarshalTable(table, plugin); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-24 21:22:11 +00:00
|
|
|
rp := &RunningPlugin{
|
|
|
|
Name: name,
|
|
|
|
Plugin: plugin,
|
|
|
|
Config: pluginConfig,
|
|
|
|
}
|
|
|
|
c.Plugins = append(c.Plugins, rp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-01 14:15:28 +00:00
|
|
|
// buildFilter builds a Filter (tagpass/tagdrop/pass/drop) to
|
|
|
|
// be inserted into the OutputConfig/PluginConfig to be used for prefix
|
|
|
|
// filtering on tags and measurements
|
|
|
|
func buildFilter(tbl *ast.Table) Filter {
|
|
|
|
f := Filter{}
|
2015-11-24 21:22:11 +00:00
|
|
|
|
|
|
|
if node, ok := tbl.Fields["pass"]; ok {
|
|
|
|
if kv, ok := node.(*ast.KeyValue); ok {
|
|
|
|
if ary, ok := kv.Value.(*ast.Array); ok {
|
|
|
|
for _, elem := range ary.Value {
|
|
|
|
if str, ok := elem.(*ast.String); ok {
|
2015-12-01 14:15:28 +00:00
|
|
|
f.Pass = append(f.Pass, str.Value)
|
|
|
|
f.IsActive = true
|
2015-11-24 21:22:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if node, ok := tbl.Fields["drop"]; ok {
|
|
|
|
if kv, ok := node.(*ast.KeyValue); ok {
|
|
|
|
if ary, ok := kv.Value.(*ast.Array); ok {
|
|
|
|
for _, elem := range ary.Value {
|
|
|
|
if str, ok := elem.(*ast.String); ok {
|
2015-12-01 14:15:28 +00:00
|
|
|
f.Drop = append(f.Drop, str.Value)
|
|
|
|
f.IsActive = true
|
2015-11-24 21:22:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if node, ok := tbl.Fields["tagpass"]; ok {
|
|
|
|
if subtbl, ok := node.(*ast.Table); ok {
|
|
|
|
for name, val := range subtbl.Fields {
|
|
|
|
if kv, ok := val.(*ast.KeyValue); ok {
|
|
|
|
tagfilter := &TagFilter{Name: name}
|
|
|
|
if ary, ok := kv.Value.(*ast.Array); ok {
|
|
|
|
for _, elem := range ary.Value {
|
|
|
|
if str, ok := elem.(*ast.String); ok {
|
|
|
|
tagfilter.Filter = append(tagfilter.Filter, str.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-01 14:15:28 +00:00
|
|
|
f.TagPass = append(f.TagPass, *tagfilter)
|
|
|
|
f.IsActive = true
|
2015-11-24 21:22:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if node, ok := tbl.Fields["tagdrop"]; ok {
|
|
|
|
if subtbl, ok := node.(*ast.Table); ok {
|
|
|
|
for name, val := range subtbl.Fields {
|
|
|
|
if kv, ok := val.(*ast.KeyValue); ok {
|
|
|
|
tagfilter := &TagFilter{Name: name}
|
|
|
|
if ary, ok := kv.Value.(*ast.Array); ok {
|
|
|
|
for _, elem := range ary.Value {
|
|
|
|
if str, ok := elem.(*ast.String); ok {
|
|
|
|
tagfilter.Filter = append(tagfilter.Filter, str.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-01 14:15:28 +00:00
|
|
|
f.TagDrop = append(f.TagDrop, *tagfilter)
|
|
|
|
f.IsActive = true
|
2015-11-24 21:22:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(tbl.Fields, "drop")
|
|
|
|
delete(tbl.Fields, "pass")
|
|
|
|
delete(tbl.Fields, "tagdrop")
|
|
|
|
delete(tbl.Fields, "tagpass")
|
2015-12-01 14:15:28 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildPlugin parses plugin specific items from the ast.Table, builds the filter and returns a
|
|
|
|
// PluginConfig to be inserted into RunningPlugin
|
|
|
|
func buildPlugin(name string, tbl *ast.Table) (*PluginConfig, error) {
|
|
|
|
cp := &PluginConfig{Name: name}
|
|
|
|
if node, ok := tbl.Fields["interval"]; ok {
|
|
|
|
if kv, ok := node.(*ast.KeyValue); ok {
|
|
|
|
if str, ok := kv.Value.(*ast.String); ok {
|
|
|
|
dur, err := time.ParseDuration(str.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cp.Interval = dur
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(tbl.Fields, "interval")
|
|
|
|
cp.Filter = buildFilter(tbl)
|
|
|
|
return cp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildOutput parses output specific items from the ast.Table, builds the filter and returns an
|
|
|
|
// OutputConfig to be inserted into RunningPlugin
|
|
|
|
// Note: error exists in the return for future calls that might require error
|
|
|
|
func buildOutput(name string, tbl *ast.Table) (*OutputConfig, error) {
|
|
|
|
oc := &OutputConfig{
|
|
|
|
Name: name,
|
|
|
|
Filter: buildFilter(tbl),
|
|
|
|
}
|
|
|
|
return oc, nil
|
|
|
|
|
2015-11-24 21:22:11 +00:00
|
|
|
}
|