Telegraf exec plugin with bash script

telegraf

#1

Hi,

I paste some part of my config:

[agent]
  interval = "60s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "2s"
  precision = ""
  debug = false
  quiet = true
  logfile = ""

[[outputs.kafka]]
  brokers = ["servers"]
  topic = "telegraf-measurements-json"
  client_id = "telegraf-hardware-measurements"
  routing_tag = "host"
  version = "0.11.0.2"
  compression_codec = 2
  required_acks = 1
  data_format = "json"

[[inputs.exec]]
commands = ["/etc/infra-telegraf/telegraf_spawningtest.sh"]
name_suffix = "_spawningtest"
data_format = "json"

This is the script that I want to run:

 #!/bin/bash
 LINTIME=$(cat /var/tmp/linspawntime)
 WINTIME=$(cat /var/tmp/winspawntime)
 LINDELETETIME=$(cat /var/tmp/lindeletetime)
 WINDELETETIME=$(cat /var/tmp/windeletetime)
 LINSUCCESS=$(cat /var/tmp/linsuccesscounter)
 WINSUCCESS=$(cat /var/tmp/winsuccesscounter)
 LINFAILED=$(cat /var/tmp/linfailedcounter)
 WINFAILED=$(cat /var/tmp/winfailedcounter)
 SUMSUCCESS=$(cat /var/tmp/sumsuccesscounter)
 LINSPAWNSUCCESS=$(cat /var/tmp/lss)
 WINSPAWNSUCCESS=$(cat /var/tmp/wss)
 LINSPAWNFAILED=$(cat /var/tmp/lsf)
 WINSPAWNFAILED=$(cat /var/tmp/wsf)
 LINDELETESUCCESS=$(cat /var/tmp/lds)
 WINDELETESUCCESS=$(cat /var/tmp/wds)
 LINDELETEFAILED=$(cat /var/tmp/ldf)
 WINDELETEFAILED=$(cat /var/tmp/wdf)

    echo "spawntest,linspawntime=$LINTIME winspawntime=$WINTIME lindeletetime=$LINDELETETIME windeletetime=$WINDELETETIME linsuccesscounter=$LINSUCCESS winsuccesscounter=$WINSUCCESS linfailedcounter=$LINFAILED winfailedcounter=$WINFAILED sumsuccesscounter=$SUMSUCCESS lss=$LINSPAWNSUCCESS wss=$WINSPAWNSUCCESS lsf=$LINSPAWNFAILED wsf=$WINSPAWNFAILED lds=$LINDELETESUCCESS wds=$WINDELETESUCCESS ldf=$LINDELETEFAILED wdf=$WINDELETEFAILED"

The values inside the files are integer simple number values like 34, 1,0
How can I send these values?
I don’t even sure that I should send as json.


#2

I can’t run this to test, as those /var/tmp/ files are unique to your system.

Your echo statement in the script outputs line protocol, not JSON.

echo "spawntest,linspawntime=$LINTIME winspawntime=$WINTIME lindeletetime=$LINDELETETIME windeletetime=$WINDELETETIME linsuccesscounter=$LINSUCCESS winsuccesscounter=$WINSUCCESS linfailedcounter=$LINFAILED winfailedcounter=$WINFAILED sumsuccesscounter=$SUMSUCCESS lss=$LINSPAWNSUCCESS wss=$WINSPAWNSUCCESS lsf=$LINSPAWNFAILED wsf=$WINSPAWNFAILED lds=$LINDELETESUCCESS wds=$WINDELETESUCCESS ldf=$LINDELETEFAILED wdf=$WINDELETEFAILED"

So if you change

data_format = "json"

to

data_format = "influx"

It will hopefully work.


#3

I got this
2019-02-08T08:54:00Z E! [inputs.exec]: Error in plugin: metric parse error: expected timestamp at offset 42: "spawntest,linspawntime=27 winspawntime=55 lindeletetime=13 windeletetime=17 linsuccesscounter=6055 winsuccesscounter=6050 linfailedcounter= winfailedcounter= sumsuccesscounter= lss=1 wss=1 lsf=0 wsf=0 lds=1 wds=1 ldf=0 wdf=0\n"


#4

