Skip to content

Commit

Permalink
Followup to issue #77, create configured database name from toml file
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Aug 12, 2015
1 parent 53969ae commit b0b8bc8
Show file tree
Hide file tree
Showing 4 changed files with 803 additions and 0 deletions.
2 changes: 2 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func (a *Agent) LoadOutputs() ([]string, error) {
return nil, fmt.Errorf("Undefined but requested output: %s", name)
}

_, err = c.Query(client.Query{
Command: fmt.Sprintf("CREATE DATABASE %s", config.Database),
output := creator()

err := a.Config.ApplyOutput(name, output)
Expand Down
5 changes: 5 additions & 0 deletions outputs/all/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package all

import (
_ "github.com/influxdb/telegraf/outputs/influxdb"
)
67 changes: 67 additions & 0 deletions outputs/influxdb/influxdb.go
Original file line number Diff line number Diff line change
@@ -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{}
})
}
Loading

0 comments on commit b0b8bc8

Please sign in to comment.