Tom999
March 18, 2023, 4:27pm
1
Hello All,
following input quasi JSON:
‘{“TimeStamp”:1679156093057,“WorkCenter”:“MyPlant1”,“PlantCode”:“4183”,“LineName”:“Line001”,“MachineName”:“AssemblerNo1”,“StationName”:“10N1”,“DeviceName”:“HardStop”,“TagName”:“ExtCmd_T”,“Value”:50,“Anomaly”:0,“AvgWindow”:50.0,“StdWindow”:0.0} NULL’
as you can see last character in this string is NULL which is produced by PLC. I can’t get rid of it.
Now how can I convert this in fact non-JSON into valid line protocol for InfluxDB ver2.0?
How to parse tags and fields from payload (not topic) os this JSON?
How to create measurement name as hard-coded name (not parsed from topic or payload) ?
I tried many configurations and read all topics in internet.
There is no clear documentation with examples how to handle such issue.
What I found is:
opened 11:55AM - 18 May 20 UTC
closed 11:17PM - 20 May 20 UTC
bug
area/mqtt
area/json
Hi everyone,
I’m working on some project, where I need to collect data to inf… luxdb using mqtt input plugin in telegraf. But I’m facing strange behaviour of mqtt input. I’m sending data on specific topic using JSON string (i.e. “{value”:15}", but MQTT producer, which I’m using adds, null (’\x00’) char at the end of message. It looks like below in preview using MQTTBox client:
![14eeee37bcfdb5e2943c54c0281cae077d6ebade](https://user-images.githubusercontent.com/65446967/82209574-c2337c00-990d-11ea-88af-1e5f69277588.png)
First message was sent from MQTT producer and it has lenght of 41 bytes, while second I sent from regular MQTT client - MQTTBox - length of the second message is 40 bytes - null character at the end is missing. First message causes error in telegraf, while second is correctly wrote to database:
```
2020-05-16T14:03:14Z E! [inputs.mqtt_consumer] Error in plugin: invalid character '\x00' after top-level value
2020-05-16T14:03:15Z E! [inputs.mqtt_consumer] Error in plugin: invalid character '\x00' after top-level value
```
My current telegraf config file looks like this (I have to use multiple MQTT inputs to multiple Influx databases, but this not a problem):
```
[[outputs.influxdb]]
urls = ["http://localhost:8086"]
database = "telegraf_metrics_01"
retention_policy = ""
write_consistency = "any"
timeout = "37s"
tagexclude = ["destinationdb"]
[outputs.influxdb.tagpass]
destinationdb = ["db01"]
[[inputs.mqtt_consumer]]
servers = ["tcp://localhost:1883"]
qos = 0
topics = [
"test/maszyna1/#"
]
data_format = "json"
[inputs.mqtt_consumer.tags]
destinationdb = "db01"
```
Do you have any idea, how to configure telegraf to (maybe) ignore those null characters, or cut this one byte using some processor plugin? I read some about proccessors.string.trim_right plugin, but as I tested - it only works, when input data are correct, so It won't be helpful in this case.
It’s also important that, when I’m using “value” data_format of mqtt input, instead of “json” - telegraf works fine with this mqtt producer, even at the end of simple value also null character appears.
**So maybe plugin (or even parser) should ignore those characters in payload (i.e. whitespaces, nulls) by default?** Anyways - I have to use json format to pass multiple parameters to influx at once.
I will appreciate any help
Have a good day!
So probably parsing the non-Json from string to valid JSON is needed. But I use InfluxDB >=2.0
I used also docker.
Here is my telegraf.conf:
[agent]
interval = "1s"
round_interval = true
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "0s"
flush_interval = "10s"
flush_jitter = "0s"
precision = ""
hostname = ""
omit_hostname = true
##############################################################################
[[inputs.mqtt_consumer]]
servers = ["tcp://MQTT_Broker:1883"]
topics = ["PPS_L1/AssemblerNo1/10N1/HardStop/ExtCmd_T"]
username = "mqttbroker"
password = "LoopEdge.123"
#data_format = "json"
##data_format = "influx"
data_format = "value"
data_type = "string"
#data_format = "json_v2"
topic_tag = ""
[[inputs.mqtt_consumer.json_v2]]
measurement_name = "measurement1"
timestamp_path = "TimeStamp"
timestamp_format = "unix"
[[inputs.mqtt_consumer.json_v2.tag]]
path = "LineName"
[[inputs.mqtt_consumer.json_v2.field]]
path = "Value"
type = "float"
[[inputs.mqtt_consumer.json_v2.field]]
path = "AvgWindow"
type = "float"
###############################################################################
[[processors.parser]]
parse_fields = ["Value", "Anomaly", "AvgWindow"]
parse_tags = ["LineName", "MachineName"]
merge = "override"
data_format = "json"
###############################################################################
[[outputs.influxdb_v2]]
urls = ["http://InfluxDB:8086"]
token = "firToJO9jjn0USZfpmAg5RfU5-R5BCKYjDFHNL8j3oI3VmVAjEQxLmPRq3uGzQw5w6t8PuBGy-A5KRzjv_7EBA=="
organization = "Company"
namepass = ["measurement1"]
bucket = "BUCKET_TEST"
So please, please any hint and clear answers with vallid telegraf.conf file are appreciated.
Regards
Tom
Hello @Tom999 ,
I like to use the --test
flag to see what the resulting line protocol looks like and then fix my config based off of that.
As well as setting debug=true
in the agent portion of the config.
Can you please use that and share the output?
Tom999
March 23, 2023, 10:21am
3
OK, I managed to fix it on the PLC level to send valid JSON.
Thank you.