2015-08-26 17:02:10 +00:00
|
|
|
package kafka
|
|
|
|
|
|
|
|
import (
|
2016-01-06 20:50:33 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2015-08-26 17:02:10 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/Shopify/sarama"
|
2015-10-16 22:13:32 +00:00
|
|
|
"github.com/influxdb/influxdb/client/v2"
|
2015-08-26 17:02:10 +00:00
|
|
|
"github.com/influxdb/telegraf/outputs"
|
2016-01-06 20:50:33 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2015-08-26 17:02:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Kafka struct {
|
|
|
|
// Kafka brokers to send metrics to
|
|
|
|
Brokers []string
|
|
|
|
// Kafka topic
|
|
|
|
Topic string
|
2015-09-16 19:10:26 +00:00
|
|
|
// Routing Key Tag
|
|
|
|
RoutingTag string `toml:"routing_tag"`
|
2016-01-06 20:50:33 +00:00
|
|
|
// TLS client certificate
|
|
|
|
Certificate string
|
|
|
|
// TLS client key
|
|
|
|
Key string
|
|
|
|
// TLS certificate authority
|
|
|
|
CA string
|
|
|
|
// Verfiy SSL certificate chain
|
|
|
|
VerifySsl bool
|
2015-08-26 17:02:10 +00:00
|
|
|
|
2016-01-06 20:50:33 +00:00
|
|
|
tlsConfig tls.Config
|
2015-08-26 17:02:10 +00:00
|
|
|
producer sarama.SyncProducer
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2015-10-15 21:53:29 +00:00
|
|
|
# URLs of kafka brokers
|
|
|
|
brokers = ["localhost:9092"]
|
|
|
|
# Kafka topic for producer messages
|
|
|
|
topic = "telegraf"
|
|
|
|
# Telegraf tag to use as a routing key
|
|
|
|
# ie, if this tag exists, it's value will be used as the routing key
|
|
|
|
routing_tag = "host"
|
2016-01-06 20:50:33 +00:00
|
|
|
# Client certificate
|
|
|
|
certificate = ""
|
|
|
|
# Client key
|
|
|
|
key = ""
|
|
|
|
# Certificate authority file
|
|
|
|
ca = ""
|
|
|
|
# Verify SSL certificate chain
|
|
|
|
verify_ssl = false
|
2015-08-26 17:02:10 +00:00
|
|
|
`
|
|
|
|
|
2016-01-06 20:50:33 +00:00
|
|
|
func createTlsConfiguration(k *Kafka) (t *tls.Config) {
|
|
|
|
if k.Certificate != "" && k.Key != "" && k.CA != "" {
|
|
|
|
cert, err := tls.LoadX509KeyPair(k.Certificate, k.Key)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
caCert, err := ioutil.ReadFile(k.CA)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
|
|
|
|
t = &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
RootCAs: caCertPool,
|
|
|
|
InsecureSkipVerify: k.VerifySsl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// will be nil by default if nothing is provided
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2015-08-26 17:02:10 +00:00
|
|
|
func (k *Kafka) Connect() error {
|
2016-01-06 20:50:33 +00:00
|
|
|
config := sarama.NewConfig()
|
|
|
|
config.Producer.RequiredAcks = sarama.WaitForAll // Wait for all in-sync replicas to ack the message
|
|
|
|
config.Producer.Retry.Max = 10 // Retry up to 10 times to produce the message
|
|
|
|
tlsConfig := createTlsConfiguration(k)
|
|
|
|
if tlsConfig != nil {
|
|
|
|
config.Net.TLS.Config = tlsConfig
|
|
|
|
config.Net.TLS.Enable = true
|
|
|
|
}
|
|
|
|
|
|
|
|
producer, err := sarama.NewSyncProducer(k.Brokers, config)
|
2015-08-26 17:02:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
k.producer = producer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Kafka) Close() error {
|
|
|
|
return k.producer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Kafka) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Kafka) Description() string {
|
|
|
|
return "Configuration for the Kafka server to send metrics to"
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
func (k *Kafka) Write(points []*client.Point) error {
|
|
|
|
if len(points) == 0 {
|
2015-08-26 17:02:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
for _, p := range points {
|
2015-08-26 17:02:10 +00:00
|
|
|
// Combine tags from Point and BatchPoints and grab the resulting
|
|
|
|
// line-protocol output string to write to Kafka
|
2015-10-16 22:13:32 +00:00
|
|
|
value := p.String()
|
2015-08-26 17:02:10 +00:00
|
|
|
|
|
|
|
m := &sarama.ProducerMessage{
|
|
|
|
Topic: k.Topic,
|
|
|
|
Value: sarama.StringEncoder(value),
|
|
|
|
}
|
2015-10-16 22:13:32 +00:00
|
|
|
if h, ok := p.Tags()[k.RoutingTag]; ok {
|
2015-08-26 17:02:10 +00:00
|
|
|
m.Key = sarama.StringEncoder(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := k.producer.SendMessage(m)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("FAILED to send kafka message: %s\n",
|
|
|
|
err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
outputs.Add("kafka", func() outputs.Output {
|
|
|
|
return &Kafka{}
|
|
|
|
})
|
|
|
|
}
|