Kapacitor: How can I pass arguments when execute a script

Created on 21 Jul 2016  路  5Comments  路  Source: influxdata/kapacitor

Hi,
I want to pass arguments to the script, when an alert is triggered, and then I use exec function,
I found the follew description in official document: "Execute a command whenever an alert is triggered and pass the alert data over STDIN in JSON format.".
But I don't know how to apply.
The arguments may be the alert data, such as "Message" "Name" "Details".
Thanks!

Most helpful comment

@jommgoncalves The JSON is sent over STDIN to the script. In the case of bash the easiest way to read from STDIN is to use the cat program.

For example:

#!/bin/bash

cat >> /tmp/alert.json

This script will read the JSON from STDIN and write it to the /tmp/alert.json file.

Also since the data is JSON I personally like using the jq tool that will also read from STDIN and then allow you to select specific JSON data.

This script uses jq to grab just the alert message and write it to a file.

#!/bin/bash

jq .message >> /tmp/alert.msg.txt

If you are not using bash then look up how to read from STDIN for your language of choice.

All 5 comments

@ygtzf You can pass static args to your script like:

.exec('/path/to/script', 'arg1', 'arg2') but these cannot be the Message, Name etc.

To get the Message, Name etc simply read from STDIN and decode the data as JSON.

All the relevant alert data is contained in the JSON.

OK. Thanks very much for your reply quickly.
I will try as your comment.

@nathanielc You say "All the relevant alert data is contained in the JSON." but how can I send this JSON?

Please can you show me an example?

@jommgoncalves The JSON is sent over STDIN to the script. In the case of bash the easiest way to read from STDIN is to use the cat program.

For example:

#!/bin/bash

cat >> /tmp/alert.json

This script will read the JSON from STDIN and write it to the /tmp/alert.json file.

Also since the data is JSON I personally like using the jq tool that will also read from STDIN and then allow you to select specific JSON data.

This script uses jq to grab just the alert message and write it to a file.

#!/bin/bash

jq .message >> /tmp/alert.msg.txt

If you are not using bash then look up how to read from STDIN for your language of choice.

@nathanielc thank you for your response.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

evan-ty picture evan-ty  路  5Comments

sslupsky picture sslupsky  路  3Comments

haowells picture haowells  路  6Comments

davidhiendl picture davidhiendl  路  6Comments

juise picture juise  路  7Comments