Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retain flag to mqtt output to let brokers/servers cache measurements #4892

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion plugins/outputs/mqtt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ This plugin writes to a [MQTT Broker](http://http://mqtt.org/) acting as a mqtt
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false

## When true, metrics will be sent in one MQTT message per flush. Otherwise,
## 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
danielnelson marked this conversation as resolved.
Show resolved Hide resolved
# retain = flase
danielnelson marked this conversation as resolved.
Show resolved Hide resolved

## Data format to output.
# data_format = "influx"
Expand All @@ -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)
7 changes: 6 additions & 1 deletion plugins/outputs/mqtt/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ var sampleConfig = `
## When true, metrics will be sent in one MQTT message per flush. Otherwise,
## 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
danielnelson marked this conversation as resolved.
Show resolved Hide resolved

## Data format to output.
## Each data format has its own unique set of configuration options, read
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down