Tai Do
Tai Do

Reputation: 177

How to echo variable env in telegraf [[input.exec]]

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.

Upvotes: 0

Views: 1189

Answers (2)

Mohammad Sohail
Mohammad Sohail

Reputation: 1

I had the same issue where I wanted to implement environment variable in bash script under inputs.exec output plugin of Telegraf. Following was my bash command

[[inputs.exec]]

commands = ["/usr/bin/bash -c 'response=$(ssh "$MY_IP_ADDRESS" "kubectl exec -n services my-shared-kafka-kafka-0 -- curl -sS -G \"http://vmselect-vms.sysmgmt-health.svc.cluster.local:8481/select/0/prometheus/api/v1/query\" --data-urlencode \"query=(avg(rate(container_cpu_usage_seconds_total{namespace=\\\"services\\\", pod=~\\\"mycontainer.\\\", container=\\\"mycontainer\\\"})) by (instance,job) / avg(container_spec_cpu_quota{namespace=\\\"services\\\", pod=~\\\"mycontainer.\\\", container=\\\"mycontainer\\\"} / container_spec_cpu_period{namespace=\\\"services\\\", pod=~\\\"mycontainer.*\\\", container=\\\"mycontainer\\\"}) by (instance,job)) * 100\"");'"]

In the above command, $MY_IP_ADDRESS is the environment variable. I set this up in .bashrc file like as follows

export MY_IP_ADDRESS="100.252.1.15"

When we run the config manually using “telegraf --config my_disk.conf”, it inherits the shell environment set in .bashrc, including MY_IP_ADDRESS, and the we get the desired results.

But when we place the my_disk.conf in /etc/telegraf/telegraf.d folder and Telegraf runs as a systemd service, it doesn’t inherit the shell variables, and throws the following error.

Could not resolve hostname : Name or service not known

To resolve this issue I followed following steps.

  1. Opened the telegraf systemD config

systemctl edit --full telegraf

  1. And looked for EnvironmentFile under [Service]

[Service] Type=notify

EnvironmentFile=-/etc/default/telegraf

  1. Edited the telegraf environment file by adding the environment variable

MY_IP_ADDRESS="100.252.1.15"

  1. Saved the file and restarted daemon and telegrag systemD file.

systemctl daemon-reexec

systemctl start telegraf

  1. Checked the output plugins and it worked.

Upvotes: -1

morbeo
morbeo

Reputation: 97

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

Upvotes: 0

Related Questions