2015-10-04 13:52:29 +00:00
|
|
|
package mqtt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
paho "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
|
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"
|
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
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-02-09 22:03:46 +00:00
|
|
|
QoS int `toml:"qos"`
|
2015-10-04 13:52:29 +00:00
|
|
|
|
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"`
|
|
|
|
// Use SSL but skip chain & host verification
|
|
|
|
InsecureSkipVerify bool
|
|
|
|
|
|
|
|
client *paho.Client
|
|
|
|
opts *paho.ClientOptions
|
|
|
|
|
2015-10-04 13:52:29 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2015-10-15 21:53:29 +00:00
|
|
|
servers = ["localhost:1883"] # required.
|
2016-02-09 22:03:46 +00:00
|
|
|
### MQTT QoS, must be 0, 1, or 2
|
|
|
|
qos = 0
|
2015-10-04 13:52:29 +00:00
|
|
|
|
2016-02-03 19:59:34 +00:00
|
|
|
### MQTT outputs send metrics to this topic format
|
|
|
|
### "<topic_prefix>/<hostname>/<pluginname>/"
|
|
|
|
### ex: prefix/host/web01.example.com/mem
|
|
|
|
topic_prefix = "telegraf"
|
2015-10-04 13:52:29 +00:00
|
|
|
|
2016-02-03 19:59:34 +00:00
|
|
|
### username and password to connect MQTT server.
|
2015-10-15 21:53:29 +00:00
|
|
|
# username = "telegraf"
|
|
|
|
# password = "metricsmetricsmetricsmetrics"
|
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-10-04 13:52:29 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
for _, p := 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-03 19:59:34 +00:00
|
|
|
t = append(t, p.Name())
|
2015-10-04 13:52:29 +00:00
|
|
|
topic := strings.Join(t, "/")
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
value := p.String()
|
2015-10-04 13:52:29 +00:00
|
|
|
err := m.publish(topic, value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not write to MQTT server, %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MQTT) publish(topic, body string) error {
|
2016-02-09 22:03:46 +00:00
|
|
|
token := m.client.Publish(topic, byte(m.QoS), false, body)
|
2015-10-04 13:52:29 +00:00
|
|
|
token.Wait()
|
|
|
|
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()
|
|
|
|
|
2016-02-03 19:59:34 +00:00
|
|
|
opts.SetClientID("Telegraf-Output-" + internal.RandomString(5))
|
|
|
|
|
|
|
|
tlsCfg, err := internal.GetTLSConfig(
|
|
|
|
m.SSLCert, m.SSLKey, m.SSLCA, m.InsecureSkipVerify)
|
|
|
|
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
|
|
|
|
if user == "" {
|
|
|
|
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{}
|
|
|
|
})
|
|
|
|
}
|