From 4b5811e8edb15f09693280d0d75350327947a469 Mon Sep 17 00:00:00 2001 From: Pieter Slabbert Date: Thu, 24 Nov 2016 17:11:24 +0200 Subject: [PATCH] Enable setting a clientID for MQTT Output --- plugins/outputs/mqtt/mqtt.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plugins/outputs/mqtt/mqtt.go b/plugins/outputs/mqtt/mqtt.go index c57ee8cd0..058cac110 100644 --- a/plugins/outputs/mqtt/mqtt.go +++ b/plugins/outputs/mqtt/mqtt.go @@ -25,6 +25,9 @@ var sampleConfig = ` # username = "telegraf" # password = "metricsmetricsmetricsmetrics" + ## client ID, if not set a random ID is generated + # client_id = "telegraf" + ## Optional SSL Config # ssl_ca = "/etc/telegraf/ca.pem" # ssl_cert = "/etc/telegraf/cert.pem" @@ -46,7 +49,8 @@ type MQTT struct { Database string Timeout internal.Duration TopicPrefix string - QoS int `toml:"qos"` + QoS int `toml:"qos"` + ClientID string `toml:"client_id"` // Path to CA file SSLCA string `toml:"ssl_ca"` @@ -157,7 +161,11 @@ func (m *MQTT) publish(topic, body string) error { func (m *MQTT) createOpts() (*paho.ClientOptions, error) { opts := paho.NewClientOptions() - opts.SetClientID("Telegraf-Output-" + internal.RandomString(5)) + if m.ClientID != "" { + opts.SetClientID(m.ClientID) + } else { + opts.SetClientID("Telegraf-Output-" + internal.RandomString(5)) + } tlsCfg, err := internal.GetTLSConfig( m.SSLCert, m.SSLKey, m.SSLCA, m.InsecureSkipVerify)