tj26
tj26

Reputation: 353

telegraf and script command to send to grafana

I have the following inputs in my telegraf.conf that go to Grafana. I can get the simpler first 3 examples to work but cannot get the 4th to work.

[[inputs.exec]]
commands = ["sh -c 'grep -i DatasetVersion /etc/aaa/systemnameX/configfile | cut -d'=' -f2'"]
name_override = "systemnameX"
timeout = "5s"
data_format = "value"
data_type = "string"
[inputs.exec.tags]
type = "dataset_version"

[[inputs.exec]]
commands = ["sh -c 'cat /root/config/OS_Version.txt | tr -d C'"]
name_override = "systemnameX"
timeout = "5s"
data_format = "value"
data_type = "string"
[inputs.exec.tags]
type = "os_version"

[[inputs.exec]]
commands = ["sh -c 'grep -i VERSION= /usr/bin/sw_install | head -1 | cut -d'=' -f2 '"]
name_override = "systemnameX"
timeout = "5s"
data_format = "value"
data_type = "string"
[inputs.exec.tags]
type = "sw_version"

[[inputs.exec]]
commands = ["/sbin/ifconfig eth0 | grep 'inet' | cut -d: -f2 | awk '{print $2}'"]
name_override = "systemnameX"
timeout = "5s"
data_format = "value"
data_type = "string"
[inputs.exec.tags]
type = "ip_address"

This is the error when I run the telegraf test

2021-09-04T06:36:51Z E! Error in plugin [inputs.exec]: exec: exit status 1 for command '/sbin/ifconfig eth0 | grep 'inet' | cut -d: -f2 | awk '{print $2}''

and when I put the exact command into a command line I get the following error:

awk: cmd. line:1: {print
awk: cmd. line:1:       ^ unexpected newline or end of string

Any help about how to fix the awk part would be greatly appreciated.

cheers, Tara

Upvotes: 4

Views: 4882

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133428

I have tried running different permutations and combinations of your /sbin/ifconfig command but its giving me also same error. But I have read the telegraf manual and come up with following approach/steps.

I have tested this in Linux with Telegraf's 1.19.3 version and it worked fine for me.

Steps:

  • First thing first, don't waste your time(until someone posts an answer here), when we can create a script from your used commands in inputs.exec module of telegraf.
  • So in spite of running direct command in telegraf conf file create a script(eg: script.bash in my tested case) and place your command there like as follows:

cat /etc/telegraf/script.bash
#!/bin/bash
/sbin/ifconfig eth0 | grep 'inet' | cut -d: -f2 | awk '{print $2}'

OR you can also change your /sbin/ifconfig command to following in a single awk command:

/sbin/ifconfig eth0 | awk '/inet/{print $2}'
  • Now make following entry in your conf file:

[[inputs.exec]]
commands = ["/etc/telegraf/script.bash" ]
name_override = "systemnameX"
timeout = "5s"
data_format = "value"
data_type = "string"
[inputs.exec.tags]
type = "ip_address"

  • Restart your telegraf services once and test your status if your telegraf is operating fine or not by following command:

sudo -u telegraf telegraf -test -config /etc/telegraf/telegraf.conf

NOTE: I have created test script in /etc/telegraf/script.bash you can create it wherever you want to, but make sure you are giving absolute complete and correct path in conf file of telegraf.

Upvotes: 3

Related Questions