Search code examples
curlxargsinfluxdb

curl / xargs / influxdb - generate cli argument from text file


I have the file data.txt that looks like this:

ecgtimeseries value=-0.145 1441114274130
ecgtimeseries value=-0.145 1441114274133
ecgtimeseries value=-0.145 1441114274136
ecgtimeseries value=-0.145 1441114274139
ecgtimeseries value=-0.145 1441114274142

I need to generate this InfluxDB command based on the contents of data.txt:

curl -i -XPOST 'http://192.168.99.100:8086/write?db=ecg&precision=ms' --data-binary 'ecgtimeseries value=-0.145 1441114274130
ecgtimeseries value=-0.145 1441114274133
ecgtimeseries value=-0.145 1441114274136
ecgtimeseries value=-0.145 1441114274139
ecgtimeseries value=-0.145 1441114274142'

This is my first attempt at a solution (with xargs):

$ cat data.txt | xargs -0 -t curl -i -XPOST 'http://192.168.99.100:8086/write?db=ecg&precision=ms' --data-binary 
curl -i -XPOST http://192.168.99.100:8086/write?db=ecg&precision=ms --data-binary ecgtimeseries value=-0.145 1441114274130
ecgtimeseries value=-0.145 1441114274133
ecgtimeseries value=-0.145 1441114274136
ecgtimeseries value=-0.145 1441114274139
ecgtimeseries value=-0.145 1441114274142

However, because I don't know how to also generate those apostrophes that enclose the data, the command doesn't behave as expected.

Any ideas?


Solution

  • In order to pass a file to InfluxDB using curl, use the @<filename> syntax as described in the "Write a Batch of Points with curl" section of the write syntax doc.

    For example: curl -i -XPOST 'http://192.168.99.100:8086/write?db=ecg&precision=ms' --data-binary @data.txt

    The @<filename> syntax is part of curl, so if you have difficulties please check the curl manpage. Note that your file must not have carriage returns 0x0D, only linefeeds 0x0A. If you aren't on a Windows machine you should be fine.