2015-08-11 20:02:04 +00:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
2015-09-09 21:56:10 +00:00
|
|
|
"errors"
|
2015-08-11 20:02:04 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-09-09 21:56:10 +00:00
|
|
|
"math/rand"
|
2015-08-11 20:02:04 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
"github.com/influxdb/influxdb/client/v2"
|
2015-10-17 06:10:14 +00:00
|
|
|
"github.com/influxdb/telegraf/duration"
|
2015-08-11 20:02:04 +00:00
|
|
|
"github.com/influxdb/telegraf/outputs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InfluxDB struct {
|
2015-09-09 21:56:10 +00:00
|
|
|
// URL is only for backwards compatability
|
2015-08-11 20:02:04 +00:00
|
|
|
URL string
|
2015-09-09 21:56:10 +00:00
|
|
|
URLs []string `toml:"urls"`
|
2015-08-11 20:02:04 +00:00
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Database string
|
|
|
|
UserAgent string
|
2015-10-16 22:13:32 +00:00
|
|
|
Precision string
|
2015-10-17 06:10:14 +00:00
|
|
|
Timeout duration.Duration
|
2015-08-11 20:02:04 +00:00
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
conns []client.Client
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
var sampleConfig = `
|
2015-10-15 21:53:29 +00:00
|
|
|
# The full HTTP endpoint URL for your InfluxDB instance
|
|
|
|
# Multiple urls can be specified for InfluxDB cluster support.
|
|
|
|
urls = ["http://localhost:8086"] # required
|
|
|
|
# The target database for metrics (telegraf will create it if not exists)
|
|
|
|
database = "telegraf" # required
|
2015-10-16 22:13:32 +00:00
|
|
|
precision = "s"
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2015-10-15 21:53:29 +00:00
|
|
|
# Connection timeout (for the connection with InfluxDB), formatted as a string.
|
|
|
|
# Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
|
|
|
# If not provided, will default to 0 (no timeout)
|
|
|
|
# timeout = "5s"
|
|
|
|
# username = "telegraf"
|
|
|
|
# password = "metricsmetricsmetricsmetrics"
|
|
|
|
# Set the user agent for the POSTs (can be useful for log differentiation)
|
|
|
|
# user_agent = "telegraf"
|
2015-08-25 23:59:12 +00:00
|
|
|
`
|
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
func (i *InfluxDB) Connect() error {
|
2015-09-09 21:56:10 +00:00
|
|
|
var urls []*url.URL
|
|
|
|
for _, URL := range i.URLs {
|
|
|
|
u, err := url.Parse(URL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
urls = append(urls, u)
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 21:56:10 +00:00
|
|
|
// Backward-compatability with single Influx URL config files
|
|
|
|
// This could eventually be removed in favor of specifying the urls as a list
|
|
|
|
if i.URL != "" {
|
|
|
|
u, err := url.Parse(i.URL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
urls = append(urls, u)
|
|
|
|
}
|
2015-08-11 20:02:04 +00:00
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
var conns []client.Client
|
2015-09-09 21:56:10 +00:00
|
|
|
for _, parsed_url := range urls {
|
2015-10-16 22:13:32 +00:00
|
|
|
c := client.NewClient(client.Config{
|
|
|
|
URL: parsed_url,
|
2015-09-09 21:56:10 +00:00
|
|
|
Username: i.Username,
|
|
|
|
Password: i.Password,
|
|
|
|
UserAgent: i.UserAgent,
|
|
|
|
Timeout: i.Timeout.Duration,
|
|
|
|
})
|
|
|
|
conns = append(conns, c)
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 21:56:10 +00:00
|
|
|
// This will get set to nil if a successful connection is made
|
|
|
|
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),
|
|
|
|
})
|
2015-08-11 20:02:04 +00:00
|
|
|
|
2015-09-09 21:56:10 +00:00
|
|
|
if e != nil && !strings.Contains(e.Error(), "database already exists") {
|
|
|
|
log.Println("ERROR: " + e.Error())
|
|
|
|
} else {
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 21:56:10 +00:00
|
|
|
i.conns = conns
|
|
|
|
return err
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 17:04:25 +00:00
|
|
|
func (i *InfluxDB) Close() error {
|
|
|
|
// InfluxDB client does not provide a Close() function
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
func (i *InfluxDB) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *InfluxDB) Description() string {
|
|
|
|
return "Configuration for influxdb server to send metrics to"
|
|
|
|
}
|
|
|
|
|
2015-09-09 21:56:10 +00:00
|
|
|
// Choose a random server in the cluster to write to until a successful write
|
|
|
|
// occurs, logging each unsuccessful. If all servers fail, return error.
|
2015-10-16 22:13:32 +00:00
|
|
|
func (i *InfluxDB) Write(points []*client.Point) error {
|
|
|
|
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
|
|
|
|
Database: i.Database,
|
|
|
|
Precision: i.Precision,
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, point := range points {
|
|
|
|
bp.AddPoint(point)
|
|
|
|
}
|
2015-09-09 21:56:10 +00:00
|
|
|
|
|
|
|
// 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 {
|
2015-10-16 22:13:32 +00:00
|
|
|
if e := i.conns[n].Write(bp); e != nil {
|
2015-09-09 21:56:10 +00:00
|
|
|
log.Println("ERROR: " + e.Error())
|
|
|
|
} else {
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
2015-09-09 21:56:10 +00:00
|
|
|
return err
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
outputs.Add("influxdb", func() outputs.Output {
|
|
|
|
return &InfluxDB{}
|
|
|
|
})
|
|
|
|
}
|