Webhook action in watcher to use environment variables

alerting

(Vinod Kannan) #1

Hi,

This is the webhook action I am using:

"actions" : {
"web-post" : {
"webhook": {
"method" : "POST",
"host": "x.x.x.x",
"port": 8090,
"path": ":/notification",
"headers": {
"Content-Type": "application/json"
},
"body": "{{#toJson}}ctx.payload.hits{{/toJson}}"
}
}
}

While the same code needs to be used in different environments, how can I make the host and port parameterized and use environment variables. something like " host => [${HOST}] " in logstash.

Thanks
Vinod


(Guillaume Dufrenne) #2

If you want to re-use the same code in many differents watch you have to use stored sripts fields

    POST _scripts/dat_super_script
        {
          "script": {
            "lang": "painless",
            "code": """def my_data = ["DATA1","DATA2","DATA3"]; ctx.vars.my_data=my_data;
     --- do whatever you want there---
        """
            }
        }

This script will create an array and add this array to the execution context and you can use it as condition validator or transform or just logging action , to just add data to the execution context

  "email_administrator": {
  "condition": {
    "script": {
      "stored": "dat_super_script"
    }
  },

you can access to this data by this variable : ctx.vars.my_data
for example :

"actions": {
    "web-post": {
        "webhook": {
            "method": "POST",
            "host": {{ctx.vars.my_data.0}},
            "port": 8090,
            "path": ":/notification",
            "headers": {
                "Content-Type": "application/json"
            },
            "body": "{{#toJson}}ctx.payload.hits{{/toJson}}"
        }
    }
}

(Vinod Kannan) #3

Thank you Guillaume, Let me try your suggestion.

Thanks
VInod


(Alexander Reelsen) #4

Hey,

another possibility is to reuse the same watches (same input, same action ,same condition), but change the metadata field and access those. This way you could have scripts creating the watches on your deployment mechanism.

--Alex


(system) #5

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.