2015-08-11 20:02:04 +00:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
2018-03-28 00:30:51 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2015-08-11 20:02:04 +00:00
|
|
|
"fmt"
|
2015-09-09 21:56:10 +00:00
|
|
|
"math/rand"
|
2018-03-28 00:30:51 +00:00
|
|
|
"net/url"
|
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"
|
2018-05-04 23:33:23 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/tls"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2018-03-28 00:30:51 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/influx"
|
2015-08-11 20:02:04 +00:00
|
|
|
)
|
|
|
|
|
2017-05-25 20:25:52 +00:00
|
|
|
var (
|
2018-03-28 00:30:51 +00:00
|
|
|
defaultURL = "http://localhost:8086"
|
|
|
|
|
|
|
|
ErrMissingURL = errors.New("missing URL")
|
2017-05-25 20:25:52 +00:00
|
|
|
)
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
type Client interface {
|
|
|
|
Write(context.Context, []telegraf.Metric) error
|
2019-02-27 18:54:02 +00:00
|
|
|
CreateDatabase(ctx context.Context, database string) error
|
2018-03-28 00:30:51 +00:00
|
|
|
Database() string
|
2019-02-27 18:54:02 +00:00
|
|
|
URL() string
|
2019-05-31 23:55:31 +00:00
|
|
|
Close()
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-03-28 00:59:57 +00:00
|
|
|
URL string // url deprecated in 0.1.9; use urls
|
|
|
|
URLs []string `toml:"urls"`
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Database string
|
2019-02-27 18:54:02 +00:00
|
|
|
DatabaseTag string `toml:"database_tag"`
|
2019-07-30 21:16:51 +00:00
|
|
|
ExcludeDatabaseTag bool `toml:"exclude_database_tag"`
|
2018-03-28 00:59:57 +00:00
|
|
|
UserAgent string
|
|
|
|
RetentionPolicy string
|
|
|
|
WriteConsistency string
|
|
|
|
Timeout internal.Duration
|
2018-10-19 18:17:18 +00:00
|
|
|
UDPPayload internal.Size `toml:"udp_payload"`
|
2018-03-28 00:59:57 +00:00
|
|
|
HTTPProxy string `toml:"http_proxy"`
|
|
|
|
HTTPHeaders map[string]string `toml:"http_headers"`
|
|
|
|
ContentEncoding string `toml:"content_encoding"`
|
|
|
|
SkipDatabaseCreation bool `toml:"skip_database_creation"`
|
2018-03-29 20:31:43 +00:00
|
|
|
InfluxUintSupport bool `toml:"influx_uint_support"`
|
2018-05-04 23:33:23 +00:00
|
|
|
tls.ClientConfig
|
2016-02-13 00:00:11 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
Precision string // precision deprecated in 1.0; value is ignored
|
2016-06-13 14:21:11 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
clients []Client
|
|
|
|
|
|
|
|
CreateHTTPClientF func(config *HTTPConfig) (Client, error)
|
|
|
|
CreateUDPClientF func(config *UDPConfig) (Client, error)
|
|
|
|
|
|
|
|
serializer *influx.Serializer
|
2019-08-21 23:49:07 +00:00
|
|
|
Log telegraf.Logger
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
var sampleConfig = `
|
2017-09-14 00:27:01 +00:00
|
|
|
## The full HTTP or UDP URL for your InfluxDB instance.
|
2017-03-24 23:01:35 +00:00
|
|
|
##
|
2018-03-28 00:30:51 +00:00
|
|
|
## Multiple URLs can be specified for a single cluster, only ONE of the
|
|
|
|
## urls will be written to each interval.
|
2018-03-28 01:36:08 +00:00
|
|
|
# urls = ["unix:///var/run/influxdb.sock"]
|
2018-03-28 00:30:51 +00:00
|
|
|
# urls = ["udp://127.0.0.1:8089"]
|
|
|
|
# urls = ["http://127.0.0.1:8086"]
|
|
|
|
|
|
|
|
## The target database for metrics; will be created as needed.
|
2018-11-13 00:06:23 +00:00
|
|
|
## For UDP url endpoint database needs to be configured on server side.
|
2018-03-28 00:30:51 +00:00
|
|
|
# database = "telegraf"
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2019-02-27 18:54:02 +00:00
|
|
|
## The value of this tag will be used to determine the database. If this
|
|
|
|
## tag is not set the 'database' option is used as the default.
|
|
|
|
# database_tag = ""
|
|
|
|
|
2019-07-30 21:16:51 +00:00
|
|
|
## If true, the database tag will not be added to the metric.
|
|
|
|
# exclude_database_tag = false
|
|
|
|
|
2018-03-28 00:59:57 +00:00
|
|
|
## If true, no CREATE DATABASE queries will be sent. Set to true when using
|
|
|
|
## Telegraf with a user without permissions to create databases or when the
|
|
|
|
## database already exists.
|
|
|
|
# skip_database_creation = false
|
|
|
|
|
2017-04-28 20:46:23 +00:00
|
|
|
## Name of existing retention policy to write to. Empty string writes to
|
2018-04-25 20:46:16 +00:00
|
|
|
## the default retention policy. Only takes effect when using HTTP.
|
2018-03-28 00:30:51 +00:00
|
|
|
# retention_policy = ""
|
|
|
|
|
2018-04-25 20:46:16 +00:00
|
|
|
## Write consistency (clusters only), can be: "any", "one", "quorum", "all".
|
|
|
|
## Only takes effect when using HTTP.
|
2018-03-28 00:30:51 +00:00
|
|
|
# write_consistency = "any"
|
2016-05-24 09:08:34 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
## Timeout for HTTP messages.
|
|
|
|
# timeout = "5s"
|
|
|
|
|
|
|
|
## HTTP Basic Auth
|
2015-10-15 21:53:29 +00:00
|
|
|
# username = "telegraf"
|
|
|
|
# password = "metricsmetricsmetricsmetrics"
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
## HTTP User-Agent
|
2015-10-15 21:53:29 +00:00
|
|
|
# user_agent = "telegraf"
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
## UDP payload size is the maximum packet size to send.
|
2018-10-19 18:17:18 +00:00
|
|
|
# udp_payload = "512B"
|
2016-02-13 00:00:11 +00:00
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
## Optional TLS Config for use on HTTP connections.
|
|
|
|
# tls_ca = "/etc/telegraf/ca.pem"
|
|
|
|
# tls_cert = "/etc/telegraf/cert.pem"
|
|
|
|
# tls_key = "/etc/telegraf/key.pem"
|
|
|
|
## Use TLS 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
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
## HTTP Proxy override, if unset values the standard proxy environment
|
|
|
|
## variables are consulted to determine which proxy, if any, should be used.
|
2017-06-16 19:05:08 +00:00
|
|
|
# http_proxy = "http://corporate.proxy:3128"
|
2017-08-22 23:52:26 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
## Additional HTTP headers
|
2017-08-28 23:08:50 +00:00
|
|
|
# http_headers = {"X-Special-Header" = "Special-Value"}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
## HTTP Content-Encoding for write request body, can be set to "gzip" to
|
|
|
|
## compress body or "identity" to apply no encoding.
|
|
|
|
# content_encoding = "identity"
|
2018-03-29 20:31:43 +00:00
|
|
|
|
|
|
|
## When true, Telegraf will output unsigned integers as unsigned values,
|
|
|
|
## i.e.: "42u". You will need a version of InfluxDB supporting unsigned
|
|
|
|
## integer values. Enabling this option will result in field type errors if
|
|
|
|
## existing data has been written.
|
|
|
|
# influx_uint_support = false
|
2015-08-25 23:59:12 +00:00
|
|
|
`
|
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
func (i *InfluxDB) Connect() error {
|
2018-03-28 00:30:51 +00:00
|
|
|
ctx := context.Background()
|
2015-08-11 20:02:04 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
urls := make([]string, 0, len(i.URLs))
|
|
|
|
urls = append(urls, i.URLs...)
|
2015-09-09 21:56:10 +00:00
|
|
|
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
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
if len(urls) == 0 {
|
|
|
|
urls = append(urls, defaultURL)
|
2016-02-13 00:00:11 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
i.serializer = influx.NewSerializer()
|
2018-03-29 20:31:43 +00:00
|
|
|
if i.InfluxUintSupport {
|
|
|
|
i.serializer.SetFieldTypeSupport(influx.UintSupport)
|
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
|
2015-11-10 21:19:32 +00:00
|
|
|
for _, u := range urls {
|
2018-08-07 18:07:46 +00:00
|
|
|
parts, err := url.Parse(u)
|
2018-03-28 00:30:51 +00:00
|
|
|
if err != nil {
|
2018-08-07 18:07:46 +00:00
|
|
|
return fmt.Errorf("error parsing url [%q]: %v", u, err)
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var proxy *url.URL
|
|
|
|
if len(i.HTTPProxy) > 0 {
|
|
|
|
proxy, err = url.Parse(i.HTTPProxy)
|
2015-11-10 21:19:32 +00:00
|
|
|
if err != nil {
|
2018-08-07 18:07:46 +00:00
|
|
|
return fmt.Errorf("error parsing proxy_url [%s]: %v", i.HTTPProxy, err)
|
2015-11-10 21:19:32 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 18:07:46 +00:00
|
|
|
switch parts.Scheme {
|
2018-03-28 00:30:51 +00:00
|
|
|
case "udp", "udp4", "udp6":
|
2018-08-07 18:07:46 +00:00
|
|
|
c, err := i.udpClient(parts)
|
2015-11-10 21:19:32 +00:00
|
|
|
if err != nil {
|
2018-03-28 00:30:51 +00:00
|
|
|
return err
|
2015-11-10 21:19:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
i.clients = append(i.clients, c)
|
2018-03-28 01:36:08 +00:00
|
|
|
case "http", "https", "unix":
|
2018-08-07 18:07:46 +00:00
|
|
|
c, err := i.httpClient(ctx, parts, proxy)
|
2016-04-19 04:23:48 +00:00
|
|
|
if err != nil {
|
2018-03-28 00:30:51 +00:00
|
|
|
return err
|
2015-11-10 21:19:32 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
i.clients = append(i.clients, c)
|
|
|
|
default:
|
2018-08-07 18:07:46 +00:00
|
|
|
return fmt.Errorf("unsupported scheme [%q]: %q", u, parts.Scheme)
|
2015-09-09 21:56:10 +00:00
|
|
|
}
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-05-31 23:55:31 +00:00
|
|
|
for _, client := range i.clients {
|
|
|
|
client.Close()
|
|
|
|
}
|
2015-08-12 17:04:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (i *InfluxDB) Description() string {
|
|
|
|
return "Configuration for sending metrics to InfluxDB"
|
2015-08-25 23:59:12 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (i *InfluxDB) SampleConfig() string {
|
|
|
|
return sampleConfig
|
2015-08-25 23:59:12 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
// Write sends metrics to one of the configured servers, logging each
|
|
|
|
// unsuccessful. If all servers fail, return an error.
|
2016-01-27 23:15:14 +00:00
|
|
|
func (i *InfluxDB) Write(metrics []telegraf.Metric) error {
|
2018-03-28 00:30:51 +00:00
|
|
|
ctx := context.Background()
|
2015-09-09 21:56:10 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
var err error
|
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 {
|
2018-03-28 00:30:51 +00:00
|
|
|
client := i.clients[n]
|
|
|
|
err = client.Write(ctx, metrics)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-22 23:52:26 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
switch apiError := err.(type) {
|
2019-02-27 18:54:02 +00:00
|
|
|
case *DatabaseNotFoundError:
|
2018-03-28 00:59:57 +00:00
|
|
|
if !i.SkipDatabaseCreation {
|
2019-02-27 18:54:02 +00:00
|
|
|
err := client.CreateDatabase(ctx, apiError.Database)
|
|
|
|
if err != nil {
|
2019-09-23 22:39:50 +00:00
|
|
|
i.Log.Errorf("When writing to [%s]: database %q not found and failed to recreate",
|
2019-02-27 18:54:02 +00:00
|
|
|
client.URL(), apiError.Database)
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
2017-01-24 00:38:07 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
2017-08-22 23:52:26 +00:00
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
i.Log.Errorf("When writing to [%s]: %v", client.URL(), err)
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
2017-08-22 23:52:26 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
return errors.New("could not write any address")
|
|
|
|
}
|
2017-08-22 23:52:26 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (i *InfluxDB) udpClient(url *url.URL) (Client, error) {
|
|
|
|
config := &UDPConfig{
|
|
|
|
URL: url,
|
2018-10-19 18:17:18 +00:00
|
|
|
MaxPayloadSize: int(i.UDPPayload.Size),
|
2018-03-28 00:30:51 +00:00
|
|
|
Serializer: i.serializer,
|
2019-08-22 01:02:51 +00:00
|
|
|
Log: i.Log,
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
2017-08-22 23:52:26 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
c, err := i.CreateUDPClientF(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating UDP client [%s]: %v", url, err)
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
2016-03-14 10:28:01 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
return c, nil
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (i *InfluxDB) httpClient(ctx context.Context, url *url.URL, proxy *url.URL) (Client, error) {
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsConfig, err := i.ClientConfig.TLSConfig()
|
2018-03-28 00:30:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &HTTPConfig{
|
2019-02-27 18:54:02 +00:00
|
|
|
URL: url,
|
|
|
|
Timeout: i.Timeout.Duration,
|
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
UserAgent: i.UserAgent,
|
|
|
|
Username: i.Username,
|
|
|
|
Password: i.Password,
|
|
|
|
Proxy: proxy,
|
|
|
|
ContentEncoding: i.ContentEncoding,
|
|
|
|
Headers: i.HTTPHeaders,
|
|
|
|
Database: i.Database,
|
|
|
|
DatabaseTag: i.DatabaseTag,
|
2019-07-30 21:16:51 +00:00
|
|
|
ExcludeDatabaseTag: i.ExcludeDatabaseTag,
|
2019-02-27 18:54:02 +00:00
|
|
|
SkipDatabaseCreation: i.SkipDatabaseCreation,
|
|
|
|
RetentionPolicy: i.RetentionPolicy,
|
|
|
|
Consistency: i.WriteConsistency,
|
|
|
|
Serializer: i.serializer,
|
2019-08-22 01:02:51 +00:00
|
|
|
Log: i.Log,
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := i.CreateHTTPClientF(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating HTTP client [%s]: %v", url, err)
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
|
2018-03-28 00:59:57 +00:00
|
|
|
if !i.SkipDatabaseCreation {
|
2019-02-27 18:54:02 +00:00
|
|
|
err = c.CreateDatabase(ctx, c.Database())
|
2018-03-28 00:59:57 +00:00
|
|
|
if err != nil {
|
2019-09-23 22:39:50 +00:00
|
|
|
i.Log.Warnf("When writing to [%s]: database %q creation failed: %v",
|
2019-02-27 18:54:02 +00:00
|
|
|
c.URL(), i.Database, err)
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
func init() {
|
2018-03-28 00:30:51 +00:00
|
|
|
outputs.Add("influxdb", func() telegraf.Output {
|
|
|
|
return &InfluxDB{
|
|
|
|
Timeout: internal.Duration{Duration: time.Second * 5},
|
|
|
|
CreateHTTPClientF: func(config *HTTPConfig) (Client, error) {
|
2019-02-27 18:54:02 +00:00
|
|
|
return NewHTTPClient(*config)
|
2018-03-28 00:30:51 +00:00
|
|
|
},
|
|
|
|
CreateUDPClientF: func(config *UDPConfig) (Client, error) {
|
2019-02-27 18:54:02 +00:00
|
|
|
return NewUDPClient(*config)
|
2018-03-28 00:30:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|