I’ve now spent several days to figure out how to write a proper Telegraf configuration but I couldn’t get it right:
I have a physical device and I want to record information from its components. I subscribe to several MQTT topics (which are named similarly) and they cover basic information like serial numbers, installed components, firmware versions and so on.
So I defined a bucket named device_info and should receive data extracted from multiple MQTT topics. One of the interesting data points are the product names of the various components of the device.
...
[[inputs.mqtt_consumer]]
servers = ["ssl://mqtt.myserver.com:8883"]
topics = [
"t/+/cpu/1/ProductName",
...
"t/+/battery/1/ProductName",
...
]
topic_tag = "" # Don't add a tag with the individual topic which would prevent combining different fields into one measurement entry.
name_override = "device_info"
...
And then I set the parsing rules to feed the MQTT data into the bucket:
# cpu_product_name
[[inputs.mqtt_consumer.topic_parsing]]
alias = "device_info__cpu__product_name"
topic = "t/+/cpu/1/ProductName"
tags = "_/site_id/_/_/pivot_field"
[[inputs.mqtt_consumer.json_v2]]
[[inputs.mqtt_consumer.json_v2.field]]
path = "value"
type = "string"
[[processors.pivot]]
# Let the tag become the actual measurement value
tag_key = "pivot_field"
value_key = "value"
[[processors.rename]]
namepass = ["device_info__cpu__product_name"]
[[processors.rename.replace]]
field = "ProductName"
dest = "cpu_product_name"
# battery_product_name
[[inputs.mqtt_consumer.topic_parsing]]
alias = "device_info__battery__product_name"
topic = "t/+/battery/1/ProductName"
tags = "_/site_id/_/_/pivot_field"
[[inputs.mqtt_consumer.json_v2]]
[[inputs.mqtt_consumer.json_v2.field]]
path = "value"
type = "string"
[[processors.pivot]]
# Let the tag become the actual measurement value
tag_key = "pivot_field"
value_key = "value"
[[processors.rename]]
namepass = ["device_info__battery__product_name"]
[[processors.rename.replace]]
field = "ProductName"
dest = "batt_product_name"
I end up not receiving the CPU product name in column cpu_product_name and the battery name in column batt_product_name. I either had the renaming processors not firing, or that both values overwrite them mutually.
I’ve tried many different things with namepass, tags, fields and so on but they all didn’t work out. You help is much appreciated!
So both topics end up in column ProductName instead of their individual columns because the renaming processor drops the input.
In contrast, if I remove the namepass = ... condition in the two renaming sections the inputs are processed, but I end up seeing only the CPU in the correct collumn but the battery the but batt_product_name column is empty:
ProductName
batt_product_name
cpu_product_name
site_id
time
ARM Cortex-A72
c061abcd
2024-01-27T06:05:30Z
Again, my desired output is this:
ProductName
batt_product_name
cpu_product_name
site_id
time
LiONbatt
ARM Cortex-A72
c061abcd
2024-01-27T05:58:40Z
What do I have to change to have both the cpu_product_name and the batt_product_name in their respective columns?
So that output is not from outputs.fiile Why I wanted you to show that output is I believe the metrics you are collecting are in two different metrics, not one given you have two different topic parsers configured.
To merge two different metrics, if they have the same tag set + field + timestamp, you can use the merge processors. Otherwise you have to use an aggregator to combine them together.
I don’t understand why it is double output but it’s what the logs give me.
I understood from your answer that having two [[inputs.mqtt_consumer.topic_parsing]] might lead to my problem of not being able to merge both MQTT messages together easily. Can you give me a sample on how I have to modify my configuration above, so I can put the two different MQTT topics ending with the same suffix (“ProductName”) into the same metric (with different names cpu_ / batt_)?
Essentially, you could try using the merge aggregator. Take a look at the example there. However, if the field names are in fact the same that won’t work. The fields need unique names.