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

InfluxDB Database/Measurement Settings Based on MQTT Topic #6145

Closed
orcunbaslak opened this issue Jul 21, 2019 · 5 comments
Closed

InfluxDB Database/Measurement Settings Based on MQTT Topic #6145

orcunbaslak opened this issue Jul 21, 2019 · 5 comments
Labels
area/mqtt feature request Requests for new plugin and for new features to existing plugins

Comments

@orcunbaslak
Copy link

Feature Request

Opening a feature request kicks off a discussion.

Proposal: Telegraf will parse the topic message and move it to appropriate database/measurement based on the MQTT topic. Like /db_1/measurement_1/# will forward all the messages in that topic to those databases/measurements.

Current behavior: Not that I know of.

Desired behavior: As I explained above.

Use case:

Well, I plant to use MQTT as a communication hub for 1.000 IoT devices. I cannot go to telegraf.conf each time and create a configuration for each topic to be parsed by namepass= and moved to their respective databases/measurments. That should be done on the fly.

Thanks.

@GeorgeMac
Copy link
Contributor

GeorgeMac commented Jul 22, 2019

@orcunbaslak could you give an example of how this would differ from the current behavior of the mqtt_consumer input plugin:

https://github.com/influxdata/telegraf/tree/master/plugins/inputs/mqtt_consumer#tags

Or perhaps some more details on your usecase, so that we can understand how the current plugin doesn't support it.

@GeorgeMac GeorgeMac added area/mqtt need more info feature request Requests for new plugin and for new features to existing plugins labels Jul 22, 2019
@orcunbaslak
Copy link
Author

Hello,

Let me try to make it more clear.

Let's say I have 1.000 IoT devices.
Device #1 sends its metrics to topic : /db1/device1/
Device #2 sends its metrics to topic : /db1/device2/
Device #3 sends its metrics to topic : /db2/device3/
Device #4 sends its metrics to topic : /db2/device4/

So I want [[inputs.mqtt_consumer]] to listen to '#' and when it receives a message from device1 in the topic /db1/device2, It sends this message to [[outputs.influxdb]] with the appropriate database and measurement.

However with the name_pass setting; measurement is the same for all incoming messages which is something like mqtt_consumer_xxxx and database is what we've set in [[outputs.influxdb]].

I'd like to extract measurement name and database name from the topic and use the data shared in that topic go to exactly to that database/measurement pair (like passing a variable to # database_tag = "").

I don't know If that's doable in this current state but README files doesnt give the documentation needed for such config if it is possible.

@danielnelson
Copy link
Contributor

The mqtt_consumer does not support setting the measurement from the topic. Your best bet is to use an input data_format that supports setting the measurement as part of the message. Most of the formats support this but one notably exception I commonly see used with MQTT is data_format="value".

You could however split the topic into two tags using the regex processor, and then use the results with the database_tag:

[[processors.regex]]
  [[processors.regex.tags]]
    key = "topic"
    pattern = '^/(\w+)/(\w+)/$'
    replacement = "${1}"
    result_key = "influxdb_database"
  [[processors.regex.tags]]
    key = "topic"
    pattern = '^/(\w+)/(\w+)/$'
    replacement = "${2}"

This was referenced Dec 27, 2019
@bamne123
Copy link

Hi @danielnelson ,

Can we use Processor to Create Fields in db?
Above code will set the Tags,
What changes are required if I want to store those values to db into different column?

Eg. I have device_id in the topic, Which will be split using regex processor. Now rather than setting into tags now i want to store it in Device_ID column.

@danielnelson
Copy link
Contributor

@bamne123 The regex processor can operate on tags or fields, but it can't convert tags to fields. What you cando is use the converter processor as a second step. Use the order option to control the order in which the processors are applied.

If you have more questions please ask at the InfluxData Community site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/mqtt feature request Requests for new plugin and for new features to existing plugins
Projects
None yet
Development

No branches or pull requests

4 participants