2015-10-04 13:52:29 +00:00
|
|
|
package mqtt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2018-01-22 19:15:13 +00:00
|
|
|
"time"
|
2015-10-04 13:52:29 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2018-05-04 23:33:23 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/tls"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2016-02-10 22:50:07 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers"
|
2016-02-13 00:00:11 +00:00
|
|
|
|
2016-03-30 17:54:01 +00:00
|
|
|
paho "github.com/eclipse/paho.mqtt.golang"
|
2015-10-04 13:52:29 +00:00
|
|
|
)
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
var sampleConfig = `
|
|
|
|
servers = ["localhost:1883"] # required.
|
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## MQTT outputs send metrics to this topic format
|
|
|
|
## "<topic_prefix>/<hostname>/<pluginname>/"
|
|
|
|
## ex: prefix/web01.example.com/mem
|
2016-02-10 22:50:07 +00:00
|
|
|
topic_prefix = "telegraf"
|
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## username and password to connect MQTT server.
|
2016-02-10 22:50:07 +00:00
|
|
|
# username = "telegraf"
|
|
|
|
# password = "metricsmetricsmetricsmetrics"
|
|
|
|
|
2018-01-22 19:15:13 +00:00
|
|
|
## Timeout for write operations. default: 5s
|
|
|
|
# timeout = "5s"
|
|
|
|
|
2016-11-24 15:11:24 +00:00
|
|
|
## client ID, if not set a random ID is generated
|
|
|
|
# client_id = ""
|
|
|
|
|
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-02-10 22:50:07 +00:00
|
|
|
# insecure_skip_verify = false
|
|
|
|
|
2016-03-31 23:50:24 +00:00
|
|
|
## 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-02-18 21:26:51 +00:00
|
|
|
## more about them here:
|
|
|
|
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
|
2016-02-10 22:50:07 +00:00
|
|
|
data_format = "influx"
|
2018-05-19 01:55:02 +00:00
|
|
|
|
|
|
|
## Batch messages in a topic
|
|
|
|
## batch = false
|
|
|
|
## Flag to determine if messages sent in a topic in a flush interval,
|
|
|
|
## need to be batched into one message.
|
|
|
|
## batch = true, batches the messages in a topic to one messages
|
|
|
|
## batch = false, default behaviour
|
|
|
|
# batch = false
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
`
|
|
|
|
|
2015-10-04 13:52:29 +00:00
|
|
|
type MQTT struct {
|
|
|
|
Servers []string `toml:"servers"`
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Database string
|
2015-11-10 21:40:39 +00:00
|
|
|
Timeout internal.Duration
|
2015-10-04 13:52:29 +00:00
|
|
|
TopicPrefix string
|
2016-11-24 15:11:24 +00:00
|
|
|
QoS int `toml:"qos"`
|
|
|
|
ClientID string `toml:"client_id"`
|
2018-05-04 23:33:23 +00:00
|
|
|
tls.ClientConfig
|
2018-05-19 01:55:02 +00:00
|
|
|
BatchMessage bool `toml:"batch"`
|
2016-02-03 19:59:34 +00:00
|
|
|
|
2016-03-30 17:54:01 +00:00
|
|
|
client paho.Client
|
2016-02-03 19:59:34 +00:00
|
|
|
opts *paho.ClientOptions
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
serializer serializers.Serializer
|
|
|
|
|
2015-10-04 13:52:29 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MQTT) Connect() error {
|
|
|
|
var err error
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2016-02-09 22:03:46 +00:00
|
|
|
if m.QoS > 2 || m.QoS < 0 {
|
|
|
|
return fmt.Errorf("MQTT Output, invalid QoS value: %d", m.QoS)
|
|
|
|
}
|
2015-10-04 13:52:29 +00:00
|
|
|
|
2016-02-03 19:59:34 +00:00
|
|
|
m.opts, err = m.createOpts()
|
2015-10-04 13:52:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-03 19:59:34 +00:00
|
|
|
m.client = paho.NewClient(m.opts)
|
|
|
|
if token := m.client.Connect(); token.Wait() && token.Error() != nil {
|
2015-10-04 13:52:29 +00:00
|
|
|
return token.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
func (m *MQTT) SetSerializer(serializer serializers.Serializer) {
|
|
|
|
m.serializer = serializer
|
|
|
|
}
|
|
|
|
|
2015-10-04 13:52:29 +00:00
|
|
|
func (m *MQTT) Close() error {
|
2016-02-03 19:59:34 +00:00
|
|
|
if m.client.IsConnected() {
|
|
|
|
m.client.Disconnect(20)
|
2015-10-04 13:52:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MQTT) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MQTT) Description() string {
|
|
|
|
return "Configuration for MQTT server to send metrics to"
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func (m *MQTT) Write(metrics []telegraf.Metric) error {
|
2015-10-04 13:52:29 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2016-01-27 23:15:14 +00:00
|
|
|
if len(metrics) == 0 {
|
2015-10-04 13:52:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-01-27 23:15:14 +00:00
|
|
|
hostname, ok := metrics[0].Tags()["host"]
|
2015-10-05 02:07:41 +00:00
|
|
|
if !ok {
|
|
|
|
hostname = ""
|
|
|
|
}
|
2015-10-04 13:52:29 +00:00
|
|
|
|
2018-05-19 01:55:02 +00:00
|
|
|
metricsmap := make(map[string][]telegraf.Metric)
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
for _, metric := range metrics {
|
2015-10-04 13:52:29 +00:00
|
|
|
var t []string
|
|
|
|
if m.TopicPrefix != "" {
|
|
|
|
t = append(t, m.TopicPrefix)
|
|
|
|
}
|
2016-02-03 19:59:34 +00:00
|
|
|
if hostname != "" {
|
|
|
|
t = append(t, hostname)
|
2015-10-05 01:43:46 +00:00
|
|
|
}
|
2015-10-16 22:13:32 +00:00
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
t = append(t, metric.Name())
|
2015-10-04 13:52:29 +00:00
|
|
|
topic := strings.Join(t, "/")
|
|
|
|
|
2018-05-19 01:55:02 +00:00
|
|
|
if m.BatchMessage {
|
|
|
|
metricsmap[topic] = append(metricsmap[topic], metric)
|
|
|
|
} else {
|
|
|
|
buf, err := m.serializer.Serialize(metric)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.publish(topic, buf)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not write to MQTT server, %s", err)
|
|
|
|
}
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
2018-05-19 01:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key := range metricsmap {
|
|
|
|
buf, err := m.serializer.SerializeBatch(metricsmap[key])
|
2016-02-10 22:50:07 +00:00
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
if err != nil {
|
2018-05-19 01:55:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
publisherr := m.publish(key, buf)
|
|
|
|
if publisherr != nil {
|
|
|
|
return fmt.Errorf("Could not write to MQTT server, %s", publisherr)
|
2015-10-04 13:52:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
func (m *MQTT) publish(topic string, body []byte) error {
|
2016-02-09 22:03:46 +00:00
|
|
|
token := m.client.Publish(topic, byte(m.QoS), false, body)
|
2018-01-22 19:15:13 +00:00
|
|
|
token.WaitTimeout(m.Timeout.Duration)
|
2015-10-04 13:52:29 +00:00
|
|
|
if token.Error() != nil {
|
|
|
|
return token.Error()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-03 19:59:34 +00:00
|
|
|
func (m *MQTT) createOpts() (*paho.ClientOptions, error) {
|
2015-10-04 13:52:29 +00:00
|
|
|
opts := paho.NewClientOptions()
|
2018-02-25 09:04:04 +00:00
|
|
|
opts.KeepAlive = 0 * time.Second
|
2015-10-04 13:52:29 +00:00
|
|
|
|
2018-01-22 19:15:13 +00:00
|
|
|
if m.Timeout.Duration < time.Second {
|
|
|
|
m.Timeout.Duration = 5 * time.Second
|
|
|
|
}
|
|
|
|
opts.WriteTimeout = m.Timeout.Duration
|
|
|
|
|
2016-11-24 15:11:24 +00:00
|
|
|
if m.ClientID != "" {
|
|
|
|
opts.SetClientID(m.ClientID)
|
|
|
|
} else {
|
|
|
|
opts.SetClientID("Telegraf-Output-" + internal.RandomString(5))
|
|
|
|
}
|
2016-02-03 19:59:34 +00:00
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsCfg, err := m.ClientConfig.TLSConfig()
|
2016-02-03 19:59:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-04 13:52:29 +00:00
|
|
|
|
|
|
|
scheme := "tcp"
|
2016-02-03 19:59:34 +00:00
|
|
|
if tlsCfg != nil {
|
2015-10-04 13:52:29 +00:00
|
|
|
scheme = "ssl"
|
2016-02-03 19:59:34 +00:00
|
|
|
opts.SetTLSConfig(tlsCfg)
|
2015-10-04 13:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user := m.Username
|
2016-03-03 13:14:50 +00:00
|
|
|
if user != "" {
|
2015-10-04 13:52:29 +00:00
|
|
|
opts.SetUsername(user)
|
|
|
|
}
|
|
|
|
password := m.Password
|
|
|
|
if password != "" {
|
|
|
|
opts.SetPassword(password)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m.Servers) == 0 {
|
|
|
|
return opts, fmt.Errorf("could not get host infomations")
|
|
|
|
}
|
|
|
|
for _, host := range m.Servers {
|
|
|
|
server := fmt.Sprintf("%s://%s", scheme, host)
|
|
|
|
|
|
|
|
opts.AddBroker(server)
|
|
|
|
}
|
|
|
|
opts.SetAutoReconnect(true)
|
|
|
|
return opts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
outputs.Add("mqtt", func() telegraf.Output {
|
2015-10-04 13:52:29 +00:00
|
|
|
return &MQTT{}
|
|
|
|
})
|
|
|
|
}
|