2015-08-26 17:02:10 +00:00
|
|
|
package kafka
|
|
|
|
|
|
|
|
import (
|
2016-01-11 12:20:51 +00:00
|
|
|
"crypto/tls"
|
2015-08-26 17:02:10 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-02-03 19:59:34 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-02-03 19:59:34 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2016-02-03 19:59:34 +00:00
|
|
|
|
|
|
|
"github.com/Shopify/sarama"
|
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-02-03 19:59:34 +00:00
|
|
|
|
|
|
|
// Legacy SSL config options
|
2016-01-11 12:20:51 +00:00
|
|
|
// TLS client certificate
|
|
|
|
Certificate string
|
|
|
|
// TLS client key
|
|
|
|
Key string
|
|
|
|
// TLS certificate authority
|
|
|
|
CA string
|
2016-02-03 19:59:34 +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"`
|
|
|
|
|
|
|
|
// Skip SSL verification
|
|
|
|
InsecureSkipVerify bool
|
2015-08-26 17:02:10 +00:00
|
|
|
|
2016-01-11 12:20:51 +00:00
|
|
|
tlsConfig tls.Config
|
|
|
|
producer sarama.SyncProducer
|
2015-08-26 17:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-03 19:59:34 +00:00
|
|
|
### URLs of kafka brokers
|
2015-10-15 21:53:29 +00:00
|
|
|
brokers = ["localhost:9092"]
|
2016-02-03 19:59:34 +00:00
|
|
|
### Kafka topic for producer messages
|
2015-10-15 21:53:29 +00:00
|
|
|
topic = "telegraf"
|
2016-02-03 19:59:34 +00:00
|
|
|
### Telegraf tag to use as a routing key
|
|
|
|
### ie, if this tag exists, it's value will be used as the routing key
|
2015-10-15 21:53:29 +00:00
|
|
|
routing_tag = "host"
|
2016-01-11 12:20:51 +00:00
|
|
|
|
2016-02-03 19:59:34 +00:00
|
|
|
### 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
|
2015-08-26 17:02:10 +00:00
|
|
|
`
|
|
|
|
|
2016-02-03 19:59:34 +00:00
|
|
|
func (k *Kafka) Connect() error {
|
|
|
|
config := sarama.NewConfig()
|
|
|
|
// Wait for all in-sync replicas to ack the message
|
|
|
|
config.Producer.RequiredAcks = sarama.WaitForAll
|
|
|
|
// Retry up to 10 times to produce the message
|
|
|
|
config.Producer.Retry.Max = 10
|
2016-01-11 12:20:51 +00:00
|
|
|
|
2016-02-03 19:59:34 +00:00
|
|
|
// Legacy support ssl config
|
|
|
|
if k.Certificate != "" {
|
|
|
|
k.SSLCert = k.Certificate
|
|
|
|
k.SSLCA = k.CA
|
|
|
|
k.SSLKey = k.Key
|
2016-01-11 12:20:51 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 19:59:34 +00:00
|
|
|
tlsConfig, err := internal.GetTLSConfig(
|
|
|
|
k.SSLCert, k.SSLKey, k.SSLCA, k.InsecureSkipVerify)
|
2016-01-11 12:20:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func (k *Kafka) Write(metrics []telegraf.Metric) error {
|
|
|
|
if len(metrics) == 0 {
|
2015-08-26 17:02:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
for _, p := range metrics {
|
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() {
|
2016-01-27 21:21:36 +00:00
|
|
|
outputs.Add("kafka", func() telegraf.Output {
|
2015-08-26 17:02:10 +00:00
|
|
|
return &Kafka{}
|
|
|
|
})
|
|
|
|
}
|