2016-09-01 12:11:42 +00:00
|
|
|
package nats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-06-04 00:34:48 +00:00
|
|
|
"log"
|
2020-02-20 22:30:04 +00:00
|
|
|
"strings"
|
2016-09-01 12:11:42 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2018-05-04 23:33:23 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/tls"
|
2016-09-01 12:11:42 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
|
|
|
"github.com/influxdata/telegraf/plugins/serializers"
|
2020-02-20 22:30:04 +00:00
|
|
|
"github.com/nats-io/nats.go"
|
2016-09-01 12:11:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NATS struct {
|
2020-02-20 22:30:04 +00:00
|
|
|
Servers []string `toml:"servers"`
|
|
|
|
Secure bool `toml:"secure"`
|
|
|
|
Username string `toml:"username"`
|
|
|
|
Password string `toml:"password"`
|
|
|
|
Credentials string `toml:"credentials"`
|
|
|
|
Subject string `toml:"subject"`
|
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
tls.ClientConfig
|
2016-09-01 12:11:42 +00:00
|
|
|
|
2020-02-20 22:30:04 +00:00
|
|
|
conn *nats.Conn
|
2016-09-01 12:11:42 +00:00
|
|
|
serializer serializers.Serializer
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
|
|
|
## URLs of NATS servers
|
|
|
|
servers = ["nats://localhost:4222"]
|
2020-02-20 22:30:04 +00:00
|
|
|
|
2016-09-01 12:11:42 +00:00
|
|
|
## Optional credentials
|
|
|
|
# username = ""
|
|
|
|
# password = ""
|
2020-02-20 22:30:04 +00:00
|
|
|
|
|
|
|
## Optional NATS 2.0 and NATS NGS compatible user credentials
|
|
|
|
# credentials = "/etc/telegraf/nats.creds"
|
|
|
|
|
2016-09-01 12:11:42 +00:00
|
|
|
## NATS subject for producer messages
|
|
|
|
subject = "telegraf"
|
2016-09-06 12:41:26 +00:00
|
|
|
|
2019-08-02 23:56:49 +00:00
|
|
|
## Use Transport Layer Security
|
|
|
|
# secure = false
|
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
## Optional TLS Config
|
|
|
|
# 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-09-01 12:11:42 +00:00
|
|
|
# insecure_skip_verify = false
|
|
|
|
|
|
|
|
## Data format to output.
|
2017-04-27 21:59:18 +00:00
|
|
|
## Each data format has its own unique set of configuration options, read
|
2016-09-01 12:11:42 +00:00
|
|
|
## 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
|
|
|
|
2020-02-20 22:30:04 +00:00
|
|
|
opts := []nats.Option{
|
|
|
|
nats.MaxReconnects(-1),
|
|
|
|
}
|
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 != "" {
|
2020-02-20 22:30:04 +00:00
|
|
|
opts = append(opts, nats.UserInfo(n.Username, n.Password))
|
2016-09-01 12:11:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 23:56:49 +00:00
|
|
|
if n.Secure {
|
|
|
|
tlsConfig, err := n.ClientConfig.TLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-20 22:30:04 +00:00
|
|
|
opts = append(opts, nats.Secure(tlsConfig))
|
2016-09-01 12:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// try and connect
|
2020-02-20 22:30:04 +00:00
|
|
|
n.conn, err = nats.Connect(strings.Join(n.Servers, ","), opts...)
|
2016-09-01 12:11:42 +00:00
|
|
|
|
|
|
|
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 {
|
2019-06-04 00:34:48 +00:00
|
|
|
log.Printf("D! [outputs.nats] Could not serialize metric: %v", err)
|
|
|
|
continue
|
2016-09-01 12:11:42 +00:00
|
|
|
}
|
|
|
|
|
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{}
|
|
|
|
})
|
|
|
|
}
|