2015-05-22 23:45:14 +00:00
|
|
|
package telegraf
|
2015-04-01 16:34:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-10-19 07:09:36 +00:00
|
|
|
"path/filepath"
|
2015-10-17 04:47:13 +00:00
|
|
|
"reflect"
|
2015-04-01 16:34:32 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
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-04-01 16:34:32 +00:00
|
|
|
"github.com/naoina/toml"
|
|
|
|
"github.com/naoina/toml/ast"
|
|
|
|
)
|
|
|
|
|
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-10-17 04:47:13 +00:00
|
|
|
// This lives outside the agent because mergeStruct doesn't need to handle maps normally.
|
2015-10-22 18:22:54 +00:00
|
|
|
// We just copy the elements manually in ApplyAgent.
|
2015-08-11 20:02:04 +00:00
|
|
|
Tags map[string]string
|
|
|
|
|
2015-10-17 04:47:13 +00:00
|
|
|
agent *Agent
|
|
|
|
plugins map[string]plugins.Plugin
|
|
|
|
pluginConfigurations map[string]*ConfiguredPlugin
|
|
|
|
outputs map[string]outputs.Output
|
|
|
|
|
|
|
|
agentFieldsSet []string
|
|
|
|
pluginFieldsSet map[string][]string
|
|
|
|
pluginConfigurationFieldsSet map[string][]string
|
|
|
|
outputFieldsSet map[string][]string
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-10-17 04:47:13 +00:00
|
|
|
// Plugins returns the configured plugins as a map of name -> plugins.Plugin
|
|
|
|
func (c *Config) Plugins() map[string]plugins.Plugin {
|
2015-04-01 16:34:32 +00:00
|
|
|
return c.plugins
|
|
|
|
}
|
2015-08-07 20:31:25 +00:00
|
|
|
|
2015-10-17 04:47:13 +00:00
|
|
|
// Outputs returns the configured outputs as a map of name -> outputs.Output
|
|
|
|
func (c *Config) Outputs() map[string]outputs.Output {
|
2015-08-07 20:31:25 +00:00
|
|
|
return c.outputs
|
|
|
|
}
|
2015-04-01 16:34:32 +00:00
|
|
|
|
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-08-04 14:58:32 +00:00
|
|
|
// ConfiguredPlugin containing a name, interval, and drop/pass prefix lists
|
2015-08-10 19:41:20 +00:00
|
|
|
// Also lists the tags to filter
|
2015-05-20 05:19:32 +00:00
|
|
|
type ConfiguredPlugin struct {
|
|
|
|
Name string
|
|
|
|
|
|
|
|
Drop []string
|
|
|
|
Pass []string
|
|
|
|
|
2015-08-10 19:41:20 +00:00
|
|
|
TagDrop []TagFilter
|
|
|
|
TagPass []TagFilter
|
|
|
|
|
2015-05-20 05:19:32 +00:00
|
|
|
Interval time.Duration
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// ShouldPass returns true if the metric should pass, false if should drop
|
2015-08-10 19:41:20 +00:00
|
|
|
func (cp *ConfiguredPlugin) ShouldPass(measurement string, tags map[string]string) bool {
|
2015-05-20 05:19:32 +00:00
|
|
|
if cp.Pass != nil {
|
|
|
|
for _, pat := range cp.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
|
|
|
|
}
|
|
|
|
|
|
|
|
if cp.Drop != nil {
|
|
|
|
for _, pat := range cp.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-08-10 19:41:20 +00:00
|
|
|
if cp.TagPass != nil {
|
|
|
|
for _, pat := range cp.TagPass {
|
|
|
|
if tagval, ok := tags[pat.Name]; ok {
|
|
|
|
for _, filter := range pat.Filter {
|
|
|
|
if filter == tagval {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if cp.TagDrop != nil {
|
|
|
|
for _, pat := range cp.TagDrop {
|
|
|
|
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-10-17 04:47:13 +00:00
|
|
|
// ApplyOutput loads the Output struct built from the config into the given Output struct.
|
|
|
|
// Overrides only values in the given struct that were set in the config.
|
2015-08-07 20:31:25 +00:00
|
|
|
func (c *Config) ApplyOutput(name string, v interface{}) error {
|
|
|
|
if c.outputs[name] != nil {
|
2015-10-17 04:47:13 +00:00
|
|
|
return mergeStruct(v, c.outputs[name], c.outputFieldsSet[name])
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-08-07 20:31:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-17 04:47:13 +00:00
|
|
|
// ApplyAgent loads the Agent struct built from the config into the given Agent struct.
|
|
|
|
// Overrides only values in the given struct that were set in the config.
|
2015-09-02 16:30:44 +00:00
|
|
|
func (c *Config) ApplyAgent(a *Agent) error {
|
2015-05-20 05:19:32 +00:00
|
|
|
if c.agent != nil {
|
2015-10-22 18:22:54 +00:00
|
|
|
for key, value := range c.Tags {
|
|
|
|
a.Tags[key] = value
|
|
|
|
}
|
2015-10-17 04:47:13 +00:00
|
|
|
return mergeStruct(a, c.agent, c.agentFieldsSet)
|
|
|
|
}
|
2015-04-01 16:34:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-17 04:47:13 +00:00
|
|
|
// ApplyPlugin loads the Plugin struct built from the config into the given Plugin struct.
|
|
|
|
// Overrides only values in the given struct that were set in the config.
|
|
|
|
// Additionally return a ConfiguredPlugin, which is always generated from the config.
|
2015-05-20 05:19:32 +00:00
|
|
|
func (c *Config) ApplyPlugin(name string, v interface{}) (*ConfiguredPlugin, error) {
|
2015-10-17 04:47:13 +00:00
|
|
|
if c.plugins[name] != nil {
|
|
|
|
err := mergeStruct(v, c.plugins[name], c.pluginFieldsSet[name])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-08-10 19:41:20 +00:00
|
|
|
}
|
2015-10-17 04:47:13 +00:00
|
|
|
return c.pluginConfigurations[name], nil
|
2015-05-20 05:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-10-17 04:47:13 +00:00
|
|
|
return nil, nil
|
2015-05-20 05:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-10-17 04:47:13 +00:00
|
|
|
// Couldn't figure out how to get this to work with the declared function.
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// PluginsDeclared returns the name of all plugins declared in the config.
|
2015-05-18 21:10:12 +00:00
|
|
|
func (c *Config) PluginsDeclared() []string {
|
2015-10-17 04:47:13 +00:00
|
|
|
var names []string
|
|
|
|
for name := range c.plugins {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
return names
|
2015-08-07 20:31:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OutputsDeclared returns the name of all outputs declared in the config.
|
|
|
|
func (c *Config) OutputsDeclared() []string {
|
2015-08-11 20:02:04 +00:00
|
|
|
var names []string
|
2015-10-17 04:47:13 +00:00
|
|
|
for name := range c.outputs {
|
2015-08-11 20:02:04 +00:00
|
|
|
names = append(names, name)
|
2015-05-18 21:10:12 +00:00
|
|
|
}
|
2015-08-11 20:02:04 +00:00
|
|
|
sort.Strings(names)
|
|
|
|
return names
|
2015-05-18 21:10:12 +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
|
|
|
|
|
|
|
type hasConfig interface {
|
|
|
|
BasicConfig() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type hasDescr interface {
|
|
|
|
Description() string
|
|
|
|
}
|
|
|
|
|
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-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-05-18 22:10:11 +00:00
|
|
|
|
2015-08-26 15:21:39 +00:00
|
|
|
fmt.Printf("\n# %s\n[outputs.%s]", output.Description(), oname)
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
config := output.SampleConfig()
|
|
|
|
if config == "" {
|
2015-10-15 21:53:29 +00:00
|
|
|
fmt.Printf("\n # no configuration\n")
|
2015-08-25 23:59:12 +00:00
|
|
|
} else {
|
|
|
|
fmt.Printf(config)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
printConfig(pname, plugin)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print Service Plugins
|
|
|
|
fmt.Printf(servicePluginHeader)
|
|
|
|
for name, plugin := range servPlugins {
|
|
|
|
printConfig(name, plugin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 20:24:51 +00:00
|
|
|
type printer interface {
|
|
|
|
Description() string
|
|
|
|
SampleConfig() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func printConfig(name string, p printer) {
|
|
|
|
fmt.Printf("\n# %s\n[%s]", p.Description(), name)
|
|
|
|
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-09-24 18:06:11 +00:00
|
|
|
printConfig(name, creator())
|
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 {
|
|
|
|
printConfig(name, creator())
|
|
|
|
} else {
|
|
|
|
return errors.New(fmt.Sprintf("Output %s not found", name))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-17 04:47:13 +00:00
|
|
|
// Used for fuzzy matching struct field names in FieldByNameFunc calls below
|
|
|
|
func fieldMatch(field string) func(string) bool {
|
|
|
|
return func(name string) bool {
|
|
|
|
r := strings.NewReplacer("_", "")
|
|
|
|
return strings.ToLower(name) == strings.ToLower(r.Replace(field))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A very limited merge. Merges the fields named in the fields parameter, replacing most values, but appending to arrays.
|
|
|
|
func mergeStruct(base, overlay interface{}, fields []string) error {
|
|
|
|
baseValue := reflect.ValueOf(base).Elem()
|
|
|
|
overlayValue := reflect.ValueOf(overlay).Elem()
|
|
|
|
if baseValue.Kind() != reflect.Struct {
|
|
|
|
return fmt.Errorf("Tried to merge something that wasn't a struct: type %v was %v", baseValue.Type(), baseValue.Kind())
|
|
|
|
}
|
|
|
|
if baseValue.Type() != overlayValue.Type() {
|
|
|
|
return fmt.Errorf("Tried to merge two different types: %v and %v", baseValue.Type(), overlayValue.Type())
|
|
|
|
}
|
|
|
|
for _, field := range fields {
|
|
|
|
overlayFieldValue := overlayValue.FieldByNameFunc(fieldMatch(field))
|
|
|
|
if !overlayFieldValue.IsValid() {
|
|
|
|
return fmt.Errorf("could not find field in %v matching %v", overlayValue.Type(), field)
|
|
|
|
}
|
|
|
|
if overlayFieldValue.Kind() == reflect.Slice {
|
|
|
|
baseFieldValue := baseValue.FieldByNameFunc(fieldMatch(field))
|
|
|
|
baseFieldValue.Set(reflect.AppendSlice(baseFieldValue, overlayFieldValue))
|
|
|
|
} else {
|
|
|
|
baseValue.FieldByNameFunc(fieldMatch(field)).Set(overlayFieldValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
if name[len(name)-5:] != ".conf" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
subConfig, err := LoadConfig(filepath.Join(path, name))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if subConfig.agent != nil {
|
|
|
|
err = mergeStruct(c.agent, subConfig.agent, subConfig.agentFieldsSet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, field := range subConfig.agentFieldsSet {
|
|
|
|
if !sliceContains(field, c.agentFieldsSet) {
|
|
|
|
c.agentFieldsSet = append(c.agentFieldsSet, field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for pluginName, plugin := range subConfig.plugins {
|
|
|
|
if _, ok := c.plugins[pluginName]; !ok {
|
|
|
|
c.plugins[pluginName] = plugin
|
|
|
|
c.pluginFieldsSet[pluginName] = subConfig.pluginFieldsSet[pluginName]
|
|
|
|
c.pluginConfigurations[pluginName] = subConfig.pluginConfigurations[pluginName]
|
|
|
|
c.pluginConfigurationFieldsSet[pluginName] = subConfig.pluginConfigurationFieldsSet[pluginName]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = mergeStruct(c.plugins[pluginName], plugin, subConfig.pluginFieldsSet[pluginName])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, field := range subConfig.pluginFieldsSet[pluginName] {
|
|
|
|
if !sliceContains(field, c.pluginFieldsSet[pluginName]) {
|
|
|
|
c.pluginFieldsSet[pluginName] = append(c.pluginFieldsSet[pluginName], field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = mergeStruct(c.pluginConfigurations[pluginName], subConfig.pluginConfigurations[pluginName], subConfig.pluginConfigurationFieldsSet[pluginName])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, field := range subConfig.pluginConfigurationFieldsSet[pluginName] {
|
|
|
|
if !sliceContains(field, c.pluginConfigurationFieldsSet[pluginName]) {
|
|
|
|
c.pluginConfigurationFieldsSet[pluginName] = append(c.pluginConfigurationFieldsSet[pluginName], field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for outputName, output := range subConfig.outputs {
|
|
|
|
if _, ok := c.outputs[outputName]; !ok {
|
|
|
|
c.outputs[outputName] = output
|
|
|
|
c.outputFieldsSet[outputName] = subConfig.outputFieldsSet[outputName]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = mergeStruct(c.outputs[outputName], output, subConfig.outputFieldsSet[outputName])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, field := range subConfig.outputFieldsSet[outputName] {
|
|
|
|
if !sliceContains(field, c.outputFieldsSet[outputName]) {
|
|
|
|
c.outputFieldsSet[outputName] = append(c.outputFieldsSet[outputName], field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-17 04:47:13 +00:00
|
|
|
// hazmat area. Keeping the ast parsing here.
|
|
|
|
|
|
|
|
// LoadConfig loads the given config file and returns a *Config pointer
|
|
|
|
func LoadConfig(path string) (*Config, error) {
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tbl, err := toml.Parse(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Config{
|
|
|
|
Tags: make(map[string]string),
|
|
|
|
plugins: make(map[string]plugins.Plugin),
|
|
|
|
pluginConfigurations: make(map[string]*ConfiguredPlugin),
|
|
|
|
outputs: make(map[string]outputs.Output),
|
|
|
|
pluginFieldsSet: make(map[string][]string),
|
|
|
|
pluginConfigurationFieldsSet: make(map[string][]string),
|
|
|
|
outputFieldsSet: make(map[string][]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, val := range tbl.Fields {
|
|
|
|
subtbl, ok := val.(*ast.Table)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("invalid configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "agent":
|
|
|
|
err := c.parseAgent(subtbl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case "tags":
|
|
|
|
if err = toml.UnmarshalTable(subtbl, c.Tags); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case "outputs":
|
|
|
|
for outputName, outputVal := range subtbl.Fields {
|
|
|
|
outputSubtbl, ok := outputVal.(*ast.Table)
|
|
|
|
if !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = c.parseOutput(outputName, outputSubtbl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = c.parsePlugin(name, subtbl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needs to have the field names, for merging later.
|
|
|
|
func extractFieldNames(ast *ast.Table) []string {
|
|
|
|
// A reasonable capacity?
|
|
|
|
var names []string
|
|
|
|
for name := range ast.Fields {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the agent config out of the given *ast.Table.
|
|
|
|
func (c *Config) parseAgent(agentAst *ast.Table) error {
|
|
|
|
c.agentFieldsSet = extractFieldNames(agentAst)
|
|
|
|
agent := &Agent{}
|
|
|
|
err := toml.UnmarshalTable(agentAst, agent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.agent = agent
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse an output config out of the given *ast.Table.
|
|
|
|
func (c *Config) parseOutput(name string, outputAst *ast.Table) error {
|
|
|
|
c.outputFieldsSet[name] = extractFieldNames(outputAst)
|
|
|
|
creator, ok := outputs.Outputs[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Undefined but requested output: %s", name)
|
|
|
|
}
|
|
|
|
output := creator()
|
|
|
|
err := toml.UnmarshalTable(outputAst, output)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.outputs[name] = output
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a plugin config, plus plugin meta-config, out of the given *ast.Table.
|
|
|
|
func (c *Config) parsePlugin(name string, pluginAst *ast.Table) error {
|
|
|
|
creator, ok := plugins.Plugins[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Undefined but requested plugin: %s", name)
|
|
|
|
}
|
|
|
|
plugin := creator()
|
|
|
|
cp := &ConfiguredPlugin{Name: name}
|
|
|
|
cpFields := make([]string, 0, 5)
|
|
|
|
|
|
|
|
if node, ok := pluginAst.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 {
|
|
|
|
cp.Pass = append(cp.Pass, str.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cpFields = append(cpFields, "pass")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if node, ok := pluginAst.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 {
|
|
|
|
cp.Drop = append(cp.Drop, str.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cpFields = append(cpFields, "drop")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if node, ok := pluginAst.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 err
|
|
|
|
}
|
|
|
|
|
|
|
|
cp.Interval = dur
|
|
|
|
cpFields = append(cpFields, "interval")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if node, ok := pluginAst.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cp.TagPass = append(cp.TagPass, *tagfilter)
|
|
|
|
cpFields = append(cpFields, "tagpass")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if node, ok := pluginAst.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cp.TagDrop = append(cp.TagDrop, *tagfilter)
|
|
|
|
cpFields = append(cpFields, "tagdrop")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(pluginAst.Fields, "drop")
|
|
|
|
delete(pluginAst.Fields, "pass")
|
|
|
|
delete(pluginAst.Fields, "interval")
|
|
|
|
delete(pluginAst.Fields, "tagdrop")
|
|
|
|
delete(pluginAst.Fields, "tagpass")
|
|
|
|
c.pluginFieldsSet[name] = extractFieldNames(pluginAst)
|
|
|
|
c.pluginConfigurationFieldsSet[name] = cpFields
|
|
|
|
err := toml.UnmarshalTable(pluginAst, plugin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.plugins[name] = plugin
|
|
|
|
c.pluginConfigurations[name] = cp
|
|
|
|
return nil
|
|
|
|
}
|