Add option to set retain flag on messages in mqtt output (#4892)

This commit is contained in:
Paweł Kraszewski 2019-02-12 02:22:31 +01:00 committed by Daniel Nelson
parent c8832a28c4
commit 0084138bc6
2 changed files with 12 additions and 2 deletions

View File

@ -37,6 +37,10 @@ This plugin writes to a [MQTT Broker](http://http://mqtt.org/) acting as a mqtt
## metrics are written one metric per MQTT message.
# batch = false
## When true, metric will have RETAIN flag set, making broker cache entries until someone
## actually reads it
# retain = flase
## Data format to output.
# data_format = "influx"
```
@ -56,4 +60,5 @@ This plugin writes to a [MQTT Broker](http://http://mqtt.org/) acting as a mqtt
* `tls_cert`: TLS CERT
* `tls_key`: TLS key
* `insecure_skip_verify`: Use TLS but skip chain & host verification (default: false)
* `retain`: Set `retain` flag when publishing, instructing server to cache metric until someone reads it (default: false)
* `data_format`: [About Telegraf data formats](https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md)

View File

@ -50,6 +50,10 @@ var sampleConfig = `
## metrics are written one metric per MQTT message.
# batch = false
## When true, metric will have RETAIN flag set, making broker cache entries until someone
## actually reads it
# retain = flase
## Data format to output.
## Each data format has its own unique set of configuration options, read
## more about them here:
@ -68,6 +72,7 @@ type MQTT struct {
ClientID string `toml:"client_id"`
tls.ClientConfig
BatchMessage bool `toml:"batch"`
Retain bool
client paho.Client
opts *paho.ClientOptions
@ -174,7 +179,7 @@ func (m *MQTT) Write(metrics []telegraf.Metric) error {
}
func (m *MQTT) publish(topic string, body []byte) error {
token := m.client.Publish(topic, byte(m.QoS), false, body)
token := m.client.Publish(topic, byte(m.QoS), m.Retain, body)
token.WaitTimeout(m.Timeout.Duration)
if token.Error() != nil {
return token.Error()