Skip to content

Instantly share code, notes, and snippets.

@shantanoo-desai
Last active July 29, 2020 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shantanoo-desai/c35792ed3a15d4ec9ac5e92be7c90a3f to your computer and use it in GitHub Desktop.
Save shantanoo-desai/c35792ed3a15d4ec9ac5e92be7c90a3f to your computer and use it in GitHub Desktop.
telegraf configuration file that connects to Mosquitto Open broker and pushes data into InfluxDB v1.x
# For Telegraf v1.13
docker run --name=telegrafv1.13 -v $pwd/telegraf.conf:/etc/telegraf.conf:ro --net=host telegraf:1.13
# For Telegraf v1.15
docker run --name=telegrafv1.15 -v $pwd/telegraf.conf/etc/telegraf.conf:ro --net=host telegraf:1.15
# IOT/sensor2/temp
env,type=A temp=23.39
env,type=B temp=40.00
# IOT/sensor1/acc
env,type=A x=0.2
env,type=B x=1.3
{
"publishTopics": [
{
"name": "IOT/sensor1/acc"
},
{
"name": "IOT/sensor2/temp"
}
]
}
[agent]
interval = "20s"
round_interval = true
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "0s"
flush_interval = "10s"
flush_jitter = "0s"
precision = ""
debug = true
quiet = false
hostname = ""
omit_hostname = true
#############################################################
# OUTPUT PLUGINS #
#############################################################
[[outputs.influxdb]]
urls = [ "http://influxdb:8086" ]
database = "mqtelegraftest"
skip_database_creation = false
timeout = "5s"
###############################################################
# PROCESSOR PLUGINS #
###############################################################
[[processors.regex]]
[[processors.regex.tags]]
# use the `topic` tag to extract information from the MQTT Topic
key = "topic"
# Topic: IOT/<SENSOR_ID>/<measurement>
# Extract <SENSOR_ID>
pattern = ".*/(.*)/.*"
# Replace the first occurrence
replacement = "${1}"
# Store it in tag called:
result_key = "sensorID"
[[processors.enum]]
[[processors.enum.mapping]]
# create a mapping between extracted sensorID and some meta-data
tag = "sensorID"
dest = "location"
[processors.enum.mapping.value_mappings]
"sensor1" = "kitchen"
"sensor2" = "livingroom"
"sensor3" = "bedroom"
##################################################################
# INPUT PLUGINS #
##################################################################
[[inputs.mqtt_consumer]]
servers = [ "tcp://test.mosquitto.org:1883" ]
# Topics to subscribe to:
topics = [
"IOT/+/acc",
"IOT/+/mag",
"IOT/+/gyro",
"IOT/+/temp"
]
# Telegraf will also store the topic as a tag with name `topic`
# NOTE: necessary for the Processor REGEX to extract <Sensor_ID>
topic_tag = "topic"
# Connection timeout
connection_timeout = "30s"
# Incoming MQTT Payload from Sensor nodes is in InfluxDB Line Protocol strings
data_format = "influx"
@shantanoo-desai
Copy link
Author

shantanoo-desai commented Jul 29, 2020

Observations

the processors.enum does not work when spinning a container for telegraf v1.15 and one does not see the location tag being inserted in this result

the processors.enum WORKS only with telegraf v1.13

Reproduction

  1. use Chronograf to create the database mqtelegraftest
  2. Use MQTT.fx as client to subscribe to the Broker
    • Address: test.mosquitto.org
    • Port: 1883
  3. Publish the line protocol data presented to their respective Topics
  4. Observe the inserted data in Chronograf

@shantanoo-desai
Copy link
Author

Solution

https://gist.github.com/ssoroka/04a10106e55a9915ed74c646dba5b233#file-telegraf-conf-L31-L60

add order to which processors need to be executed in which order

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment