Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run a CMD command in Inputs.Exec #6013

Closed
raider111111 opened this issue Jun 19, 2019 · 2 comments
Closed

Run a CMD command in Inputs.Exec #6013

raider111111 opened this issue Jun 19, 2019 · 2 comments

Comments

@raider111111
Copy link

Relevant telegraf.conf:

[[inputs.exec]]
name_prefix = "prod.compute_services.machine_metrics."
  ## Commands array
  commands = ["PowerShell -Command "(((C:\test\telegraf.exe --version) -split '\s+')[1] | ForEach-Object{$M = $_});$M.Remove($M.LastIndexOf('.'),1)| Write-Output | ForEach-Object{[float]$end = $_};$end""]
  interval = "30s"

  ## Timeout for each command to complete.
  timeout = "5s"

  ## measurement name suffix (for separating different commands)
  #name_suffix = "_mycollector"
  name_override = "TelegrafVersion"

  ## Data format to consume.
  ## Each data format has its own unique set of configuration options, read
  ## more about them here:
  ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
  data_format = "value"
  data_type = "float"

System info:

Telegraf 1.11.0 Windows 10

Steps to reproduce:

  1. Use inputs.exec to run a CMD command that calls PowerShell.

Expected behavior:

Runs the command and accepts the output, then sends the data to the monitoring appliance. This command works normally in CMD or when Telegraf calls it from a .bat file.

Actual behavior:

The Telegraf service doesn't start.

Additional info:

For security reasons we don't want to deploy out a .bat file for inputs.exec. Instead we would like to just place the CMD command inside the command block of inputs.exec.

@danielnelson
Copy link
Contributor

Try using triple quotes for your string, so that you can use the " inside to quote the powershell command:

commands = ['''PowerShell -Command "(((C:\test\telegraf.exe --version) -split '\s+')[1] | ForEach-Object{$M = $_});$M.Remove($M.LastIndexOf('.'),1)| Write-Output | ForEach-Object{[float]$end = $_};$end"''']

You might be interested in #5958 too.

@raider111111
Copy link
Author

That worked!

You're a busy guy, I see you all over the place!

Here's the output for anyone who's interested:

metrics.TelegrafVersion,host=COMPUTERNAME value=1.82 1560974282000000000

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants