I am creating a watch which queries and aggregates the output. How do I loop through the aggregated results and create a json format and send through web hook. Tried various solutions but still stuck it. Any help will be appreciated.
PUT _xpack/watcher/watch/iad_watch
{
"trigger" : { "schedule" : { "interval" : "60s" }},
"input" : {
"search" : {
"request" : {
"body" : {
"query" : {
"bool" : {
"must" : [
{"match" : {"type":"iad" }},
{"match" : {"priority":"ERROR" }}
],
"filter" : {
"range" : {
"@timestamp" : {
"gte" : "now-1d",
"lt" : "now"
}
}
},
,
"size" :0,
"aggs": {
"analyst_name": {
"terms": {
"field": "iAnalyst.keyword"
},
"aggs": {
"group_docs": {
"top_hits": {
"size": 1,
"sort": [
{
"@timestamp": {
"order": "desc"
}
}
]
}
}
}
}
}
}
}
}
}
}
},
"condition" : {
"compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}
},
"actions" : {
"iad_webhook" : {
"webhook" : {
"auth" : {
"basic" : {
"username" : "admin",
"password" : "admin"
}
},
"method" : "POST",
"host" : "172.18.187.151",
"port" : 8161,
"path" : "/api/message",
"params" : {
"destination" : "topic://TOPIC.IAD.WATCHER.ERRORS"
},
"headers" : {
"Content-Type" : "application/json"
},
"body" : "{ "total_errors" : {{ctx.payload.hits.total}}, "iAnalyst" : "{{ctx.payload.aggregate.analyst_name.hits.hits.0._source.iAnalyst}}", "iAnalystHost" : "{{ctx.payload.aggregate.analyst_name.hits.hits.0._source.host}}", "last_error_message" : "{{ctx.payload.aggregate.analyst_name.hits.hits.0._source.message}}", "error_datetime" : "{{ctx.payload.aggregate.analyst_name.hits.hits.0._source.@timestamp}}" }"
}
}
}}