Support InfluxDB clusters

Closes #143
This commit is contained in:
Cameron Sparr 2015-09-09 15:56:10 -06:00
parent a7ed46160a
commit 3c7c8926fb
4 changed files with 78 additions and 27 deletions

View File

@ -1,6 +1,11 @@
## v0.1.9 [unreleased] ## v0.1.9 [unreleased]
### Release Notes
- InfluxDB output config change: `url` is now `urls`, and is a list. Config files
will still be backwards compatible if only `url` is specified.
### Features ### Features
- [#143](https://github.com/influxdb/telegraf/issues/143): InfluxDB clustering support
### Bugfixes ### Bugfixes
- [#170](https://github.com/influxdb/telegraf/issues/170): Systemd support - [#170](https://github.com/influxdb/telegraf/issues/170): Systemd support

View File

@ -84,6 +84,9 @@ func NewAgent(config *Config) (*Agent, error) {
// Connect connects to all configured outputs // Connect connects to all configured outputs
func (a *Agent) Connect() error { func (a *Agent) Connect() error {
for _, o := range a.outputs { for _, o := range a.outputs {
if a.Debug {
log.Printf("Attempting connection to output: %s\n", o.name)
}
err := o.output.Connect() err := o.output.Connect()
if err != nil { if err != nil {
return err return err

View File

@ -37,7 +37,7 @@
[outputs] [outputs]
[outputs.influxdb] [outputs.influxdb]
# The full HTTP endpoint URL for your InfluxDB instance # The full HTTP endpoint URL for your InfluxDB instance
url = "http://localhost:8086" # required. urls = ["http://localhost:8086"] # required.
# The target database for metrics. This database must already exist # The target database for metrics. This database must already exist
database = "telegraf" # required. database = "telegraf" # required.

View File

@ -1,8 +1,10 @@
package influxdb package influxdb
import ( import (
"errors"
"fmt" "fmt"
"log" "log"
"math/rand"
"net/url" "net/url"
"strings" "strings"
@ -12,19 +14,23 @@ import (
) )
type InfluxDB struct { type InfluxDB struct {
// URL is only for backwards compatability
URL string URL string
URLs []string `toml:"urls"`
Username string Username string
Password string Password string
Database string Database string
UserAgent string UserAgent string
Timeout t.Duration Timeout t.Duration
conn *client.Client conns []*client.Client
} }
var sampleConfig = ` var sampleConfig = `
# The full HTTP endpoint URL for your InfluxDB instance # The full HTTP endpoint URL for your InfluxDB instance
url = "http://localhost:8086" # required. # Multiple urls can be specified for InfluxDB cluster support. Server to
# write to will be randomly chosen each interval.
urls = ["http://localhost:8086"] # required.
# The target database for metrics. This database must already exist # The target database for metrics. This database must already exist
database = "telegraf" # required. database = "telegraf" # required.
@ -42,33 +48,58 @@ var sampleConfig = `
` `
func (i *InfluxDB) Connect() error { func (i *InfluxDB) Connect() error {
u, err := url.Parse(i.URL) var urls []*url.URL
if err != nil { for _, URL := range i.URLs {
return err u, err := url.Parse(URL)
if err != nil {
return err
}
urls = append(urls, u)
} }
c, err := client.NewClient(client.Config{ // Backward-compatability with single Influx URL config files
URL: *u, // This could eventually be removed in favor of specifying the urls as a list
Username: i.Username, if i.URL != "" {
Password: i.Password, u, err := url.Parse(i.URL)
UserAgent: i.UserAgent, if err != nil {
Timeout: i.Timeout.Duration, return err
}) }
urls = append(urls, u)
if err != nil {
return err
} }
_, err = c.Query(client.Query{ var conns []*client.Client
Command: fmt.Sprintf("CREATE DATABASE %s", i.Database), for _, parsed_url := range urls {
}) c, err := client.NewClient(client.Config{
URL: *parsed_url,
if err != nil && !strings.Contains(err.Error(), "database already exists") { Username: i.Username,
log.Fatal(err) Password: i.Password,
UserAgent: i.UserAgent,
Timeout: i.Timeout.Duration,
})
if err != nil {
return err
}
conns = append(conns, c)
} }
i.conn = c // This will get set to nil if a successful connection is made
return nil err := errors.New("Could not create database on any server")
for _, conn := range conns {
_, e := conn.Query(client.Query{
Command: fmt.Sprintf("CREATE DATABASE %s", i.Database),
})
if e != nil && !strings.Contains(e.Error(), "database already exists") {
log.Println("ERROR: " + e.Error())
} else {
err = nil
break
}
}
i.conns = conns
return err
} }
func (i *InfluxDB) Close() error { func (i *InfluxDB) Close() error {
@ -84,12 +115,24 @@ func (i *InfluxDB) Description() string {
return "Configuration for influxdb server to send metrics to" return "Configuration for influxdb server to send metrics to"
} }
// Choose a random server in the cluster to write to until a successful write
// occurs, logging each unsuccessful. If all servers fail, return error.
func (i *InfluxDB) Write(bp client.BatchPoints) error { func (i *InfluxDB) Write(bp client.BatchPoints) error {
bp.Database = i.Database bp.Database = i.Database
if _, err := i.conn.Write(bp); err != nil {
return err // This will get set to nil if a successful write occurs
err := errors.New("Could not write to any InfluxDB server in cluster")
p := rand.Perm(len(i.conns))
for _, n := range p {
if _, e := i.conns[n].Write(bp); e != nil {
log.Println("ERROR: " + e.Error())
} else {
err = nil
break
}
} }
return nil return err
} }
func init() { func init() {