2015-08-11 20:02:04 +00:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-09-09 21:56:10 +00:00
|
|
|
"math/rand"
|
2015-08-11 20:02:04 +00:00
|
|
|
"strings"
|
2015-12-15 17:08:13 +00:00
|
|
|
"time"
|
2015-08-11 20:02:04 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-12-04 20:18:13 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2016-01-27 23:15:14 +00:00
|
|
|
|
2016-12-04 20:18:13 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs/influxdb/client"
|
2015-08-11 20:02:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InfluxDB struct {
|
2015-09-09 21:56:10 +00:00
|
|
|
// URL is only for backwards compatability
|
2016-05-24 09:08:34 +00:00
|
|
|
URL string
|
|
|
|
URLs []string `toml:"urls"`
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Database string
|
|
|
|
UserAgent string
|
|
|
|
RetentionPolicy string
|
|
|
|
WriteConsistency string
|
|
|
|
Timeout internal.Duration
|
|
|
|
UDPPayload int `toml:"udp_payload"`
|
2015-08-11 20:02:04 +00:00
|
|
|
|
2016-02-13 00:00:11 +00:00
|
|
|
// Path to CA file
|
|
|
|
SSLCA string `toml:"ssl_ca"`
|
|
|
|
// Path to host cert file
|
|
|
|
SSLCert string `toml:"ssl_cert"`
|
|
|
|
// Path to cert key file
|
|
|
|
SSLKey string `toml:"ssl_key"`
|
|
|
|
// Use SSL but skip chain & host verification
|
|
|
|
InsecureSkipVerify bool
|
|
|
|
|
2016-06-13 14:21:11 +00:00
|
|
|
// Precision is only here for legacy support. It will be ignored.
|
|
|
|
Precision string
|
|
|
|
|
2016-12-04 20:18:13 +00:00
|
|
|
clients []client.Client
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## The full HTTP or UDP endpoint URL for your InfluxDB instance.
|
|
|
|
## Multiple urls can be specified as 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
|
2016-02-29 16:13:00 +00:00
|
|
|
## The target database for metrics (telegraf will create it if not exists).
|
2015-10-15 21:53:29 +00:00
|
|
|
database = "telegraf" # required
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2016-06-16 11:18:08 +00:00
|
|
|
## Retention policy to write to. Empty string writes to the default rp.
|
|
|
|
retention_policy = ""
|
2016-08-10 16:51:21 +00:00
|
|
|
## Write consistency (clusters only), can be: "any", "one", "quorum", "all"
|
2016-05-24 09:08:34 +00:00
|
|
|
write_consistency = "any"
|
|
|
|
|
2016-02-19 21:46:03 +00:00
|
|
|
## Write timeout (for the InfluxDB client), formatted as a string.
|
|
|
|
## If not provided, will default to 5s. 0s means no timeout (not recommended).
|
|
|
|
timeout = "5s"
|
2015-10-15 21:53:29 +00:00
|
|
|
# username = "telegraf"
|
|
|
|
# password = "metricsmetricsmetricsmetrics"
|
2016-02-18 21:26:51 +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"
|
2016-02-18 21:26:51 +00:00
|
|
|
## Set UDP payload size, defaults to InfluxDB UDP Client default (512 bytes)
|
2015-11-10 21:19:32 +00:00
|
|
|
# udp_payload = 512
|
2016-02-13 00:00:11 +00:00
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## Optional SSL Config
|
2016-02-13 00:00:11 +00:00
|
|
|
# ssl_ca = "/etc/telegraf/ca.pem"
|
|
|
|
# ssl_cert = "/etc/telegraf/cert.pem"
|
|
|
|
# ssl_key = "/etc/telegraf/key.pem"
|
2016-02-18 21:26:51 +00:00
|
|
|
## Use SSL but skip chain & host verification
|
2016-02-13 00:00:11 +00:00
|
|
|
# insecure_skip_verify = false
|
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
|
|
|
|
2016-12-04 20:18:13 +00:00
|
|
|
tlsConfig, err := internal.GetTLSConfig(
|
2016-02-13 00:00:11 +00:00
|
|
|
i.SSLCert, i.SSLKey, i.SSLCA, i.InsecureSkipVerify)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-10 21:19:32 +00:00
|
|
|
for _, u := range urls {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(u, "udp"):
|
2016-12-04 20:18:13 +00:00
|
|
|
config := client.UDPConfig{
|
|
|
|
URL: u,
|
2015-11-10 21:19:32 +00:00
|
|
|
PayloadSize: i.UDPPayload,
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
2017-01-11 16:01:32 +00:00
|
|
|
c, err := client.NewUDP(config)
|
2015-11-10 21:19:32 +00:00
|
|
|
if err != nil {
|
2016-12-04 20:18:13 +00:00
|
|
|
return fmt.Errorf("Error creating UDP Client [%s]: %s", u, err)
|
2015-11-10 21:19:32 +00:00
|
|
|
}
|
2016-12-04 20:18:13 +00:00
|
|
|
i.clients = append(i.clients, c)
|
2015-11-10 21:19:32 +00:00
|
|
|
default:
|
|
|
|
// If URL doesn't start with "udp", assume HTTP client
|
2016-12-04 20:18:13 +00:00
|
|
|
config := client.HTTPConfig{
|
|
|
|
URL: u,
|
2015-11-10 21:19:32 +00:00
|
|
|
Timeout: i.Timeout.Duration,
|
2016-12-04 20:18:13 +00:00
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
UserAgent: i.UserAgent,
|
2017-02-13 15:30:30 +00:00
|
|
|
Username: i.Username,
|
|
|
|
Password: i.Password,
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
|
|
|
wp := client.WriteParams{
|
|
|
|
Database: i.Database,
|
|
|
|
RetentionPolicy: i.RetentionPolicy,
|
|
|
|
Consistency: i.WriteConsistency,
|
|
|
|
}
|
|
|
|
c, err := client.NewHTTP(config, wp)
|
2015-11-10 21:19:32 +00:00
|
|
|
if err != nil {
|
2016-12-04 20:18:13 +00:00
|
|
|
return fmt.Errorf("Error creating HTTP Client [%s]: %s", u, err)
|
2015-11-10 21:19:32 +00:00
|
|
|
}
|
2016-12-04 20:18:13 +00:00
|
|
|
i.clients = append(i.clients, c)
|
2015-11-10 21:19:32 +00:00
|
|
|
|
2016-12-04 20:18:13 +00:00
|
|
|
err = c.Query("CREATE DATABASE " + i.Database)
|
2016-04-19 04:23:48 +00:00
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Println("E! Database creation failed: " + err.Error())
|
2016-02-26 16:26:43 +00:00
|
|
|
continue
|
2015-11-10 21:19:32 +00:00
|
|
|
}
|
2015-09-09 21:56:10 +00:00
|
|
|
}
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 17:08:13 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
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 {
|
|
|
|
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.
|
2016-01-27 23:15:14 +00:00
|
|
|
func (i *InfluxDB) Write(metrics []telegraf.Metric) error {
|
2016-12-04 20:18:13 +00:00
|
|
|
bufsize := 0
|
|
|
|
for _, m := range metrics {
|
|
|
|
bufsize += m.Len()
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
2017-01-11 16:01:32 +00:00
|
|
|
r := metric.NewReader(metrics)
|
2015-09-09 21:56:10 +00:00
|
|
|
|
|
|
|
// This will get set to nil if a successful write occurs
|
2016-12-04 20:18:13 +00:00
|
|
|
err := fmt.Errorf("Could not write to any InfluxDB server in cluster")
|
2015-09-09 21:56:10 +00:00
|
|
|
|
2016-12-04 20:18:13 +00:00
|
|
|
p := rand.Perm(len(i.clients))
|
2015-09-09 21:56:10 +00:00
|
|
|
for _, n := range p {
|
2016-12-04 20:18:13 +00:00
|
|
|
if _, e := i.clients[n].WriteStream(r, bufsize); e != nil {
|
|
|
|
// If the database was not found, try to recreate it:
|
2016-04-19 04:23:48 +00:00
|
|
|
if strings.Contains(e.Error(), "database not found") {
|
2016-12-04 20:18:13 +00:00
|
|
|
if errc := i.clients[n].Query("CREATE DATABASE " + i.Database); errc != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Error: Database %s not found and failed to recreate\n",
|
2016-04-19 04:23:48 +00:00
|
|
|
i.Database)
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 00:38:07 +00:00
|
|
|
if strings.Contains(e.Error(), "field type conflict") {
|
|
|
|
log.Printf("E! Field type conflict, dropping conflicted points: %s", e)
|
|
|
|
// setting err to nil, otherwise we will keep retrying and points
|
|
|
|
// w/ conflicting types will get stuck in the buffer forever.
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Log write failure
|
|
|
|
log.Printf("E! InfluxDB Output Error: %s", e)
|
2015-09-09 21:56:10 +00:00
|
|
|
} else {
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
2016-03-14 10:28:01 +00:00
|
|
|
|
2015-09-09 21:56:10 +00:00
|
|
|
return err
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 20:18:13 +00:00
|
|
|
func newInflux() *InfluxDB {
|
|
|
|
return &InfluxDB{
|
|
|
|
Timeout: internal.Duration{Duration: time.Second * 5},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
func init() {
|
2016-12-04 20:18:13 +00:00
|
|
|
outputs.Add("influxdb", func() telegraf.Output { return newInflux() })
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|