# Include all hosts that met condition in watcher email alert

**URL:** <https://discuss.elastic.co/t/include-all-hosts-that-met-condition-in-watcher-email-alert/299866>\
**Category:** Kibana\
**Tags:** elastic-stack-alerting, painless\
**Created:** [March 16, 2022, 2:04pm UTC](https://discuss.elastic.co/t/include-all-hosts-that-met-condition-in-watcher-email-alert/299866 "2022-03-16T14:04:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew22](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew22](https://discuss.elastic.co/u/Andrew22)\
**Post date:** [March 16, 2022, 2:04pm UTC](https://discuss.elastic.co/t/include-all-hosts-that-met-condition-in-watcher-email-alert/299866/1 "2022-03-16T14:04:43Z")

</div>

Hello,

I am wondering if it is possible to include all hosts that have met the condition set in the watcher in one email? I would like to send my clients a list of devices that have met the condition once a day.

Here is my config so far:

```auto
{
  "trigger": {
    "schedule": {
      "interval": "24h"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "poller-*"
        ],
        "rest_total_hits_as_int": true,
        "body": {
          "size": 0,
          "sort": [
            {
              "timestamp": {
                "order": "desc"
              }
            }
          ],
          "query": {
            "bool": {
              "must": [
                {
                  "wildcard": {
                    "host": {
                      "value": "device_*"
                    }
                  }
                },
                {
                  "range": {
                    "timestamp": {
                      "gte": "now-1d",
                      "lte": "now"
                    }
                  }
                }
              ]
            }
          },
          "aggs": {
            "host": {
              "terms": {
                "field": "host.keyword",
                "size": 10000
              },
              "aggs": {
                "inErrors": {
                  "sum": {
                    "field": "inErrors"
                  }
                },
                "in_errors": {
                  "bucket_script": {
                    "buckets_path": {
                      "var1": "inErrors"
                    },
                    "script": "if (params.var1 > 0) { return(params.var1) } else{ return(0)}"
                  }
                },
                "outErrors": {
                  "sum": {
                    "field": "outErrors"
                  }
                },
                "out_errors": {
                  "bucket_script": {
                    "buckets_path": {
                      "var1": "outErrors"
                    },
                    "script": "if (params.var1 > 0) { return(params.var1)} else{ return(0)}"
                  }
                },
                "inDiscards": {
                  "sum": {
                    "field": "inDiscards"
                  }
                },
                "in_Discards": {
                  "bucket_script": {
                    "buckets_path": {
                      "var1": "inDiscards"
                    },
                    "script": "if (params.var1 > 0) { return(params.var1)} else{ return(0)}"
                  }
                },
                "outDiscards": {
                  "sum": {
                    "field": "outDiscards"
                  }
                },
                "out_Discards": {
                  "bucket_script": {
                    "buckets_path": {
                      "var1": "outDiscards"
                    },
                    "script": "if (params.var1 > 0) { return(params.var1)} else{ return(0)}"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "script": {
      "source": "ArrayList arr = ctx.payload.aggregations.host.buckets; for (int i = 0; i < arr.length; i++) { if (arr[i].in_Discards.value == params.threshold || arr[i].out_Discards.value == params.threshold || arr[i].out_errors.value == params.threshold || arr[i].in_errors.value == params.threshold) { return true; } } return false;",
      "lang": "painless",
      "params": {
        "threshold": 0
      }
    }
  },
  "actions": {
    "send_email": {
      "foreach": "ctx.payload.results",
      "throttle_period_in_millis": 1800000,
      "email": {
        "profile": "standard",
        "to": [
          " ***********@************"
        ],
        "subject": "Alert {{ctx.result.total}} Devices With Discards/Errors",
        "body": {
          "text": """ The Device: {{ctx.payload.key}} Had:
          {{ctx.payload.in_errors}} In Errors.
           """
        }
      }
    }
  },
  "transform": {
    "script": {
      "source": "HashMap result = new HashMap(); ArrayList arr = ctx.payload.aggregations.host.buckets; ArrayList filteredHits = new ArrayList(); for (int i = 0; i < arr.length; i++) { HashMap filteredHit = new HashMap(); filteredHit.key = arr[i].key; filteredHit.in_Discards = arr[i].in_Discards.value; filteredHit.out_Discards = arr[i].out_Discards.value; filteredHit.in_errors = arr[i].in_errors.value; filteredHit.out_errors = arr[i].out_errors.value; if (filteredHit.in_Discards == params.threshold || filteredHit.out_Discards == params.threshold || filteredHit.in_errors == params.threshold || filteredHit.out_errors == params.threshold) { filteredHits.add(filteredHit); } } result.results = filteredHits; return result;",
      "lang": "painless",
      "params": {
        "threshold": 0
      }
    }
  }
}

```

Any help would be greatly appreciated!

---

<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:** [March 17, 2022, 9:34am UTC](https://discuss.elastic.co/t/include-all-hosts-that-met-condition-in-watcher-email-alert/299866/2 "2022-03-17T09:34:58Z")

</div>

Hey,

in general the answer to your question is `yes` as you can include the complete payload in your email. Maybe you could explain, what part of your script is not working as expected, so we can drill down on a specific part?

--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:** [April 14, 2022, 9:35am UTC](https://discuss.elastic.co/t/include-all-hosts-that-met-condition-in-watcher-email-alert/299866/3 "2022-04-14T09:35:26Z")

</div>

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