Drats, I didn’t see those spaces. Change to:

echo "spawntest,linspawntime=$LINTIME,winspawntime=$WINTIME,lindeletetime=$LINDELETETIME,windeletetime=$WINDELETETIME,linsuccesscounter=$LINSUCCESS winsuccesscounter=$WINSUCCESS,linfailedcounter=$LINFAILED,winfailedcounter=$WINFAILED sumsuccesscounter=$SUMSUCCESS,lss=$LINSPAWNSUCCESS,wss=$WINSPAWNSUCCESS,lsf=$LINSPAWNFAILED,wsf=$WINSPAWNFAILED,lds=$LINDELETESUCCESS,wds=$WINDELETESUCCESS,ldf=$LINDELETEFAILED,wdf=$WINDELETEFAILED"

This will tread all of these as tags, which means they are indexed.

if you don’t require that, adding a single space before using comma separated fields.

Docs on our line protocol are here.


#5

This is the exec now and this is the shell script:

[[inputs.exec]]
commands = ["/etc/infra-telegraf/telegraf_spawningtest.sh"]
name_suffix = "_spawningtest"
data_format = "influx"

This is the script:

#!/bin/bash
LINTIME=$(cat /var/tmp/linspawntime)
WINTIME=$(cat /var/tmp/winspawntime)
LINDELETETIME=$(cat /var/tmp/lindeletetime)
WINDELETETIME=$(cat /var/tmp/windeletetime)
LINSPAWNSUCCESS=$(cat /var/tmp/lss)
WINSPAWNSUCCESS=$(cat /var/tmp/wss)
LINSPAWNFAILED=$(cat /var/tmp/lsf)
WINSPAWNFAILED=$(cat /var/tmp/wsf)
LINDELETESUCCESS=$(cat /var/tmp/lds)
WINDELETESUCCESS=$(cat /var/tmp/wds)
LINDELETEFAILED=$(cat /var/tmp/ldf)
WINDELETEFAILED=$(cat /var/tmp/wdf)

echo "spawntest,linspawntime=$LINTIME,winspawntime=$WINTIME,lindeletetime=$LINDELETETIME,windeletetime=$WINDELETETIME,lss=$LINSPAWNSUCCESS,wss=$WINSPAWNSUCCESS,lsf=$LINSPAWNFAILED,wsf=$WINSPAWNFAILED,lds=$LINDELETESUCCESS,wds=$WINDELETESUCCESS,ldf=$LINDELETEFAILED,wdf=$WINDELETEFAILED"

If I run a debug I get this:

infra-telegraf -config=/etc/infra-telegraf/infra-telegraf.conf  -input-filter=exec -debug
2019-02-12T06:09:54Z I! Starting Telegraf 1.9.2
2019-02-12T06:10:00Z E! [inputs.exec]: Error in plugin: metric parse error: expected tag at offset 123: "spawntest,linspawntime=33,winspawntime=53,lindeletetime=31,windeletetime=14,lss=1,wss=1,lsf=0,wsf=0,lds=1,wds=1,ldf=0,wdf=0\n"

How it works at the moment if I specify like this all values:

#[[inputs.exec]]
#commands = ["cat /var/tmp/linspawntime"]
#name_override = "linux_spawn_time"
#data_format = "value"
#data_type = "integer"
#
#[[inputs.exec]]
#commands = ["cat /var/tmp/winspawntime"]
#name_override = "windows_spawn_time"
#data_format = "value"
#data_type = "integer"
#
#[[inputs.exec]]
#commands = ["cat /var/tmp/lindeletetime"]
#name_override = "linux_delete_time"
#data_format = "value"
#data_type = "integer"
#
#[[inputs.exec]]
#commands = ["cat /var/tmp/windeletetime"]
#name_override = "windows_delete_time"
#data_format = "value"
#data_type = "integer"
#
#[[inputs.exec]]
#commands = ["cat /var/tmp/lss"]
#name_override = "linux_spawn_success"
#data_format = "value"
#data_type = "integer"
#
#[[inputs.exec]]
#commands = ["cat /var/tmp/wss"]
#name_override = "windows_spawn_success"
#data_format = "value"
#data_type = "integer"

But this one not too pretty I think.