Using bash code in telegraf.conf

I’m trying to do something like this

    interfaces = ["${route -n | awk '$1 ~ / {print $NF}'}"]
$ sudo -u telegraf route -n | awk '$1 ~ / {print $NF}'

I tried to add INTERFACE=eth0 to /etc/default/telegraf with

interfaces = ["$INTERFACE"]

And restarted telegraf, but it doesn’t work. Telegraf is run as a service.
What am I doing wrong?

You can’t use bash script within a Telegraf configuration; you can, however, use environment variables. Documentation is available here.

If you’re on Debian or Ubuntu and you’ve installed from packages or a package manager, you can put environment variables in a /etc/default/telegraf file.

If you’re using Windows, make sure that you’re using system variables and that the Telegraf user can access them.

So I put INTERFACE=eth0 in /etc/default/telegraf

    interfaces = ["$INTERFACE"]
telegraf --input-filter net --test

* Plugin:, Collection 1
> net,,interface=all .......

What are the permissions on /etc/default/telegraf?

-rw-r--r-- 1 telegraf telegraf 16 Apr 5 09:03 /etc/default/telegraf

I’m experiencing this issue with a test VM. Let me investigate more and I’ll get back to you.

I think the issue might be that when running using --test, Telegraf doesn’t use the variables in /etc/default/telegraf. When I run Telegraf as a service I see the appropriate metrics being sent to InfluxDB.

I don’t see any data for this host if influxdb :frowning:
Though when I remove interfaces from configuration I see data coming from all interfaces.

I’m sorry, I’m not able to reproduce your issue. I am using Telegraf 1.5.2 on Ubuntu 16.04.3. Here are the steps I followed:

First, I got the interface name using the command you are using:

$ route -n | awk '$1 ~ / {print $NF}'

Then I added an environment variable to my configuration. Here are the relevant lines from my config:

$ cat /etc/telegraf/telegraf.conf | grep -B 5 \$INTERFACE
#   ## By default, telegraf gathers stats from any up interface (excluding loopback)
#   ## Setting interfaces will tell it to gather these explicit interfaces,
#   ## regardless of status.
#   ##
interfaces = ["$INTERFACE"]

I added the variable to /etc/default/telegraf. Here are the contents of the file and its permissions:

$ cat /etc/default/telegraf 
$ ls -l /etc/default/telegraf 
-rw-r--r-- 1 telegraf telegraf 17 Apr  5 10:15 /etc/default/telegraf

I am writing metrics to /tmp/metrics.out using the File output plugin. When I run Telegraf I see metrics for that specific interface:

$ cat /tmp/metrics.out | grep enp0s3
net,interface=enp0s3,host=scratchbox bytes_recv=175127i,packets_sent=1746i,err_in=0i,err_out=0i,drop_in=0i,drop_out=0i,bytes_sent=703715i,packets_recv=2159i 1522942350000000000

If you want to test using the --test argument, you’ll need to create and export an environment variable as follows:

$ INTERFACE=enp0s3
$ export INTERFACE
$ telegraf --input-filter net --test
2018/04/05 11:37:39 I! Using config file: /etc/telegraf/telegraf.conf
* Plugin:, Collection 1
> net,host=scratchbox,interface=enp0s3 drop_in=0i,drop_out=0i,bytes_sent=775291i,packets_sent=2097i,err_in=0i,err_out=0i,bytes_recv=205727i,packets_recv=2556i 1522942660000000000
> net,interface=all,host=scratchbox icmp_outparmprobs=0i,icmp_inechos=0i,ip_fragoks=0i,ip_forwdatagrams=0i,ip_reasmoks=0i,ip_fragcreates=0i,icmp_inparmprobs=0i,icmp_outerrors= [...]

Is there any more information you can provide?

1 Like

Keep in mind that when using --test, the metrics are not sent to the outputs and nothing is written to InfluxDB.


export INTERFACE with --test works for me as well.
Thanks for your time. I’ll try another approach.

I don’t know what to add more.
I try to achieve this with Ubuntu 14.04 and telegraf 1.5.3
Service is run with telegraf user

telegraf 22044     1  0 07:33 ?        00:00:05 /usr/bin/telegraf -pidfile /var/run/telegraf/ -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d`

There is this line in init script

cat /etc/default/telegraf

If I set
interfaces = ["eth0"], data is written to influxdb
If I set
interfaces = ["$INTERFACE"], it is not

I believe the problem is how environment variables are handled with init scripts. I do not think that adding DEFAULT=/etc/defualt/telegraf to the init script will work.

When you install telegraf from our repository, it’s set up as a systemd service. You can find the unit file here. The unit file has a line, EnvironmentFile=-/etc/default/telegraf, which is how Telegraf accesses the variables in that file. Since you are not running Telegraf as a systemd service, it won’t be able to access those variables.

Here are a few StackExchange posts about using environment variables with init files. Hopefully they are helpful:

The /etc/default/telegraf file should work with the sysvinit scripts, it is sourced when they are run.

I think that Ubuntu 14.04 uses upstart though, it’s possible that it’s not working correctly with it.

1 Like

Looks like if you are using sysvinit or upstart you need to export any variables you are using in the conf files:

export INTERFACE=eth0

This will be fixed in 1.6.0, you won’t need to use export.