Hi,
I know it’s a little bit old topic, but as I ended up here, some other might.
Another (easier?) solution might also be to use the inputs.tail plugin.
Let your script write its output to a file, and configure Telegraf so it reads its content.
Here is a MWE, where Telegraf reads a file called /tmp/metrics.input
as text value, and sends it to the standard output in Influx Line Protocol format.
The curated Telegraf configuration file telegraf.conf
:
# Send telegraf metrics to file(s)
[[outputs.file]]
## Files to write to, "stdout" is a specially handled file.
files = ["stdout", "/tmp/metrics.out"]
## Data format to output.
## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
data_format = "influx"
# Parse the new lines appended to a file
[[inputs.tail]]
## File names or a pattern to tail.
## These accept standard unix glob matching rules, but with the addition of
## ** as a "super asterisk". ie:
## "/var/log/**.log" -> recursively find all .log files in /var/log
## "/var/log/*/*.log" -> find all .log files with a parent dir in /var/log
## "/var/log/apache.log" -> just tail the apache log file
## "/var/log/log[!1-2]* -> tail files without 1-2
## "/var/log/log[^1-2]* -> identical behavior as above
## See https://github.com/gobwas/glob for more examples
##
# files = ["/var/mymetrics.out"]
files = ["/tmp/metrics.input"]
## Data format to consume.
## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
# data_format = "influx"
data_format = "value"
data_type = "string"
To run telegraf:
telegraf --config telegraf.conf
To write into the file manually:
echo "Hello, World!" >> /tmp/metrics.input
And here is a Bash script demo:
FILE_OUTPUT=/tmp/metrics.input
for fileNbr in {1..5}
do
# Erase the content of the file
echo -n "" > ${FILE_OUTPUT}
# Write a message every second for 10 seconds
for messageNbr in {1..10}
do
echo "message_${fileNbr}_${messageNbr}" >> ${FILE_OUTPUT}
sleep 1
done
done
The output should look like this:
2022-06-15T16:43:48Z I! Starting Telegraf 1.22.4
2022-06-15T16:43:48Z I! Loaded inputs: tail
2022-06-15T16:43:48Z I! Loaded aggregators:
2022-06-15T16:43:48Z I! Loaded processors:
2022-06-15T16:43:48Z I! Loaded outputs: file
2022-06-15T16:43:48Z I! Tags enabled: host=MyHost
2022-06-15T16:43:48Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"MyHost", Flush Interval:10s
tail,host=MyHost,path=/tmp/metrics.input value="Hello, World!" 1655311450007543601
tail,host=MyHost,path=/tmp/metrics.input value="message_1_1" 1655311469560168382
tail,host=MyHost,path=/tmp/metrics.input value="message_1_2" 1655311470563713078
tail,host=MyHost,path=/tmp/metrics.input value="message_1_3" 1655311471564995974
tail,host=MyHost,path=/tmp/metrics.input value="message_1_4" 1655311472566494242
tail,host=MyHost,path=/tmp/metrics.input value="message_1_5" 1655311473567657108
tail,host=MyHost,path=/tmp/metrics.input value="message_1_6" 1655311474568881514
tail,host=MyHost,path=/tmp/metrics.input value="message_1_7" 1655311475570046833
tail,host=MyHost,path=/tmp/metrics.input value="message_1_8" 1655311476572052497
tail,host=MyHost,path=/tmp/metrics.input value="message_1_9" 1655311477579071920
tail,host=MyHost,path=/tmp/metrics.input value="message_1_10" 1655311478583538569
tail,host=MyHost,path=/tmp/metrics.input value="message_2_1" 1655311479587399531
tail,host=MyHost,path=/tmp/metrics.input value="message_2_2" 1655311480592907369
tail,host=MyHost,path=/tmp/metrics.input value="message_2_3" 1655311481599178301
tail,host=MyHost,path=/tmp/metrics.input value="message_2_4" 1655311482603748573
tail,host=MyHost,path=/tmp/metrics.input value="message_2_5" 1655311483608296926
tail,host=MyHost,path=/tmp/metrics.input value="message_2_6" 1655311484614526181
tail,host=MyHost,path=/tmp/metrics.input value="message_2_7" 1655311485619593348
tail,host=MyHost,path=/tmp/metrics.input value="message_2_8" 1655311486623221357
tail,host=MyHost,path=/tmp/metrics.input value="message_2_9" 1655311487626948353
[...]
From there, you just have to play with Telegraf’s configuration to change its tags, format, etc. to your convenience.
Hope it helps!