Hi,
I'm trying to setup a watcher so that based on the number on hits in my query send out that number of emails.
It seems to me that this can't be done by simply looping around the email action so I have an idea but need help on the implementation.
My idea is to grab the top hit sorted by time (ascending), save that hit using an index action, and then send an email out for that hit. Also in the query will be a script query that will filter out hits that have already been alerted. ie. Comparing with doc created by index action on certain fields
What I am missing so far is getting the index action to create a doc based on my hit and then the compare script query. (I have removed my email address but i can confirm that that is working fine)
My first priority is for my hit to be created as a doc. Example below.
{
"trigger": {
"schedule": {
"interval": "1m"
}
},
"input": {
"search": {
"request": {
"search_type": "query_then_fetch",
"indices": [
"metricbeat*"
],
"types": [],
"body": {
"from": 0,
"size": 1,
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"gte": "{{ctx.trigger.scheduled_time}}||-60m",
"lte": "{{ctx.trigger.scheduled_time}}",
"format": "strict_date_optional_time||epoch_millis"
}
}
},
{
"range": {
"system.cpu.total.pct": {
"gt": "0.2"
}
}
}
]
}
}
}
},
"sort": {
"@timestamp": {
"order": "asc"
}
}
}
}
}
},
"condition": {
"compare": {
"ctx.payload.hits.total": {
"gte": 1
}
}
},
"actions": {
"index_payload": {
"index": {
"index": "metricbeat-watcher",
"doc_type": "doc"
}
},
"send_email": {
"email": {
"profile": "standard",
"to": [
""
],
"body": {
"text": "There are {{ctx.payload.hits.total}} documents in your index. Threshold is 1. {{ctx.payload.hits.hits.0._source.system.cpu.total.pct}}"
}
}
}
}
}
When I try to search for any documents on my index metricbeat-watcher I get zero hits so i'm thinking that some type of transform is needed in the index action? (i get over 1000 hits for hits total so no problem with that)
If anyone has any suggestions or alternative solutions that would be much appreciated.
Regards, Patrick