Not able to send a input.exec script to influxdb

Hi,

I am tring to send via telegraf.conf a shell script result to influxdb but I am not able to do that.
This is the result of the script
$:/home/monitor# ./gateway_connections.sh
[
{ “connections”: 2056, “gateway”: }
]

This is the telegraf.conf input:
[[inputs.exec]]
commands = [ “/home/monitor/gateway_connections.sh” ]
timeout = “5s”
name_override = “connect”
name_suffix = “”
data_format = “json”
tag_keys = [ “gateway” ]

This is the error running telegraf debug
[inputs.exec] Error in plugin: exec: fork/exec /home/monitor/gateway_connections.sh: exec format error for command ‘/home/monitor/gateway_connections.sh’:

I am new to this and sorry it would be simple to fix.

Hi,

Your output from the gateway connections script is not valid JSON, the gateway is missing a value. I tried to further reproduce with the following: I used the following shell script:

#!/bin/bash
echo '[ { "connections": 2056, "gateway": "hostname"} ]'

and this config:

[agent]
  omit_hostname = true

[[outputs.file]]

[[inputs.exec]]
  commands = ["/home/powersj/telegraf/test.sh"]
  name_override = "connect"
  data_format = "json"
  tag_keys = [ "gateway" ]

and got this output:

connect,gateway=hostname connections=2056 1648473758000000000

Hopefully that helps, but let us know if you are still running into issues.

1 Like

Hi,

Tks for the answer … I got the same error using the model script.
If you can take a look …

"gateway_connections.sh" 4L, 71C written
root@virt27:/etc/telegraf# ./gateway_connections.sh
[ { "connections": 2056, "gateway": "hostname"} ]
root@virt27:/etc/telegraf# cat gateway_connections.sh

#!/bin/bash
echo '[ { "connections": 2056, "gateway": "hostname"} ]'

root@virt27:/etc/telegraf# telegraf -test -config-directory /etc/telegraf/telegraf.d/ -input-filter=exec -debug
2022-03-28T16:07:01Z I! Starting Telegraf 1.20.3
2022-03-28T16:07:01Z I! Using config file: /etc/telegraf/telegraf.conf
2022-03-28T16:07:01Z D! [agent] Initializing plugins
2022-03-28T16:07:01Z D! [agent] Starting service inputs
2022-03-28T16:07:01Z E! [inputs.exec] Error in plugin: exec: fork/exec /etc/telegraf/gateway_connections.sh: exec format error for command '/etc/telegraf/gateway_connections.sh': 
2022-03-28T16:07:01Z D! [agent] Stopping service inputs
2022-03-28T16:07:01Z D! [agent] Input channel closed
2022-03-28T16:07:01Z D! [agent] Stopped Successfully
2022-03-28T16:07:01Z E! [telegraf] Error running agent: input plugins recorded 1 errors

Here is the telegraf.conf

# Telegraf Configuration
[global_tags]
  environment="virt"
  hostname="xxxx"
  host_type="physicalmachine"
  cluster = "production"
[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  debug = false
  quiet = false
  logfile = ""
  hostname = "xxxx"
  omit_hostname = true
[[outputs.influxdb]]
  urls = ["http://myinflux:8086"]
  database = "telegraf"
  retention_policy = ""
  write_consistency = "any"
  timeout = "5s"
[[outputs.file]]
[[inputs.socket_listener]]
  service_address = "tcp://:8094"
[[inputs.cpu]]
  percpu = true
  totalcpu = true
  collect_cpu_time = false
[[inputs.disk]]
  ignore_fs = ["tmpfs", "devtmpfs", "devfs"]
[[inputs.diskio]]
  # no config
[[inputs.kernel]]
  # no config
[[inputs.mem]]
  # no config
[[inputs.processes]]
  # no config
[[inputs.swap]]
  # no config
[[inputs.system]]
  # no config
[[inputs.net]]
  # no config
[[inputs.netstat]]
  # no config
[[inputs.temp]]
[[inputs.hddtemp]]
[[inputs.exec]]
  commands = [ "/etc/telegraf/gateway_connections.sh" ]
  name_override = "connect"
  data_format = "json"
  tag_keys = [ "gateway" ]
[[inputs.dns_query]]
  servers = ["IP1", "IP2"]
  network = "udp"
  record_type = "A"
  domains = ["sites"]
  port = 53
  timeout = 2

This is still with error and not able to send to influxdb.
Thanks once more for the support.

Per this page it sounds like it may be caused due to your shebang not being the first time. In your cat output, it does look like your first line is empty. You should move the #!/bin/bash up one line.

1 Like

With you support I managed to send the data to influxdb using the echo script.
It was very helpfull
The point is that my script is very complex but works locally, when tested with telegraf debug but when I check the influxdb the value of my metric is always 0.
I manage to send the data creating a .json file and using a cat command. Now I have the right data but the script runs just once.
I do not know if is there a way to ask telegraf.conf to run the script again after some time.

Here is the script

#!/bin/bash

#output1=$(docker ps -q --filter "name=gateway-public")
 
#output2=$(docker exec $output1 netstat -tapn |wc -l)
i=1
while [ "$i" -ne 0 ]
do
output2=$(docker exec $(docker ps -q --filter "name=gateway-public") netstat -tapn |wc -l)
output3="{\"connections\": $output2, \"gateway\": \"hostname\"}"
echo $output3 > output.json
cat output.json
sleep 10
done

Here the telegraf.conf I am using now

[[inputs.exec]]
  commands = [ "/etc/telegraf/gateway_connections_old.sh", "cat /etc/telegraf/output.json" ]
  timeout = "5s"
  name_override = "connect"
  data_format = "json"
  tag_keys = [ "gateway" ]

I am using the nohup command to run the script and update the output.json but it is not the better way to do that.

Once more, thank you very much for all the support.

I am not a bash expert, does this bash script runs infinitely? What is the purpose of the sleep 10 line?
I didn’t quite understand the details of the bash script, but be aware of the difference between these two telegraf input plugins:

[[inputs.exec]]
[[inputs.execd]]
2 Likes