telegraf/plugins/outputs/influxdb/influxdb.go

191 lines
4.7 KiB
Go
Raw Normal View History

package influxdb
import (
2015-09-09 21:56:10 +00:00
"errors"
"fmt"
"log"
2015-09-09 21:56:10 +00:00
"math/rand"
"net/url"
"strings"
2015-12-15 17:08:13 +00:00
"time"
"github.com/influxdata/telegraf"
2016-01-20 18:57:35 +00:00
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/influxdata/influxdb/client/v2"
)
type InfluxDB struct {
2015-09-09 21:56:10 +00:00
// URL is only for backwards compatability
URL string
URLs []string `toml:"urls"`
Username string
Password string
Database string
UserAgent string
Precision string
Timeout internal.Duration
UDPPayload int `toml:"udp_payload"`
// 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
conns []client.Client
}
var sampleConfig = `
### 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.
# urls = ["udp://localhost:8089"] # UDP endpoint example
urls = ["http://localhost:8086"] # required
### The target database for metrics (telegraf will create it if not exists)
database = "telegraf" # required
### Precision of writes, valid values are "ns", "us" (or "µs"), "ms", "s", "m", "h".
### note: using "s" precision greatly improves InfluxDB compression
precision = "s"
### 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"
### Set the user agent for HTTP POSTs (can be useful for log differentiation)
# user_agent = "telegraf"
### Set UDP payload size, defaults to InfluxDB UDP Client default (512 bytes)
# udp_payload = 512
### Optional SSL Config
# ssl_ca = "/etc/telegraf/ca.pem"
# ssl_cert = "/etc/telegraf/cert.pem"
# ssl_key = "/etc/telegraf/key.pem"
### Use SSL but skip chain & host verification
# insecure_skip_verify = false
`
func (i *InfluxDB) Connect() error {
var urls []string
for _, u := range i.URLs {
2015-09-09 21:56:10 +00:00
urls = append(urls, u)
}
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 != "" {
urls = append(urls, i.URL)
2015-09-09 21:56:10 +00:00
}
tlsCfg, err := internal.GetTLSConfig(
i.SSLCert, i.SSLKey, i.SSLCA, i.InsecureSkipVerify)
if err != nil {
return err
}
var conns []client.Client
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
}
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,
Username: i.Username,
Password: i.Password,
UserAgent: i.UserAgent,
Timeout: i.Timeout.Duration,
TLSConfig: tlsCfg,
})
if err != nil {
return err
}
// Create Database if it doesn't exist
_, e := c.Query(client.Query{
Command: fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", i.Database),
})
if e != nil {
log.Println("Database creation failed: " + e.Error())
}
conns = append(conns, c)
2015-09-09 21:56:10 +00:00
}
}
2015-09-09 21:56:10 +00:00
i.conns = conns
2015-12-15 17:08:13 +00:00
rand.Seed(time.Now().UnixNano())
return nil
}
func (i *InfluxDB) Close() error {
// InfluxDB client does not provide a Close() function
return nil
}
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.
func (i *InfluxDB) Write(metrics []telegraf.Metric) error {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: i.Database,
Precision: i.Precision,
})
if err != nil {
return err
}
for _, metric := range metrics {
bp.AddPoint(metric.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")
2015-09-09 21:56:10 +00:00
p := rand.Perm(len(i.conns))
for _, n := range p {
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-09-09 21:56:10 +00:00
return err
}
func init() {
outputs.Add("influxdb", func() telegraf.Output {
return &InfluxDB{}
})
}