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
|
|
|
)
|
|
|
|
|
2017-05-25 20:25:52 +00:00
|
|
|
var (
|
|
|
|
// Quote Ident replacer.
|
|
|
|
qiReplacer = strings.NewReplacer("\n", `\n`, `\`, `\\`, `"`, `\"`)
|
|
|
|
)
|
|
|
|
|
2017-05-12 21:42:18 +00:00
|
|
|
// InfluxDB struct is the primary data structure for the plugin
|
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
|
2017-08-28 23:08:50 +00:00
|
|
|
UDPPayload int `toml:"udp_payload"`
|
|
|
|
HTTPProxy string `toml:"http_proxy"`
|
|
|
|
HTTPHeaders map[string]string `toml:"http_headers"`
|
|
|
|
ContentEncoding string `toml:"content_encoding"`
|
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
|
|
|
|
|
2017-06-05 19:44:29 +00:00
|
|
|
clients []client.Client
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
var sampleConfig = `
|
2017-03-24 23:01:35 +00:00
|
|
|
## The HTTP or UDP URL for your InfluxDB instance. Each item should be
|
|
|
|
## of the form:
|
|
|
|
## scheme "://" host [ ":" port]
|
|
|
|
##
|
2016-02-18 21:26:51 +00:00
|
|
|
## 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
|
|
|
|
2017-04-28 20:46:23 +00:00
|
|
|
## Name of existing retention policy to write to. Empty string writes to
|
|
|
|
## the default retention policy.
|
2016-06-16 11:18:08 +00:00
|
|
|
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
|
2017-06-16 19:05:08 +00:00
|
|
|
|
|
|
|
## HTTP Proxy Config
|
|
|
|
# http_proxy = "http://corporate.proxy:3128"
|
2017-08-22 23:52:26 +00:00
|
|
|
|
2017-08-28 23:08:50 +00:00
|
|
|
## Optional HTTP headers
|
|
|
|
# http_headers = {"X-Special-Header" = "Special-Value"}
|
|
|
|
|
2017-08-14 21:50:15 +00:00
|
|
|
## Compress each HTTP request payload using GZIP.
|
|
|
|
# content_encoding = "gzip"
|
2015-08-25 23:59:12 +00:00
|
|
|
`
|
|
|
|
|
2017-05-12 21:42:18 +00:00
|
|
|
// Connect initiates the primary connection to the range of provided URLs
|
2015-08-11 20:02:04 +00:00
|
|
|
func (i *InfluxDB) Connect() error {
|
2015-11-10 21:19:32 +00:00
|
|
|
var urls []string
|
2017-05-12 21:42:18 +00:00
|
|
|
urls = append(urls, i.URLs...)
|
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{
|
2017-08-14 21:50:15 +00:00
|
|
|
URL: u,
|
|
|
|
Timeout: i.Timeout.Duration,
|
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
UserAgent: i.UserAgent,
|
|
|
|
Username: i.Username,
|
|
|
|
Password: i.Password,
|
|
|
|
HTTPProxy: i.HTTPProxy,
|
2017-08-28 23:08:50 +00:00
|
|
|
HTTPHeaders: client.HTTPHeaders{},
|
2017-08-14 21:50:15 +00:00
|
|
|
ContentEncoding: i.ContentEncoding,
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
2017-08-28 23:08:50 +00:00
|
|
|
for header, value := range i.HTTPHeaders {
|
|
|
|
config.HTTPHeaders[header] = value
|
|
|
|
}
|
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
|
|
|
|
2017-05-25 20:25:52 +00:00
|
|
|
err = c.Query(fmt.Sprintf(`CREATE DATABASE "%s"`, qiReplacer.Replace(i.Database)))
|
2016-04-19 04:23:48 +00:00
|
|
|
if err != nil {
|
2017-04-28 22:58:46 +00:00
|
|
|
if !strings.Contains(err.Error(), "Status Code [403]") {
|
|
|
|
log.Println("I! 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
|
|
|
}
|
|
|
|
|
2017-05-12 21:42:18 +00:00
|
|
|
// Close will terminate the session to the backend, returning error if an issue arises
|
2015-08-12 17:04:25 +00:00
|
|
|
func (i *InfluxDB) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-12 21:42:18 +00:00
|
|
|
// SampleConfig returns the formatted sample configuration for the plugin
|
2015-08-25 23:59:12 +00:00
|
|
|
func (i *InfluxDB) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2017-05-12 21:42:18 +00:00
|
|
|
// Description returns the human-readable function definition of the plugin
|
2015-08-25 23:59:12 +00:00
|
|
|
func (i *InfluxDB) Description() string {
|
|
|
|
return "Configuration for influxdb server to send metrics to"
|
|
|
|
}
|
|
|
|
|
2017-05-12 21:42:18 +00:00
|
|
|
// Write will choose a random server in the cluster to write to until a successful write
|
2015-09-09 21:56:10 +00:00
|
|
|
// 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 {
|
2017-05-31 00:38:32 +00:00
|
|
|
|
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-05-31 00:38: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") {
|
2017-05-25 20:25:52 +00:00
|
|
|
errc := i.clients[n].Query(fmt.Sprintf(`CREATE DATABASE "%s"`, qiReplacer.Replace(i.Database)))
|
|
|
|
if 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-08-22 23:52:26 +00:00
|
|
|
|
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
|
|
|
|
}
|
2017-08-22 23:52:26 +00:00
|
|
|
|
|
|
|
if strings.Contains(e.Error(), "points beyond retention policy") {
|
|
|
|
log.Printf("W! Points beyond retention policy: %s", e)
|
|
|
|
// This error is indicates the point is older than the
|
|
|
|
// retention policy permits, and is probably not a cause for
|
|
|
|
// concern. Retrying will not help unless the retention
|
|
|
|
// policy is modified.
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(e.Error(), "unable to parse") {
|
|
|
|
log.Printf("E! Parse error; dropping points: %s", e)
|
|
|
|
// This error indicates a bug in Telegraf or InfluxDB parsing
|
|
|
|
// of line protocol. Retries will not be successful.
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(e.Error(), "hinted handoff queue not empty") {
|
|
|
|
// This is an informational message
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-01-24 00:38:07 +00:00
|
|
|
// 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
|
|
|
}
|