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-11-10 21:40:39 +00:00
|
|
|
"github.com/influxdb/telegraf/internal"
|
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-11-29 13:29:26 +00:00
|
|
|
URL string
|
|
|
|
URLs []string `toml:"urls"`
|
|
|
|
DBRoutingTag string `toml:"database_routing_tag"`
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Database string
|
|
|
|
UserAgent string
|
|
|
|
Precision string
|
|
|
|
Timeout internal.Duration
|
|
|
|
UDPPayload int `toml:"udp_payload"`
|
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-11-16 20:12:45 +00:00
|
|
|
# The full HTTP or UDP endpoint URL for your InfluxDB instance.
|
|
|
|
# Multiple urls can be specified but it is assumed that they are part of the same
|
|
|
|
# cluster, this means that only ONE of the urls will be written to each interval.
|
2015-11-10 21:19:32 +00:00
|
|
|
# urls = ["udp://localhost:8089"] # UDP endpoint example
|
2015-10-15 21:53:29 +00:00
|
|
|
urls = ["http://localhost:8086"] # required
|
|
|
|
# The target database for metrics (telegraf will create it if not exists)
|
|
|
|
database = "telegraf" # required
|
2015-10-21 20:05:27 +00:00
|
|
|
# Precision of writes, valid values are n, u, ms, s, m, and h
|
|
|
|
# note: using second precision greatly helps InfluxDB compression
|
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.
|
|
|
|
# If not provided, will default to 0 (no timeout)
|
|
|
|
# timeout = "5s"
|
|
|
|
# username = "telegraf"
|
|
|
|
# password = "metricsmetricsmetricsmetrics"
|
2015-11-10 21:19:32 +00:00
|
|
|
# Set the user agent for HTTP POSTs (can be useful for log differentiation)
|
2015-10-15 21:53:29 +00:00
|
|
|
# user_agent = "telegraf"
|
2015-11-10 21:19:32 +00:00
|
|
|
# Set UDP payload size, defaults to InfluxDB UDP Client default (512 bytes)
|
|
|
|
# udp_payload = 512
|
2015-11-29 13:29:26 +00:00
|
|
|
|
|
|
|
# Route metrics to an InfluxDB database based on value of a tag
|
|
|
|
# note: if this tag has many unique values, write performance may suffer
|
|
|
|
# database_routing_tag = "mytag"
|
2015-08-25 23:59:12 +00:00
|
|
|
`
|
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
func (i *InfluxDB) Connect() error {
|
2015-11-10 21:19:32 +00:00
|
|
|
var urls []string
|
|
|
|
for _, u := range i.URLs {
|
2015-09-09 21:56:10 +00:00
|
|
|
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 != "" {
|
2015-11-10 21:19:32 +00:00
|
|
|
urls = append(urls, i.URL)
|
2015-09-09 21:56:10 +00:00
|
|
|
}
|
2015-08-11 20:02:04 +00:00
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
var conns []client.Client
|
2015-11-10 21:19:32 +00:00
|
|
|
for _, u := range urls {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(u, "udp"):
|
2015-11-12 21:54:43 +00:00
|
|
|
parsed_url, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-10 21:19:32 +00:00
|
|
|
if i.UDPPayload == 0 {
|
|
|
|
i.UDPPayload = client.UDPPayloadSize
|
|
|
|
}
|
|
|
|
c, err := client.NewUDPClient(client.UDPConfig{
|
|
|
|
Addr: parsed_url.Host,
|
|
|
|
PayloadSize: i.UDPPayload,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conns = append(conns, c)
|
|
|
|
default:
|
|
|
|
// If URL doesn't start with "udp", assume HTTP client
|
|
|
|
c, err := client.NewHTTPClient(client.HTTPConfig{
|
2015-11-12 21:54:43 +00:00
|
|
|
Addr: u,
|
2015-11-10 21:19:32 +00:00
|
|
|
Username: i.Username,
|
|
|
|
Password: i.Password,
|
|
|
|
UserAgent: i.UserAgent,
|
|
|
|
Timeout: i.Timeout.Duration,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Database if it doesn't exist
|
|
|
|
_, e := c.Query(client.Query{
|
2015-11-16 23:55:49 +00:00
|
|
|
Command: fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", i.Database),
|
2015-11-10 21:19:32 +00:00
|
|
|
})
|
2015-08-11 20:02:04 +00:00
|
|
|
|
2015-11-16 23:55:49 +00:00
|
|
|
if e != nil {
|
2015-11-10 21:19:32 +00:00
|
|
|
log.Println("Database creation failed: " + e.Error())
|
|
|
|
}
|
2015-08-11 20:02:04 +00:00
|
|
|
|
2015-11-10 21:19:32 +00:00
|
|
|
conns = append(conns, c)
|
2015-09-09 21:56:10 +00:00
|
|
|
}
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 21:56:10 +00:00
|
|
|
i.conns = conns
|
2015-10-22 17:14:10 +00:00
|
|
|
return nil
|
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 {
|
2015-11-29 13:29:26 +00:00
|
|
|
var bps map[string]client.BatchPoints = make(map[string]client.BatchPoints)
|
|
|
|
|
|
|
|
// Default database
|
|
|
|
bps["___default"], _ = client.NewBatchPoints(client.BatchPointsConfig{
|
2015-10-16 22:13:32 +00:00
|
|
|
Database: i.Database,
|
|
|
|
Precision: i.Precision,
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, point := range points {
|
2015-11-29 13:29:26 +00:00
|
|
|
if i.DBRoutingTag != "" {
|
|
|
|
if h, ok := point.Tags()[i.DBRoutingTag]; ok {
|
|
|
|
// Create a new batch point config for this tag value
|
|
|
|
if _, ok := bps[h]; !ok {
|
|
|
|
bps[h], _ = client.NewBatchPoints(client.BatchPointsConfig{
|
|
|
|
Database: h,
|
|
|
|
Precision: i.Precision,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
bps[h].AddPoint(point)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing found in overrides, lets push this into the default bucket
|
|
|
|
bps["___default"].AddPoint(point)
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
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")
|
|
|
|
|
2015-11-29 13:29:26 +00:00
|
|
|
for k, bp := range bps {
|
|
|
|
p := rand.Perm(len(i.conns))
|
|
|
|
for _, n := range p {
|
|
|
|
if e := i.conns[n].Write(bp); e != nil {
|
|
|
|
log.Println("ERROR: " + e.Error())
|
|
|
|
|
|
|
|
// Stop trying immediately if the error is for a missing database
|
|
|
|
// and we are trying a database routing tag
|
|
|
|
if k != "___default" && strings.HasPrefix(e.Error(), "database not found") {
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
2015-09-09 21:56:10 +00:00
|
|
|
}
|
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{}
|
|
|
|
})
|
|
|
|
}
|