Unable to pass through keys or fields to outputs.exec powershell script

Hi all, I have successfully set up snmp_traps for input that is basic

# Receive SNMP traps
  [[inputs.snmp_trap]]
    service_address = "udp://:162"
  ## Path to mib files
    path = ["C:/Program Files/InfluxData/telegraf/snmp/mibs"]
  ## Snmp version; one of "1", "2c" or "3".
    version = "2c"

And have configured outputs that produce results for my test switch in my lab

[[outputs.file]]
  #files = ["stdout","C:/Program Files/InfluxData/telegraf/output/stdout.out"]
  files = ["C:/Program Files/InfluxData/telegraf/output/stdout.out"]
  rotation_max_size = "50MB"
  rotation_max_archives = 4
  data_format = "influx"

# # Send telegraf metrics to file(s)
  [[outputs.file]]
    files = ["C:/Program Files/InfluxData/telegraf/output/snmptraps.out"]
	namepass = ["snmp_trap"]
    rotation_interval = "30d"
    rotation_max_size = "100MB"
    rotation_max_archives = 7

#   ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
    data_format = "influx"

Now I am trying to do something with this data, and that something is run a custom powershell script that will run other actions itself as a controller script.
I am able to execute the powershell script just fine, but I am not able to pass any keys or fields through from Telegraf into the script.

What am I missing to be able to get the outputs.exec script to pass through the snmp metrics keys and fields please, I would appreciate any help/method to get this accomplished?

I would like the metrics from telegraf/plugins/inputs/snmp_trap at master · influxdata/telegraf · GitHub to be passed through as individual arguments or parameters ideally, or anything i can get to be honest. I cant seem to pass anything through.

snmp_trap

    tags:
        source (string, IP address of trap source)
        name (string, value from SNMPv2-MIB::snmpTrapOID.0 PDU)
        mib (string, MIB from SNMPv2-MIB::snmpTrapOID.0 PDU)
        oid (string, OID string from SNMPv2-MIB::snmpTrapOID.0 PDU)
        version (string, "1" or "2c" or "3")
        community (string, value from 1 or 2c trap)
    fields:
        Fields are mapped from variables in the trap. Field names are the trap variable names after MIB lookup. Field values are trap variable values.

What I have tried:

# # Send metrics to command as input over stdin
[[outputs.exec]]
  command = ["powershell.exe", "-File", "C:/Program Files/InfluxData/telegraf/scripts/output_to_file.ps1"]

#  command = ["powershell.exe", "-File", "C:/Program Files/InfluxData/telegraf/scripts/output_to_file.ps1", "--requestID", "{{.requestID}}", "--value", "{{.Value}}", "--name", "{{.Name}}"]
#  data_format = "json"
  data_format = "influx"

And my testing powershell script, that does execute and returns nothing in any of the params or args

# Define input variables
param (
	[string]$requestID,
	[string]$value,
	[string]$name
)

# Define the output file path
$outputFile = "C:\Program Files\InfluxData\telegraf\output\output.txt"

# Write the variables to the output file, each on a new line
Add-Content -Path $outputFile -Value "requestID: $requestID"
Add-Content -Path $outputFile -Value "value: $value"
Add-Content -Path $outputFile -Value "name: $name"
Add-Content -Path $outputFile -Value "args0: $args[0]"
Add-Content -Path $outputFile -Value "args0: $args[1]"
Add-Content -Path $outputFile -Value "args0: $args[2]"
Add-Content -Path $outputFile -Value "==================================================================================="

# Output a message indicating success
Write-Host "Variables have been written to $outputFile"

Toggling one of the PSU inputs on the switch I set up for testing nets me this in the snmptraps.out file

snmp_trap,community=public,host=telegraf,mib=MELLANOX-EFM-MIB,name=systemHealthStatus,oid=.1.3.6.1.4.1.33049.2.1.2.7,source=192.168.7.248,version=2c sysUpTime.0=323567866i,invHealthStatus.1="Power Supply 2 is unresponsive " 1724813205442766400
snmp_trap,community=public,host=telegraf,mib=MELLANOX-EFM-MIB,name=systemHealthStatus,oid=.1.3.6.1.4.1.33049.2.1.2.7,source=192.168.7.248,version=2c sysUpTime.0=323576551i,invHealthStatus.1="Power Supply 2 has been restored to its normal state " 1724813292298286600

And an example debug log

