From 53969ae054f57c77ad5656978e630857e1be29ee Mon Sep 17 00:00:00 2001 From: JP Date: Fri, 7 Aug 2015 15:31:25 -0500 Subject: [PATCH 1/4] move tags to influxdb struct, update all sample configs --- .gitignore | 1 + agent.go | 85 ++++++++++++++++++++++++++-------------- cmd/telegraf/telegraf.go | 11 +++--- config.go | 59 ++++++++++++++++++++-------- etc/config.sample.toml | 3 +- testdata/influx.toml | 7 +++- 6 files changed, 112 insertions(+), 54 deletions(-) diff --git a/.gitignore b/.gitignore index a127b89f7..a471ffe03 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ pkg/ tivan .vagrant +telegraf diff --git a/agent.go b/agent.go index ef687bd30..6540601ea 100644 --- a/agent.go +++ b/agent.go @@ -10,10 +10,15 @@ import ( "sync" "time" - "github.com/influxdb/influxdb/client" + "github.com/influxdb/telegraf/outputs" "github.com/influxdb/telegraf/plugins" ) +type runningOutput struct { + name string + output outputs.Output +} + type runningPlugin struct { name string plugin plugins.Plugin @@ -32,9 +37,8 @@ type Agent struct { Config *Config + outputs []*runningOutput plugins []*runningPlugin - - conn *client.Client } // NewAgent returns an Agent struct based off the given Config @@ -66,27 +70,37 @@ func NewAgent(config *Config) (*Agent, error) { // Connect connects to the agent's config URL func (a *Agent) Connect() error { - config := a.Config - - u, err := url.Parse(config.URL) - if err != nil { - return err + for _, o := range a.outputs { + err := o.output.Connect(a.Hostname) + if err != nil { + return err + } } + return nil +} - c, err := client.NewClient(client.Config{ - URL: *u, - Username: config.Username, - Password: config.Password, - UserAgent: config.UserAgent, - Timeout: config.Timeout.Duration, - }) +func (a *Agent) LoadOutputs() ([]string, error) { + var names []string - if err != nil { - return err + for _, name := range a.Config.OutputsDeclared() { + creator, ok := outputs.Outputs[name] + if !ok { + return nil, fmt.Errorf("Undefined but requested output: %s", name) + } + + output := creator() + + err := a.Config.ApplyOutput(name, output) + if err != nil { + return nil, err + } + + a.outputs = append(a.outputs, &runningOutput{name, output}) + names = append(names, name) } _, err = c.Query(client.Query{ - Command: fmt.Sprintf("CREATE DATABASE %s", config.Database), + Command: fmt.Sprintf("CREATE DATABASE telegraf"), }) if err != nil && !strings.Contains(err.Error(), "database already exists") { @@ -95,7 +109,7 @@ func (a *Agent) Connect() error { a.conn = c - return nil + return names, nil } // LoadPlugins loads the agent's plugins @@ -114,6 +128,8 @@ func (a *Agent) LoadPlugins(pluginsFilter string) ([]string, error) { return nil, fmt.Errorf("Undefined but requested plugin: %s", name) } + plugin := creator() + isPluginEnabled := false if len(filters) > 0 { for _, runeValue := range filters { @@ -205,8 +221,7 @@ func (a *Agent) crank() error { acc.Time = time.Now() acc.Database = a.Config.Database - _, err := a.conn.Write(acc.BatchPoints) - return err + return a.flush(&acc) } func (a *Agent) crankSeparate(shutdown chan struct{}, plugin *runningPlugin) error { @@ -228,7 +243,10 @@ func (a *Agent) crankSeparate(shutdown chan struct{}, plugin *runningPlugin) err acc.Time = time.Now() acc.Database = a.Config.Database - a.conn.Write(acc.BatchPoints) + err = a.flush(&acc) + if err != nil { + return err + } select { case <-shutdown: @@ -239,6 +257,22 @@ func (a *Agent) crankSeparate(shutdown chan struct{}, plugin *runningPlugin) err } } +func (a *Agent) flush(bp *BatchPoints) error { + var wg sync.WaitGroup + var outerr error + for _, o := range a.outputs { + wg.Add(1) + go func(ro *runningOutput) { + defer wg.Done() + outerr = ro.output.Write(bp.BatchPoints) + }(o) + } + + wg.Wait() + + return outerr +} + // TestAllPlugins verifies that we can 'Gather' from all plugins with the // default configuration func (a *Agent) TestAllPlugins() error { @@ -297,13 +331,6 @@ func (a *Agent) Test() error { // Run runs the agent daemon, gathering every Interval func (a *Agent) Run(shutdown chan struct{}) error { - if a.conn == nil { - err := a.Connect() - if err != nil { - return err - } - } - var wg sync.WaitGroup for _, plugin := range a.plugins { diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index 1b6ad088a..d6fdd965b 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -59,6 +59,11 @@ func main() { ag.Debug = true } + outputs, err := ag.LoadOutputs() + if err != nil { + log.Fatal(err) + } + plugins, err := ag.LoadPlugins(*fPLuginsFilter) if err != nil { log.Fatal(err) @@ -99,6 +104,7 @@ func main() { }() log.Printf("Starting Telegraf (version %s)\n", Version) + log.Printf("Loaded outputs: %s", strings.Join(outputs, " ")) log.Printf("Loaded plugins: %s", strings.Join(plugins, " ")) if ag.Debug { log.Printf("Debug: enabled") @@ -106,11 +112,6 @@ func main() { ag.Interval, ag.Debug, ag.Hostname) } - if config.URL != "" { - log.Printf("Sending metrics to: %s", config.URL) - log.Printf("Tags enabled: %v", config.ListTags()) - } - if *fPidfile != "" { f, err := os.Create(*fPidfile) if err != nil { diff --git a/config.go b/config.go index 48df238ac..b943f5442 100644 --- a/config.go +++ b/config.go @@ -34,22 +34,24 @@ func (d *Duration) UnmarshalTOML(b []byte) error { // will be logging to, as well as all the plugins that the user has // specified type Config struct { - URL string - Username string - Password string - Database string - UserAgent string - Timeout Duration - Tags map[string]string - agent *ast.Table plugins map[string]*ast.Table + outputs map[string]*ast.Table } // Plugins returns the configured plugins as a map of name -> plugin toml func (c *Config) Plugins() map[string]*ast.Table { return c.plugins } +type TagFilter struct { + Name string + Filter []string +} + +// Outputs returns the configured outputs as a map of name -> output toml +func (c *Config) Outputs() map[string]*ast.Table { + return c.outputs +} // The name of a tag, and the values on which to filter type TagFilter struct { @@ -64,6 +66,9 @@ type ConfiguredPlugin struct { Drop []string Pass []string + TagDrop []TagFilter + + TagPass []TagFilter TagDrop []TagFilter TagPass []TagFilter @@ -122,6 +127,13 @@ func (cp *ConfiguredPlugin) ShouldPass(measurement string, tags map[string]strin return true } +// ApplyOutput loads the toml config into the given interface +func (c *Config) ApplyOutput(name string, v interface{}) error { + if c.outputs[name] != nil { + return toml.UnmarshalTable(c.outputs[name], v) + } +} + // ApplyAgent loads the toml config into the given interface func (c *Config) ApplyAgent(v interface{}) error { if c.agent != nil { @@ -225,6 +237,15 @@ func (c *Config) ApplyPlugin(name string, v interface{}) (*ConfiguredPlugin, err // PluginsDeclared returns the name of all plugins declared in the config. func (c *Config) PluginsDeclared() []string { + return declared(c.plugins) +} + +// OutputsDeclared returns the name of all outputs declared in the config. +func (c *Config) OutputsDeclared() []string { + return declared(c.outputs) +} + +func declared(endpoints map[string]*ast.Table) []string { var plugins []string for name := range c.plugins { @@ -257,6 +278,7 @@ func LoadConfig(path string) (*Config, error) { c := &Config{ plugins: make(map[string]*ast.Table), + outputs: make(map[string]*ast.Table), } for name, val := range tbl.Fields { @@ -266,13 +288,16 @@ func LoadConfig(path string) (*Config, error) { } switch name { - case "influxdb": - err := toml.UnmarshalTable(subtbl, c) - if err != nil { - return nil, err - } case "agent": c.agent = subtbl + case "outputs": + for outputName, outputVal := range subtbl.Fields { + outputSubtbl, ok := outputVal.(*ast.Table) + if !ok { + return nil, errInvalidConfig + } + c.outputs[outputName] = outputSubtbl + } default: c.plugins[name] = subtbl } @@ -327,8 +352,11 @@ var header = `# Telegraf configuration # NOTE: The configuration has a few required parameters. They are marked # with 'required'. Be sure to edit those to make this configuration work. +# OUTPUTS +[outputs] + # Configuration for influxdb server to send metrics to -[influxdb] +[outputs.influxdb] # The full HTTP endpoint URL for your InfluxDB instance url = "http://localhost:8086" # required. @@ -345,12 +373,11 @@ database = "telegraf" # required. # Set the user agent for the POSTs (can be useful for log differentiation) # user_agent = "telegraf" -# tags = { "dc": "us-east-1" } # Tags can also be specified via a normal map, but only one form at a time: # [influxdb.tags] -# dc = "us-east-1" +# tags = { "dc" = "us-east-1" } # Configuration for telegraf itself # [agent] diff --git a/etc/config.sample.toml b/etc/config.sample.toml index 38cfeba68..9faf5b871 100644 --- a/etc/config.sample.toml +++ b/etc/config.sample.toml @@ -35,12 +35,11 @@ database = "telegraf" # required. # Set the user agent for the POSTs (can be useful for log differentiation) # user_agent = "telegraf" -# tags = { "dc": "us-east-1" } # Tags can also be specified via a normal map, but only one form at a time: # [influxdb.tags] -# dc = "us-east-1" +# tags = { "dc" = "us-east-1" } # Configuration for telegraf itself # [agent] diff --git a/testdata/influx.toml b/testdata/influx.toml index fc5d3c493..80d03fa70 100644 --- a/testdata/influx.toml +++ b/testdata/influx.toml @@ -3,12 +3,15 @@ interval = "5s" http = ":11213" debug = true -[influxdb] +[outputs] +[outputs.influxdb] url = "http://localhost:8086" username = "root" password = "root" database = "telegraf" -tags = { dc = "us-phx-1" } + +[tags.influxdb] +tags = { "dc" = "us-phx-1" } [redis] address = ":6379" From 08042089f9d0a8760d786eae904bfc6d2fb2a770 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Tue, 11 Aug 2015 14:02:04 -0600 Subject: [PATCH 2/4] Followup to issue #77, create configured database name from toml file --- agent.go | 62 +++++++++++++-------------------- config.go | 26 ++++++-------- outputs/all/all.go | 5 +++ outputs/influxdb/influxdb.go | 67 ++++++++++++++++++++++++++++++++++++ outputs/registry.go | 18 ++++++++++ testdata/influx.toml | 4 +-- testdata/telegraf-agent.toml | 10 +++--- 7 files changed, 132 insertions(+), 60 deletions(-) create mode 100644 outputs/all/all.go create mode 100644 outputs/influxdb/influxdb.go create mode 100644 outputs/registry.go diff --git a/agent.go b/agent.go index 6540601ea..f86e27920 100644 --- a/agent.go +++ b/agent.go @@ -3,7 +3,6 @@ package telegraf import ( "fmt" "log" - "net/url" "os" "sort" "strings" @@ -71,7 +70,7 @@ func NewAgent(config *Config) (*Agent, error) { // Connect connects to the agent's config URL func (a *Agent) Connect() error { for _, o := range a.outputs { - err := o.output.Connect(a.Hostname) + err := o.output.Connect() if err != nil { return err } @@ -79,6 +78,7 @@ func (a *Agent) Connect() error { return nil } +// LoadOutputs loads the agent's outputs func (a *Agent) LoadOutputs() ([]string, error) { var names []string @@ -99,15 +99,7 @@ func (a *Agent) LoadOutputs() ([]string, error) { names = append(names, name) } - _, err = c.Query(client.Query{ - Command: fmt.Sprintf("CREATE DATABASE telegraf"), - }) - - if err != nil && !strings.Contains(err.Error(), "database already exists") { - log.Fatal(err) - } - - a.conn = c + sort.Strings(names) return names, nil } @@ -128,8 +120,6 @@ func (a *Agent) LoadPlugins(pluginsFilter string) ([]string, error) { return nil, fmt.Errorf("Undefined but requested plugin: %s", name) } - plugin := creator() - isPluginEnabled := false if len(filters) > 0 { for _, runeValue := range filters { @@ -190,60 +180,56 @@ func (a *Agent) crankParallel() error { close(points) - var acc BatchPoints - acc.Tags = a.Config.Tags - acc.Time = time.Now() - acc.Database = a.Config.Database + var bp BatchPoints + bp.Time = time.Now() + bp.Tags = a.Config.Tags for sub := range points { - acc.Points = append(acc.Points, sub.Points...) + bp.Points = append(bp.Points, sub.Points...) } - _, err := a.conn.Write(acc.BatchPoints) - return err + return a.flush(&bp) } func (a *Agent) crank() error { - var acc BatchPoints + var bp BatchPoints - acc.Debug = a.Debug + bp.Debug = a.Debug for _, plugin := range a.plugins { - acc.Prefix = plugin.name + "_" - acc.Config = plugin.config - err := plugin.plugin.Gather(&acc) + bp.Prefix = plugin.name + "_" + bp.Config = plugin.config + err := plugin.plugin.Gather(&bp) if err != nil { return err } } - acc.Tags = a.Config.Tags - acc.Time = time.Now() - acc.Database = a.Config.Database + bp.Time = time.Now() + bp.Tags = a.Config.Tags - return a.flush(&acc) + return a.flush(&bp) } func (a *Agent) crankSeparate(shutdown chan struct{}, plugin *runningPlugin) error { ticker := time.NewTicker(plugin.config.Interval) for { - var acc BatchPoints + var bp BatchPoints - acc.Debug = a.Debug + bp.Debug = a.Debug - acc.Prefix = plugin.name + "_" - acc.Config = plugin.config - err := plugin.plugin.Gather(&acc) + bp.Prefix = plugin.name + "_" + bp.Config = plugin.config + err := plugin.plugin.Gather(&bp) if err != nil { return err } - acc.Tags = a.Config.Tags - acc.Time = time.Now() - acc.Database = a.Config.Database + bp.Tags = a.Config.Tags + bp.Time = time.Now() - err = a.flush(&acc) + err = a.flush(&bp) if err != nil { return err } diff --git a/config.go b/config.go index b943f5442..db49dfa2f 100644 --- a/config.go +++ b/config.go @@ -34,6 +34,8 @@ func (d *Duration) UnmarshalTOML(b []byte) error { // will be logging to, as well as all the plugins that the user has // specified type Config struct { + Tags map[string]string + agent *ast.Table plugins map[string]*ast.Table outputs map[string]*ast.Table @@ -43,10 +45,6 @@ type Config struct { func (c *Config) Plugins() map[string]*ast.Table { return c.plugins } -type TagFilter struct { - Name string - Filter []string -} // Outputs returns the configured outputs as a map of name -> output toml func (c *Config) Outputs() map[string]*ast.Table { @@ -66,9 +64,6 @@ type ConfiguredPlugin struct { Drop []string Pass []string - TagDrop []TagFilter - - TagPass []TagFilter TagDrop []TagFilter TagPass []TagFilter @@ -131,7 +126,8 @@ func (cp *ConfiguredPlugin) ShouldPass(measurement string, tags map[string]strin func (c *Config) ApplyOutput(name string, v interface{}) error { if c.outputs[name] != nil { return toml.UnmarshalTable(c.outputs[name], v) - } + } + return nil } // ApplyAgent loads the toml config into the given interface @@ -246,15 +242,15 @@ func (c *Config) OutputsDeclared() []string { } func declared(endpoints map[string]*ast.Table) []string { - var plugins []string + var names []string - for name := range c.plugins { - plugins = append(plugins, name) + for name := range endpoints { + names = append(names, name) } - sort.Strings(plugins) + sort.Strings(names) - return plugins + return names } // DefaultConfig returns an empty default configuration @@ -376,8 +372,8 @@ database = "telegraf" # required. # Tags can also be specified via a normal map, but only one form at a time: -# [influxdb.tags] -# tags = { "dc" = "us-east-1" } +# [tags] +# dc = "us-east-1" } # Configuration for telegraf itself # [agent] diff --git a/outputs/all/all.go b/outputs/all/all.go new file mode 100644 index 000000000..2a8018674 --- /dev/null +++ b/outputs/all/all.go @@ -0,0 +1,5 @@ +package all + +import ( + _ "github.com/influxdb/telegraf/outputs/influxdb" +) diff --git a/outputs/influxdb/influxdb.go b/outputs/influxdb/influxdb.go new file mode 100644 index 000000000..1153f064b --- /dev/null +++ b/outputs/influxdb/influxdb.go @@ -0,0 +1,67 @@ +package influxdb + +import ( + "fmt" + "log" + "net/url" + "strings" + + "github.com/influxdb/influxdb/client" + t "github.com/influxdb/telegraf" + "github.com/influxdb/telegraf/outputs" +) + +type InfluxDB struct { + URL string + Username string + Password string + Database string + UserAgent string + Timeout t.Duration + + conn *client.Client +} + +func (i *InfluxDB) Connect() error { + u, err := url.Parse(i.URL) + if err != nil { + return err + } + + c, err := client.NewClient(client.Config{ + URL: *u, + Username: i.Username, + Password: i.Password, + UserAgent: i.UserAgent, + Timeout: i.Timeout.Duration, + }) + + if err != nil { + return err + } + + _, err = c.Query(client.Query{ + Command: fmt.Sprintf("CREATE DATABASE telegraf"), + }) + + if err != nil && !strings.Contains(err.Error(), "database already exists") { + log.Fatal(err) + } + + i.conn = c + return nil +} + +func (i *InfluxDB) Write(bp client.BatchPoints) error { + bp.Database = i.Database + if _, err := i.conn.Write(bp); err != nil { + return err + } + return nil +} + +func init() { + outputs.Add("influxdb", func() outputs.Output { + return &InfluxDB{} + }) +} diff --git a/outputs/registry.go b/outputs/registry.go new file mode 100644 index 000000000..ccc40f9b2 --- /dev/null +++ b/outputs/registry.go @@ -0,0 +1,18 @@ +package outputs + +import ( + "github.com/influxdb/influxdb/client" +) + +type Output interface { + Connect() error + Write(client.BatchPoints) error +} + +type Creator func() Output + +var Outputs = map[string]Creator{} + +func Add(name string, creator Creator) { + Outputs[name] = creator +} diff --git a/testdata/influx.toml b/testdata/influx.toml index 80d03fa70..492528cae 100644 --- a/testdata/influx.toml +++ b/testdata/influx.toml @@ -10,8 +10,8 @@ username = "root" password = "root" database = "telegraf" -[tags.influxdb] -tags = { "dc" = "us-phx-1" } +[tags] +dc = "us-phx-1" } [redis] address = ":6379" diff --git a/testdata/telegraf-agent.toml b/testdata/telegraf-agent.toml index b58995594..fd0221a47 100644 --- a/testdata/telegraf-agent.toml +++ b/testdata/telegraf-agent.toml @@ -23,7 +23,8 @@ # with 'required'. Be sure to edit those to make this configuration work. # Configuration for influxdb server to send metrics to -[influxdb] +[outputs] +[outputs.influxdb] # The full HTTP endpoint URL for your InfluxDB instance url = "http://localhost:8086" # required. @@ -40,11 +41,10 @@ database = "telegraf" # required. # Set the user agent for the POSTs (can be useful for log differentiation) # user_agent = "telegraf" -# tags = { "dc": "us-east-1" } # Tags can also be specified via a normal map, but only one form at a time: -# [influxdb.tags] +# [tags] # dc = "us-east-1" # Configuration for telegraf itself @@ -204,11 +204,11 @@ urls = ["localhost/status"] # postgres://[pqgotest[:password]]@localhost?sslmode=[disable|verify-ca|verify-full] # or a simple string: # host=localhost user=pqotest password=... sslmode=... -# +# # All connection parameters are optional. By default, the host is localhost # and the user is the currently running user. For localhost, we default # to sslmode=disable as well. -# +# address = "sslmode=disable" From 32124a791338faabb7fd590bf13c45540a1e4667 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Wed, 12 Aug 2015 11:04:25 -0600 Subject: [PATCH 3/4] Adding a Close() function to the Output interface and to the agent --- agent.go | 11 ++++++++++- outputs/influxdb/influxdb.go | 5 +++++ outputs/registry.go | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/agent.go b/agent.go index f86e27920..1c90bf1a4 100644 --- a/agent.go +++ b/agent.go @@ -67,7 +67,7 @@ func NewAgent(config *Config) (*Agent, error) { return agent, nil } -// Connect connects to the agent's config URL +// Connect connects to all configured outputs func (a *Agent) Connect() error { for _, o := range a.outputs { err := o.output.Connect() @@ -78,6 +78,15 @@ func (a *Agent) Connect() error { return nil } +// Close closes the connection to all configured outputs +func (a *Agent) Close() error { + var err error + for _, o := range a.outputs { + err = o.output.Close() + } + return err +} + // LoadOutputs loads the agent's outputs func (a *Agent) LoadOutputs() ([]string, error) { var names []string diff --git a/outputs/influxdb/influxdb.go b/outputs/influxdb/influxdb.go index 1153f064b..96505a4d7 100644 --- a/outputs/influxdb/influxdb.go +++ b/outputs/influxdb/influxdb.go @@ -52,6 +52,11 @@ func (i *InfluxDB) Connect() error { return nil } +func (i *InfluxDB) Close() error { + // InfluxDB client does not provide a Close() function + return nil +} + func (i *InfluxDB) Write(bp client.BatchPoints) error { bp.Database = i.Database if _, err := i.conn.Write(bp); err != nil { diff --git a/outputs/registry.go b/outputs/registry.go index ccc40f9b2..a2f22f73b 100644 --- a/outputs/registry.go +++ b/outputs/registry.go @@ -6,6 +6,7 @@ import ( type Output interface { Connect() error + Close() error Write(client.BatchPoints) error } From 5cc6f88ade0ed90ab4217f689d825255c116c6ef Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Wed, 12 Aug 2015 11:08:45 -0600 Subject: [PATCH 4/4] Update changelog with PR #107, thanks @jipperinbham --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc2265259..e94713337 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - [#98](https://github.com/influxdb/telegraf/pull/98): LeoFS plugin. Thanks @mocchira! - [#103](https://github.com/influxdb/telegraf/pull/103): Filter by metric tags. Thanks @srfraser! - [#106](https://github.com/influxdb/telegraf/pull/106): Options to filter plugins on startup. Thanks @zepouet! +- [#107](https://github.com/influxdb/telegraf/pull/107): Multiple outputs beyong influxdb. Thanks @jipperinbham! ### Bugfixes - [#85](https://github.com/influxdb/telegraf/pull/85): Fix GetLocalHost testutil function for mac users