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!
@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.
Most helpful comment
@jommgoncalves The JSON is sent over STDIN to the script. In the case of
bashthe easiest way to read from STDIN is to use thecatprogram.For example:
This script will read the JSON from STDIN and write it to the
/tmp/alert.jsonfile.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
jqto grab just the alert message and write it to a file.If you are not using bash then look up how to read from STDIN for your language of choice.