2024-08-28T02:48:03Z I! Starting Telegraf 1.30.3 brought to you by InfluxData the makers of InfluxDB
2024-08-28T02:48:03Z I! Available plugins: 233 inputs, 9 aggregators, 31 processors, 24 parsers, 60 outputs, 5 secret-stores
2024-08-28T02:48:03Z I! Loaded inputs: snmp_trap
2024-08-28T02:48:03Z I! Loaded aggregators: 
2024-08-28T02:48:03Z I! Loaded processors: 
2024-08-28T02:48:03Z I! Loaded secretstores: 
2024-08-28T02:48:03Z I! Loaded outputs: exec file (2x)
2024-08-28T02:48:03Z I! Tags enabled: host=telegraf
2024-08-28T02:48:03Z W! Deprecated inputs: 0 and 1 options
2024-08-28T02:48:03Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"telegraf", Flush Interval:10s
2024-08-28T02:48:03Z D! [agent] Initializing plugins
2024-08-28T02:48:03Z D! [agent] Connecting outputs
2024-08-28T02:48:03Z D! [agent] Attempting connection to [outputs.file]
2024-08-28T02:48:03Z D! [agent] Successfully connected to outputs.file
2024-08-28T02:48:03Z D! [agent] Attempting connection to [outputs.file]
2024-08-28T02:48:03Z D! [agent] Successfully connected to outputs.file
2024-08-28T02:48:03Z D! [agent] Attempting connection to [outputs.exec]
2024-08-28T02:48:03Z D! [agent] Successfully connected to outputs.exec
2024-08-28T02:48:03Z D! [agent] Starting service inputs
2024-08-28T02:48:03Z I! [inputs.snmp_trap] Listening on udp://:162
2024-08-28T02:48:12Z D! [inputs.snmp_trap] Packet sanity verified, we got all the bytes (152)
2024-08-28T02:48:12Z D! [inputs.snmp_trap] parseRawField: version
2024-08-28T02:48:12Z D! [inputs.snmp_trap] Parsed version 1
2024-08-28T02:48:12Z D! [inputs.snmp_trap] parseRawField: community
2024-08-28T02:48:12Z D! [inputs.snmp_trap] Parsed community public
2024-08-28T02:48:12Z D! [inputs.snmp_trap] UnmarshalPayload Meet PDUType 0x534e4d50763254726170. Offset 14
2024-08-28T02:48:12Z D! [inputs.snmp_trap] getResponseLength: 138
2024-08-28T02:48:12Z D! [inputs.snmp_trap] parseRawField: request id
2024-08-28T02:48:12Z D! [inputs.snmp_trap] requestID: 1520772320
2024-08-28T02:48:12Z D! [inputs.snmp_trap] parseRawField: error-status
2024-08-28T02:48:12Z D! [inputs.snmp_trap] errorStatus: 0
2024-08-28T02:48:12Z D! [inputs.snmp_trap] parseRawField: error index
2024-08-28T02:48:12Z D! [inputs.snmp_trap] error-index: 0
2024-08-28T02:48:12Z D! [inputs.snmp_trap] vblLength: 123
2024-08-28T02:48:12Z D! [inputs.snmp_trap] parseRawField: OID
2024-08-28T02:48:12Z D! [inputs.snmp_trap] OID: .1.3.6.1.2.1.1.3.0
2024-08-28T02:48:12Z D! [inputs.snmp_trap] decodeValue: type is TimeTicks
2024-08-28T02:48:12Z D! [inputs.snmp_trap] decodeValue: value is 0x134962e7
2024-08-28T02:48:12Z D! [inputs.snmp_trap] parseRawField: OID
2024-08-28T02:48:12Z D! [inputs.snmp_trap] OID: .1.3.6.1.6.3.1.1.4.1.0
2024-08-28T02:48:12Z D! [inputs.snmp_trap] decodeValue: type is ObjectIdentifier
2024-08-28T02:48:12Z D! [inputs.snmp_trap] parseRawField: OID
2024-08-28T02:48:12Z D! [inputs.snmp_trap] decodeValue: value is ".1.3.6.1.4.1.33049.2.1.2.7"
2024-08-28T02:48:12Z D! [inputs.snmp_trap] parseRawField: OID
2024-08-28T02:48:12Z D! [inputs.snmp_trap] OID: .1.3.6.1.4.1.33049.2.2.1.1.1.1.7.1
2024-08-28T02:48:12Z D! [inputs.snmp_trap] decodeValue: type is OctetString
2024-08-28T02:48:12Z D! [inputs.snmp_trap] decodeValue: value is []byte{0x50, 0x6f, 0x77, 0x65, 0x72, 0x20, 0x53, 0x75, 0x70, 0x70, 0x6c, 0x79, 0x20, 0x32, 0x20, 0x68, 0x61, 0x73, 0x20, 0x62, 0x65, 0x65, 0x6e, 0x20, 0x72, 0x65, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x69, 0x74, 0x73, 0x20, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x20, 0x73, 0x74, 0x61, 0x74, 0x65, 0x20}
2024-08-28T02:48:13Z D! [outputs.file] Wrote batch of 1 metrics in 242.2µs
2024-08-28T02:48:13Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2024-08-28T02:48:13Z D! [outputs.file] Wrote batch of 1 metrics in 0s
2024-08-28T02:48:13Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2024-08-28T02:48:14Z D! [outputs.exec] Wrote batch of 1 metrics in 326.8295ms
2024-08-28T02:48:14Z D! [outputs.exec] Buffer fullness: 0 / 10000 metrics

Any help to get me unstuck would be much appreciated.
Cheers!

@snackattack as the first sentence of the README says:

This plugin sends telegraf metrics to an external application over stdin.

:slight_smile:
So you need to read the metric from stdin in the powershell script, it is NOT passed as a paramater. Furthermore, please note that the metric is in the format you specified (line-protocol in your current setting) and you need to parse this in the script…

Out of interest, what are you trying to do in the script?

Thank you so much!
I was so busy troubleshooting that I didnt notice that line at all.

It is now working, rest is the same but I changed the powershell script for testing to

# Define the output file path
$outputFile = "C:\Program Files\InfluxData\telegraf\output\output.txt"

# Example of reading from stdin using $input
$input | ForEach-Object {
    # Process each line of input
    Add-Content -Path $outputFile -Value "Received: $_"
}
Add-Content -Path $outputFile -Value "==================================================================================="

# Output a message indicating success
Write-Host "Variables have been written to $outputFile"

The purpose of this script is going to be for creating incidents in ITSM amongst other features.
Cheers!

1 Like