I would like to monitor the hits every 1 min so I need a watcher.
Is this approach correct ?
- I will create 2 inputs (chained).
- First returns total no.of hits in 1 min --- ctx.payload.hits.total gives that.
- Second I create buckets (keys are hostnames). then i get the document count in each bucket (values are document count)
- create a painless script to check if there are any hosts that differ by 5%
And I got stuck at the buckets. How do I loop through the buckets and get the count of each bucket.
Here is my script:
{
"trigger": {
"schedule": {
"interval": "1m"
}
},
"input": {
"chain": {
"inputs": [
{
"all":{
"search": {
"request": {
"search_type": "query_then_fetch",
"indices": [
"90183*"
],
"types": [],
"body": {
"query": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"from": "now-1m",
"to": null,
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
}
],
"should": [
{
"range": {
"total_flow_time": {
"from": 1,
"to": null,
"include_lower": true,
"include_upper": true,
"boost": 2
}
}
}
],
"disable_coord": false,
"adjust_pure_negative": true,
"minimum_should_match": "1",
"boost": 1
}
},
"ext": {}
}
}
}
}
},
{
"per_host":{
"search": {
"request": {
"search_type": "query_then_fetch",
"indices": [
"90183*"
],
"types": [],
"body": {
"query": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"from": "now-1m",
"to": null,
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
}
],
"should": [
{
"range": {
"total_flow_time": {
"from": 1,
"to": null,
"include_lower": true,
"include_upper": true,
"boost": 2
}
}
}
],
"aggs": {
"origin": {
"terms": {
"field": "host.keyword"
}
}
},
"extract": [
"aggregations.origin.buckets.key",
"aggregations.origin.buckets.sum_bucket"
],
"disable_coord": false,
"adjust_pure_negative": true,
"minimum_should_match": "1",
"boost": 1
}
},
"ext": {}
}
}
}
}
}
]
}
},
"condition": {
"script": "return true"
},
"actions": {
"send_email": {
"email": {
"profile": "standard",
"attachments": {
"attached_data.txt": {
"data": {
"format": "json"
}
}
},
"priority": "high",
"to": [
"xyz@gmail.com"
],
"subject": " {{ctx.payload.all.hits.total}} ",
"body": {
"html": "{{ctx.payload.aggregations}} "
}
}
}
}
}