# Watcher alert not working for multiple query with percentage calculation

**URL:** https://discuss.elastic.co/t/watcher-alert-not-working-for-multiple-query-with-percentage-calculation/248335
**Category:** Kibana
**Tags:** elastic-stack-alerting
**Created:** [September 11, 2020, 1:46pm UTC](https://discuss.elastic.co/t/watcher-alert-not-working-for-multiple-query-with-percentage-calculation/248335 "2020-09-11T13:46:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![suryanaj](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@suryanaj](https://discuss.elastic.co/u/suryanaj)
#### Post date: [September 11, 2020, 1:46pm UTC](https://discuss.elastic.co/t/watcher-alert-not-working-for-multiple-query-with-percentage-calculation/248335/1 "2020-09-11T13:46:38Z")

</div>

watcher alert not working for multiple query with percentage calculation

code  
{  
"trigger": {  
"schedule": {  
"interval": "2m"  
}  
},  
"input": {  
"chain": {  
"inputs": [  
{  
"first": {  
"search": {  
"request": {  
"search\_type": "query\_then\_fetch",  
"indices": [  
"heartbeat-_"  
],  
"types": [],  
"body": {  
"query": {  
"bool": {  
"must": [  
{  
"match\_phrase": {  
"http.url": "[https://nexus-amazon-uat.intranet/](https://nexus-amazon-uat.intranet/)"  
}  
},  
{  
"range": {  
"@timestamp": {  
"gte": "now-28d"  
}  
}  
}  
]  
}  
},  
"\_source": [  
"message"  
]  
}  
}  
}  
}  
},  
{  
"second": {  
"search": {  
"request": {  
"search\_type": "query\_then\_fetch",  
"indices": [  
"heartbeat-_"  
],  
"types": ,  
"body": {  
"query": {  
"bool": {  
"must": [  
{  
"match\_phrase": {  
"http.url": "[https://nexus-amazon-uat.intranet/](https://nexus-amazon-uat.intranet/)"  
}  
},  
{  
"match": {  
"monitor.status": "up"  
}  
},  
{  
"range": {  
"@timestamp": {  
"gte": "now-28d"  
}  
}  
}  
]  
}  
},  
"\_source": [  
"message"  
]  
}  
}  
}  
}  
}  
]  
}  
},  
"condition": {  
"script": {  
"source": "ctx.vars\_SLO\_Percentage = ((ctx.payload.first.hits.total / ctx.payload.second.hits.total) \* 100) \<= 100",  
"lang": "painless"  
}  
},  
"actions": {  
"email\_administrator": {  
"email": {  
"profile": "standard",  
"from": "[noreply-ESaaS@xyz.com](mailto:noreply-ESaaS@xyz.com)",  
"to": [  
"[abc@xyz.com](mailto:abc@xyz.com)"  
],  
"subject": "ELASTIC STACK ALERT: SLO nexus V2 - SLO breached {{ctx.vars\_SLO\_Percentage}}",  
"body": {  
"html": " **--Alerts Notification Details--**  
This alert triggered because a total of **{{ctx.payload.second.hits.total}}** Nexusv2 success hits and **{{ctx.payload.first.hits.total}}** nexusv2 total hits were found in the application within the last 24 hour!"  
}  
}  
}  
},  
"metadata": {  
"app": "dxdt",  
"description": "Alert if status is down",  
"env": "DX DT/Teamcity UAT"  
},  
"throttle\_period\_in\_millis": 100000  
}

---

<div class="post-metadata">

### Author: ![suryanaj](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@suryanaj](https://discuss.elastic.co/u/suryanaj)
#### Post date: [September 11, 2020, 1:56pm UTC](https://discuss.elastic.co/t/watcher-alert-not-working-for-multiple-query-with-percentage-calculation/248335/2 "2020-09-11T13:56:45Z")

</div>

i want to print value of ctx.vars\_SLO\_Percentage in subject line but its not generating .

---

<div class="post-metadata">

### Author: ![suryanaj](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@suryanaj](https://discuss.elastic.co/u/suryanaj)
#### Post date: [September 15, 2020, 2:00pm UTC](https://discuss.elastic.co/t/watcher-alert-not-working-for-multiple-query-with-percentage-calculation/248335/3 "2020-09-15T14:00:25Z")

</div>

this is working now used return .

"condition": {  
"script": {  
"source": "ctx.vars.SLO\_percentage = ctx.payload.second.hits.total \* 100 / ctx.payload.first.hits.total; return ctx.vars.SLO\_percentage \<= 100",  
"lang": "painless"  
}  
},  
"action

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 13, 2020, 2:00pm UTC](https://discuss.elastic.co/t/watcher-alert-not-working-for-multiple-query-with-percentage-calculation/248335/4 "2020-10-13T14:00:26Z")

</div>

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