Need help on EXEC plugin in windows

Hi All,

I am trying to run the below exec plugin in telegraf client on windows but after updating and restarting it telegraf is stopping with below error. but the powershell command which i am using is executing in the powershell and giving output without any issues.
please help me in configuring this.
Your help is highly appreciated.

ERROR:
E! [telegraf] Error running agent: Error parsing telegraf.conf, line 178: invalid TOML syntax

EXEC PLUGIN:

[[inputs.exec]]
commands = [" powershell Get-Counter -ErrorAction SilentlyContinue ‘\Process(*)% Processor Time’ | Select-Object -ExpandProperty countersamples| Select-Object -Property instancename, cookedvalue| ? {_.instanceName -notmatch "^(idle|_total|system)"} | Sort-Object -Property cookedvalue -Descending| Select-Object -First 5| ft InstanceName,@{L=‘CPU’;E={($_.Cookedvalue/100/$env:NUMBER_OF_PROCESSORS).toString(‘P’)}} -AutoSize "]
name_override = “top5CPUprocess”
data_format = “value”
data_type = “string”

Hi!

the call to powershell may require a full path name to the executable in order for telegraf to be able to locate the program itself. You can also try adding the path for powershell to the telegraf user’s PATH environment and see if that works as well.

dg

I would also recommend putting your powershell commands into a script file so you don’t need to deal with escaping the string.

The problem might be caused by the mixed quotes and double quotes.
Each value in the command array must be a string, so wrap it in single quotes

[[inputs.exec]]
commands = [
  'powershell -Command " _mycommand_" '
]

I think your TOML syntax is not valid because a single/double quote inside the command closes the opening single/double quote that opens the command “value” of the array.
I suggest to put the command in a script file (.ps1) and call it in the exec, which should look like the command below:

[[inputs.exec]]
commands = [
  'powershell -Command " _AbsolutePathToFile.ps1_" '
]

I will give you two tips to avoid this kind of problem in the future:

  • Always run the command from the cmd (which is what the exec plugin will do) to check if that’s correct. If possible impersonate the user used by telegraf.
  • before restarting the telegraf service process you can test the edited configuration by using:
_FullPath_\telegraf.exe --config _FullPath_\_MyConfig_.conf --test