0

I have a variable env and I want to get it in telegraf. I use [[inputs.exec]]

My code:

[[inputs.exec]]
commands = ["echo $val_env"]
data_format = "value"
data_type = "string"
name_override = "value_A"

But I can't get this value $val_env. I also test another way such as [echo "${val_env}"] or ["$val_env"], ... but as same.

How I can get value variable env in commands [[input.exec]] telegraf.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Tai Do
  • 117
  • 1
  • 8

1 Answers1

0

The variable val_env has to be exported to the environment that is running telegraf.

Here's an example, with most of the noise removed for brevity, and I've added to my config exactly what you described:

$ grep '^\[\[inputs.exec' -A4 /etc/telegraf/telegraf.conf
[[inputs.exec]]
commands = ["echo $val_env"]
data_format = "value"
data_type = "string"
name_override = "value_A"

$ export val_env='Tai Do'

$ telegraf
2022-12-22T11:51:31Z I! Using config file: /etc/telegraf/telegraf.conf
2022-12-22T11:51:31Z I! Starting Telegraf 1.25.0
2022-12-22T11:51:31Z I! Available plugins: 227 inputs, 9 aggregators, 26 processors, 21 parsers, 57 outputs, 2 secret-stores
2022-12-22T11:51:31Z I! Loaded inputs: cpu disk diskio exec kernel mem processes swap system
2022-12-22T11:51:31Z I! Loaded aggregators:
2022-12-22T11:51:31Z I! Loaded processors:
2022-12-22T11:51:31Z I! Loaded secretstores:
2022-12-22T11:51:31Z I! Loaded outputs: exec file
...
value_A,host=***,user=root value="Tai Do" 1671709900000000000
morbeo
  • 87
  • 4
  • I wrote it in /etc/telegraf/telegraf.conf and the val_env was exported. I "echo value" outside and I can read it. But config inside telegraf. conf and I run (service telegraf restart). I can't read it – Tai Do Dec 23 '22 at 02:25
  • Since you have to export in the process environment, it all depends on how you start `telegraf`. Your question does not specify that. If it is systemd - [something like this](https://serverfault.com/questions/413397/how-to-set-environment-variable-in-systemd-service). If it is something else - you have to specify how you start it. – morbeo Dec 23 '22 at 11:29
  • Hi morbeo. I config in telegraf file and run it. I'm researching more and don't resolve this problem – Tai Do Dec 26 '22 at 01:16