# How to get Hostname in email subject for Diskspace alert

**URL:** https://discuss.elastic.co/t/how-to-get-hostname-in-email-subject-for-diskspace-alert/137510
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting
**Created:** [June 26, 2018, 9:09pm UTC](https://discuss.elastic.co/t/how-to-get-hostname-in-email-subject-for-diskspace-alert/137510 "2018-06-26T21:09:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rpendela](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@rpendela](https://discuss.elastic.co/u/rpendela)
#### Post date: [June 26, 2018, 9:09pm UTC](https://discuss.elastic.co/t/how-to-get-hostname-in-email-subject-for-diskspace-alert/137510/1 "2018-06-26T21:09:27Z")

</div>

My issue also similar to this [topic](https://discuss.elastic.co/t/watcher-not-sending-email-to-alert-on-disk-space/135742) but I am not using any advance query, I can see my threshold query is executing but I want to see the host name in email subject

Here is my Threshold alert

 ![33%20AM](https://us1.discourse-cdn.com/elastic/original/3X/0/0/00b276ecc897e7365802cefec03efa1bb3cae95a.png)

I want to see email notification as  
`Watch [Disk space] has exceeded the threshold on AWSN`

Here is my email subject ( I tried couple of things like below)

```
   Watch [{{ctx.metadata.name}}] has exceeded the threshold on [{{ctx.payload.aggregations.beat.hostname}}]

   Watch [{{ctx.metadata.name}}] has exceeded the threshold on [{{ctx.payload.beat.hostname}}]

 Watch [{{ctx.metadata.name}}] has exceeded the threshold on [{{ctx.payload.beat.name}}]

```

Please help me to figure what I am missing to get hostname in email?

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [June 27, 2018, 7:10am UTC](https://discuss.elastic.co/t/how-to-get-hostname-in-email-subject-for-diskspace-alert/137510/2 "2018-06-27T07:10:35Z")

</div>

can you run the [execute watch API](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/watcher-api-execute-watch.html) in the dev tools for this watch and show us the result?

---

<div class="post-metadata">

### Author: ![rpendela](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@rpendela](https://discuss.elastic.co/u/rpendela)
#### Post date: [June 27, 2018, 4:44pm UTC](https://discuss.elastic.co/t/how-to-get-hostname-in-email-subject-for-diskspace-alert/137510/5 "2018-06-27T16:44:59Z")

</div>

Thanks @spinscale I found the solution what I need from the community here but would like to know is there a way that I can see alerts for specific hosts with wildcard?, I have a multiple servers but I want to watch specifically on my AD servers.

Here is what I have now,

{  
"trigger": {  
"schedule": {  
"interval": "15m"  
}  
},  
"input": {  
"search": {  
"request": {  
"search\_type": "query\_then\_fetch",  
"indices": [  
"metricbeat-_"  
],  
"types": [],  
"body": {  
"size": 0,  
"query": {  
"bool": {  
"filter": {  
"range": {  
"@timestamp": {  
"gte": "{{ctx.trigger.scheduled\_time}}||-15m",  
"lte": "{{ctx.trigger.scheduled\_time}}",  
"format": "strict\_date\_optional\_time||epoch\_millis"  
}  
}  
},  
"should": [  
{  
"match\_phrase": {  
"beat.hostname": "ADDC1"  
}  
},  
{  
"match\_phrase": {  
"beat.hostname": "ADDC2"  
}  
},  
{  
"match\_phrase": {  
"beat.hostname": "addctcde1"  
}  
}  
],  
"minimum\_should\_match": 1  
}  
},  
"aggs": {  
"bucketAgg": {  
"terms": {  
"field": "beat.hostname",  
"size": 10,  
"order": {  
"metricAgg": "desc"  
}  
},  
"aggs": {  
"metricAgg": {  
"max": {  
"field": "system.filesystem.used.pct"  
}  
}  
}  
}  
}  
}  
}  
}  
},  
"condition": {  
"script": {  
"source": "ArrayList arr = ctx.payload.aggregations.bucketAgg.buckets; for (int i = 0; i \< arr.length; i++) { if (arr[i]['metricAgg'].value \> params.threshold) { return true; } } return false;",  
"lang": "painless",  
"params": {  
"threshold": 0.85  
}  
}  
},  
"actions": {  
"email\_1": {  
"email": {  
"profile": "standard",  
"priority": "high",  
"to": [  
 "test@domain.com",  
],  
"subject": "Watch [{{ctx.metadata.name}}] has exceeded the threshold on \n{{#ctx.payload.results}}{{key}}\n{{/ctx.payload.results}}",  
"body": {  
"text": "The following hosts have exceeded the threshold: \n{{#ctx.payload.results}}{{key}}:{{value}}\n{{/ctx.payload.results}}"  
}  
}  
}  
},  
"metadata": {  
"color": "red"  
},  
"transform": {  
"script": {  
"source": "HashMap result = new HashMap(); ArrayList arr = ctx.payload.aggregations.bucketAgg.buckets; ArrayList filteredHits = new ArrayList(); for (int i = 0; i \< arr.length; i++) { HashMap filteredHit = new HashMap(); filteredHit.key = arr[i].key; filteredHit.value = Math.round(arr[i]['metricAgg'].value_100); if (filteredHit.value \> params.threshold) { filteredHits.add(filteredHit); } } result.results = filteredHits; return result;",  
"lang": "painless",  
"params": {  
"threshold": 85  
}  
}  
}  
}

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [June 29, 2018, 8:43am UTC](https://discuss.elastic.co/t/how-to-get-hostname-in-email-subject-for-diskspace-alert/137510/6 "2018-06-29T08:43:18Z")

</div>

Hey,

if you have a certain hostname scheme, you could use a `prefix` query in your watch that searches for `ldap-` in the hostname field (or sth similar), and thus find all the LDAP servers.

Hope that helps!

--Alex

---

<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: [July 27, 2018, 8:43am UTC](https://discuss.elastic.co/t/how-to-get-hostname-in-email-subject-for-diskspace-alert/137510/7 "2018-07-27T08:43:24Z")

</div>

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