2016-09-01 12:11:42 +00:00
|
|
|
package nats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
nats_client "github.com/nats-io/nats"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
"github.com/influxdata/telegraf/plugins/serializers"
|
2017-02-04 15:58:02 +00:00
|
|
|
"github.com/influxdata/telegraf/registry/outputs"
|
2016-09-01 12:11:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NATS struct {
|
|
|
|
// Servers is the NATS server pool to connect to
|
|
|
|
Servers []string
|
|
|
|
// Credentials
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
// NATS subject to publish metrics to
|
|
|
|
Subject string
|
|
|
|
|
|
|
|
// Path to CA file
|
2016-09-06 12:41:26 +00:00
|
|
|
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
|
2016-09-01 12:11:42 +00:00
|
|
|
InsecureSkipVerify bool
|
|
|
|
|
|
|
|
conn *nats_client.Conn
|
|
|
|
serializer serializers.Serializer
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
|
|
|
## URLs of NATS servers
|
|
|
|
servers = ["nats://localhost:4222"]
|
|
|
|
## Optional credentials
|
|
|
|
# username = ""
|
|
|
|
# password = ""
|
|
|
|
## NATS subject for producer messages
|
|
|
|
subject = "telegraf"
|
2016-09-06 12:41:26 +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
|
2016-09-01 12:11:42 +00:00
|
|
|
# insecure_skip_verify = false
|
|
|
|
|
|
|
|
## Data format to output.
|
|
|
|
## Each data format has it's own unique set of configuration options, read
|
|
|
|
## more about them here:
|
|
|
|
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
|
|
|
|
data_format = "influx"
|
|
|
|
`
|
|
|
|
|
|
|
|
func (n *NATS) SetSerializer(serializer serializers.Serializer) {
|
|
|
|
n.serializer = serializer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NATS) Connect() error {
|
|
|
|
var err error
|
2016-10-26 14:45:33 +00:00
|
|
|
|
|
|
|
// set default NATS connection options
|
2016-09-01 12:11:42 +00:00
|
|
|
opts := nats_client.DefaultOptions
|
2016-10-26 14:45:33 +00:00
|
|
|
|
|
|
|
// override max reconnection tries
|
|
|
|
opts.MaxReconnect = -1
|
|
|
|
|
|
|
|
// override servers, if any were specified
|
2016-09-01 12:11:42 +00:00
|
|
|
opts.Servers = n.Servers
|
2016-10-26 14:45:33 +00:00
|
|
|
|
|
|
|
// override authentication, if any was specified
|
2016-09-01 12:11:42 +00:00
|
|
|
if n.Username != "" {
|
|
|
|
opts.User = n.Username
|
|
|
|
opts.Password = n.Password
|
|
|
|
}
|
|
|
|
|
2016-10-26 14:45:33 +00:00
|
|
|
// override TLS, if it was specified
|
2016-09-01 12:11:42 +00:00
|
|
|
tlsConfig, err := internal.GetTLSConfig(
|
2016-09-06 12:41:26 +00:00
|
|
|
n.SSLCert, n.SSLKey, n.SSLCA, n.InsecureSkipVerify)
|
2016-09-01 12:11:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if tlsConfig != nil {
|
|
|
|
// set NATS connection TLS options
|
|
|
|
opts.Secure = true
|
|
|
|
opts.TLSConfig = tlsConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// try and connect
|
|
|
|
n.conn, err = opts.Connect()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NATS) Close() error {
|
|
|
|
n.conn.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NATS) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NATS) Description() string {
|
|
|
|
return "Send telegraf measurements to NATS"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NATS) Write(metrics []telegraf.Metric) error {
|
|
|
|
if len(metrics) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, metric := range metrics {
|
2016-11-22 12:51:57 +00:00
|
|
|
buf, err := n.serializer.Serialize(metric)
|
2016-09-01 12:11:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
err = n.conn.Publish(n.Subject, buf)
|
|
|
|
if err != nil {
|
2016-09-01 12:11:42 +00:00
|
|
|
return fmt.Errorf("FAILED to send NATS message: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
outputs.Add("nats", func() telegraf.Output {
|
|
|
|
return &NATS{}
|
|
|
|
})
|
|
|
|
}
|