Inputs.exec - how can it be used to get data

Hi,
I have a problem with executing an command to get the output into grafana.

For background, I have a router and want to monitor the cpu and memory usage for that router.
Unfortunatly the SNMP requests have no single command for getting cpu or memory usage.
There is only an command thats listing me variuos information and the cpu & memory usage.

If I run

snmpwalk -v2c -c snmp 192.168.x.x SNMPv2-MIB::sysDescr.0

the output is:

SNMPv2-MIB::sysDescr.0 = STRING: DrayTek Corporation, Router Model: Vigor165 Series, Version: 4.0.2_STD, Build Date/Time:Jan 21 2019 11:27:27, CPU Usage: 2%, Memory Usage:66%

Now I used the following command to extract only the the value for the cpu usage:

“snmpwalk -v2c -c snmp 192.168.x.x SNMPv2-MIB::sysDescr.0 | awk ‘BEGIN { FS = “,” } ; {print $5}’ | awk ‘{ print $3 }’ | cut -d ‘%’ -f 1”

the output is: 2
and for memory:

“snmpwalk -v2c -c snmp 192.168.x.x SNMPv2-MIB::sysDescr.0 | awk ‘BEGIN { FS = “,” } ; {print $6}’ | awk ‘BEGIN { FS = “:” } ; {print $2}’ | cut -d ‘%’ -f 1”

the output is: 66

The question for me is now, how can I use that information that it can be imported into grafana.

My thought was, I use the “inputs.exec” command but unfortunatly that is not working.
I tried either

[[inputs.exec]]
commands = [“sh -c ‘snmpwalk -v2c -c snmp 192.168.x.x SNMPv2-MIB::sysDescr.0 | awk ‘BEGIN { FS = “,” } ; {print $5}’ | awk ‘{ print $3 }’ | cut -d ‘%’ -f 1’”]
timeout = “4s”
name_override = “CPU_router”
name_suffix = “router_cpu”
[inputs.exec.tags]
metrics_source = “CPU”
data_format = “value”
data_type = “integer”

or

[[inputs.exec]]
commands = [“/home/thomas/cpu.sh”]

(the script contains the command mentioned above and is working)
but that doesn’t work.

The output is either:
(command)

[inputs.exec]: Error in plugin: exec: exec: “}”: executable file not found in $PATH for command ’ } ; {print $5}’ | awk ‘{ print $3 }’ | cut -d ‘%’ -f 1’':
2019-11-09T20:13:14Z E! [inputs.exec]: Error in plugin: exec: exit status 127 for command 'sh -C 'snmpwalk -v2c -c snmp 192.168.x.x SNMPv2-MIB::sysDescr.0 | awk 'BEGIN { FS = ': sh: 0: Can’t open snmpwalk -v2c -c snmp 192.168.x.x SNMPv2-MIB::sysDescr.0 | awk BEGIN

or
(script)

[inputs.exec]: Error in plugin: must be an object or an array of objects

hopefully some of you can help me to solve my issue.

Thanks.

Hello @chavell3,
Thanks for your question!

This may be related to the Telegraf service running as a different user. The official packages run Telegraf as the telegraf user and group on Linux systems.

I haven’t used the exec plugin before with the ‘data_format=value’, can you please share where you got the ‘metrics_source’ parameter from?

Alternatively, I recommend trying ‘data_format=“influx”’ where the output of your cpu.sh is in line protocol, e.g. `measurement,tag1=a,tag2=b field=42’ instead of just a value.
Here’s a blog with an example of how the exec plugin is used.

I also recommend setting debug=true when you have problems with telegraf.

Hi Anaisdg,

Thanks for your reply.
well
 the data format I got from:
telegraf/plugins/inputs/exec at master · influxdata/telegraf · GitHub and then
telegraf/DATA_FORMATS_INPUT.md at master · influxdata/telegraf · GitHub

As from your “suggestion” I removed ‘metrics_source’(actually I don’t know where I got that from) and now, I don’t have the problem with the “array of objects” anymore and the script works fine.
well after I corrected the file permission
 that was one issue too, I hadn’t looked at before.
but I left the “data_format = value” as from the link

in that case I don’t need to care how to format the output :stuck_out_tongue:

THANKS!!

1 Like