From ffe4d6bfd691adcab0ce86fb85868c47d633e7b5 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Tue, 11 Aug 2015 14:02:04 -0600 Subject: [PATCH] Followup to issue #77, create configured database name from toml file --- outputs/all/all.go | 5 +++ outputs/influxdb/influxdb.go | 67 ++++++++++++++++++++++++++++++++++++ outputs/registry.go | 18 ++++++++++ 3 files changed, 90 insertions(+) create mode 100644 outputs/all/all.go create mode 100644 outputs/influxdb/influxdb.go create mode 100644 outputs/registry.go diff --git a/outputs/all/all.go b/outputs/all/all.go new file mode 100644 index 0000000000000..2a8018674349c --- /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 0000000000000..1153f064b2fc4 --- /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 0000000000000..ccc40f9b28765 --- /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 +}