2015-10-27 16:59:05 +00:00
|
|
|
package nsq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-01-20 20:19:03 +00:00
|
|
|
"github.com/influxdata/influxdb/client/v2"
|
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2015-10-27 16:59:05 +00:00
|
|
|
"github.com/nsqio/go-nsq"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NSQ struct {
|
|
|
|
Server string
|
|
|
|
Topic string
|
|
|
|
producer *nsq.Producer
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
|
|
|
# Location of nsqd instance listening on TCP
|
|
|
|
server = "localhost:4150"
|
|
|
|
# NSQ topic for producer messages
|
|
|
|
topic = "telegraf"
|
|
|
|
`
|
|
|
|
|
|
|
|
func (n *NSQ) Connect() error {
|
|
|
|
config := nsq.NewConfig()
|
|
|
|
producer, err := nsq.NewProducer(n.Server, config)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n.producer = producer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NSQ) Close() error {
|
|
|
|
n.producer.Stop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NSQ) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NSQ) Description() string {
|
|
|
|
return "Send telegraf measurements to NSQD"
|
|
|
|
}
|
|
|
|
|
2015-10-27 17:41:45 +00:00
|
|
|
func (n *NSQ) Write(points []*client.Point) error {
|
|
|
|
if len(points) == 0 {
|
2015-10-27 16:59:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-27 17:41:45 +00:00
|
|
|
for _, p := range points {
|
2015-10-27 16:59:05 +00:00
|
|
|
// Combine tags from Point and BatchPoints and grab the resulting
|
|
|
|
// line-protocol output string to write to NSQ
|
2015-10-27 17:41:45 +00:00
|
|
|
value := p.String()
|
2015-10-27 16:59:05 +00:00
|
|
|
|
|
|
|
err := n.producer.Publish(n.Topic, []byte(value))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("FAILED to send NSQD message: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
outputs.Add("nsq", func() outputs.Output {
|
|
|
|
return &NSQ{}
|
|
|
|
})
|
|
|
|
}
|