2017-03-28 17:23:01 -04:00
[[watching-meetup-data]]
=== Watching Event Data
If you are indexing event data, such as log messages, network traffic, or a web feed, you can create a watch to email notifications when certain events occur.
2017-07-14 15:31:53 -04:00
For example, if you index a feed of RSVPs for meetup events happening around the world, you can create a watch that alerts you to interesting events.
2017-03-28 17:23:01 -04:00
To index the meetup data, you can use https://www.elastic.co/products/logstash[Logstash] to ingest live data from the Meetup.com streaming API, `http://stream.meetup.com/2/rsvps`.
To ingest this data with Logstash:
. https://www.elastic.co/downloads/logstash[Download Logstash] and unpack the
archive file.
. Create a Logstash configuration file that uses the {logstash-ref}/plugins-inputs-stdin.html[Logstash standard input] and the {logstash-ref}/plugins-outputs-stdout.html[Logstash standard output] and save it in `logstash-{version}` directory as `livestream.conf`:
+
2018-06-22 21:09:37 -04:00
--
2017-03-28 17:23:01 -04:00
[source,ruby]
----------------------------------------------------------
input {
stdin {
codec => json <1>
}
}
filter {
date {
match => [ "event.time", "UNIX_MS" ]
target => "event_time"
}
}
output { <2>
stdout {
codec => rubydebug
}
elasticsearch {
hosts => "http://localhost:9200"
user => "elastic"
2017-06-29 16:27:57 -04:00
password => "x-pack-test-password"
2017-03-28 17:23:01 -04:00
}
}
----------------------------------------------------------
2018-06-22 21:09:37 -04:00
// NOTCONSOLE
2017-03-28 17:23:01 -04:00
<1> The meetup data stream is formatted in JSON.
<2> Index the meetup data into Elasticsearch.
2018-06-22 21:09:37 -04:00
--
2017-03-28 17:23:01 -04:00
. To start indexing the meetup data, pipe the RSVP stream into Logstash and specify your `livestream.conf` configuration file.
+
2018-06-22 21:09:37 -04:00
--
[source,shell]
2017-03-28 17:23:01 -04:00
----------------------------------------------------------
2017-06-27 20:16:51 -04:00
curl http://stream.meetup.com/2/rsvps | bin/logstash -f livestream.conf
2019-02-27 11:15:01 -05:00
----------------------------------------------------------
2018-06-22 21:09:37 -04:00
// NOTCONSOLE
--
2017-03-28 17:23:01 -04:00
Now that you're indexing the meetup RSVPs, you can set up a watch that lets you know about events you might be interested in. For example, let's create a watch that runs every hour, looks for events that talk about about _Open Source_, and sends an email with information about the events.
To set up the watch:
. Specify how often you want to run the watch by adding a schedule trigger to the watch:
+
2018-06-22 21:09:37 -04:00
--
2017-03-28 17:23:01 -04:00
[source,js]
--------------------------------------------------
{
"trigger": {
"schedule": {
"interval": "1h"
}
},
--------------------------------------------------
2018-06-22 21:09:37 -04:00
// NOTCONSOLE
--
2017-03-28 17:23:01 -04:00
. Load data into the watch payload by creating an input that searches the meetup data for events that have _Open Source_ as a topic. You can use aggregations to group the data by city, consolidate references to the same events, and sort the events by date.
+
2018-06-22 21:09:37 -04:00
--
2017-03-28 17:23:01 -04:00
[source,js]
-------------------------------------------------
"input": {
"search": {
"request": {
"indices": [
"<logstash-{now-1h}>", <1>
"<logstash-{now}>"
],
"body": {
"size": 0,
"query": {
"bool": {
"filter": [
{
"range": {
"@timestamp": {
"gte": "now-3h"
}
}
},
{
"match": {
"group.group_topics.topic_name": "Open Source" <2>
}
}
]
}
},
"aggs": {
"group_by_city": {
"terms": {
"field": "group.group_city.raw", <3>
"size": 5
},
"aggs": {
"group_by_event": {
"terms": {
"field": "event.event_url.raw", <4>
"size": 5
},
"aggs": {
"get_latest": {
"terms": {
"field": "@timestamp", <5>
"size": 1,
"order": {
2017-05-11 14:26:31 -04:00
"_key": "desc"
2017-03-28 17:23:01 -04:00
}
},
"aggs": {
"group_by_event_name": {
"terms": {
"field": "event.event_name.raw" <6>
}
}
}
}
}
}
}
}
}
}
}
}
},
2019-02-27 11:15:01 -05:00
-------------------------------------------------
2018-06-22 21:09:37 -04:00
// NOTCONSOLE
2017-06-27 20:16:51 -04:00
<1> Elasticsearch Date math is used to select the Logstash indices that contain the meetup data. The second pattern is needed in case the previous hour crosses days.
2017-03-28 17:23:01 -04:00
<2> Find all of the RSVPs with `Open Source` as a topic.
<3> Group the RSVPs by city.
<4> Consolidate multiple RSVPs for the same event.
<5> Sort the events so the latest events are listed first.
<6> Group the events by name.
2018-06-22 21:09:37 -04:00
--
2017-03-28 17:23:01 -04:00
. To determine whether or not there are any Open Source events, add a compare condition that checks the watch payload to see if there were any search hits.
2018-06-22 21:09:37 -04:00
+
--
2017-03-28 17:23:01 -04:00
[source,js]
--------------------------------------------------
2018-12-05 13:49:06 -05:00
"compare" : { "ctx.payload.hits.total.value" : { "gt" : 0 }}
2017-03-28 17:23:01 -04:00
--------------------------------------------------
2018-06-22 21:09:37 -04:00
// NOTCONSOLE
--
2017-03-28 17:23:01 -04:00
. To send an email when _Open Source_ events are found, add an email action:
2018-06-22 21:09:37 -04:00
+
--
2017-03-28 17:23:01 -04:00
[source,js]
2019-02-27 11:15:01 -05:00
---------------------------------------------------
2017-06-27 20:16:51 -04:00
"actions": {
2017-03-28 17:23:01 -04:00
"email_me": {
"throttle_period": "10m",
"email": {
"from": "<from:email address>",
"to": "<to:email address>",
"subject": "Open Source Events",
2017-06-27 20:16:51 -04:00
"body": {
2017-03-28 17:23:01 -04:00
"html": "Found events matching Open Source: <ul>{{#ctx.payload.aggregations.group_by_city.buckets}}< li>{{key}} ({{doc_count}})<ul>{{#group_by_event.buckets}}
2017-06-27 20:16:51 -04:00
<li><a href=\"{{key}}\">{{get_latest.buckets.0.group_by_event_name.buckets.0.key}}</a>
2017-03-28 17:23:01 -04:00
({{doc_count}})</li>{{/group_by_event.buckets}}</ul></li>
{{/ctx.payload.aggregations.group_by_city.buckets}}</ul>"
}
}
}
}
---------------------------------------------------
2018-06-22 21:09:37 -04:00
// NOTCONSOLE
--
2017-03-28 17:23:01 -04:00
NOTE: To enable Watcher to send emails, you must configure an email account in `elasticsearch.yml`. For more information, see <<configuring-email, Working with Various Email Services>>.
The complete watch looks like this:
[source,js]
--------------------------------------------------
2018-12-08 13:57:16 -05:00
PUT _watcher/watch/meetup
2017-03-28 17:23:01 -04:00
{
"trigger": {
"schedule": {
"interval": "1h"
}
},
"input": {
"search": {
"request": {
"indices": [
"<logstash-{now-1h}>",
"<logstash-{now}>"
],
"body": {
"size": 0,
"query": {
"bool": {
"filter": [
{
"range": {
"@timestamp": {
"gte": "now-3h"
}
}
},
{
"match": {
"group.group_topics.topic_name": "Open Source"
}
}
]
}
},
"aggs": {
"group_by_city": {
"terms": {
"field": "group.group_city.raw",
"size": 5
},
"aggs": {
"group_by_event": {
"terms": {
"field": "event.event_url.raw",
"size": 5
},
"aggs": {
"get_latest": {
"terms": {
"field": "@timestamp",
"size": 1,
"order": {
2017-05-11 14:26:31 -04:00
"_key": "desc"
2017-03-28 17:23:01 -04:00
}
},
"aggs": {
"group_by_event_name": {
"terms": {
"field": "event.event_name.raw"
}
}
}
}
}
}
}
}
}
}
}
}
},
"condition": {
"compare": {
2018-12-05 13:49:06 -05:00
"ctx.payload.hits.total.value": {
2017-03-28 17:23:01 -04:00
"gt": 0
}
}
},
"actions": { <1>
"email_me": {
"throttle_period": "10m",
"email": {
2018-10-18 05:54:50 -04:00
"from": "username@example.org", <2>
"to": "recipient@example.org", <3>
2017-03-28 17:23:01 -04:00
"subject": "Open Source events",
2017-06-27 20:16:51 -04:00
"body": {
"html": "Found events matching Open Source: <ul>{{#ctx.payload.aggregations.group_by_city.buckets}}<li>{{key}} ({{doc_count}})<ul>{{#group_by_event.buckets}}<li><a href=\"{{key}}\">{{get_latest.buckets.0.group_by_event_name.buckets.0.key}}</a> ({{doc_count}})</li>{{/group_by_event.buckets}}</ul></li>{{/ctx.payload.aggregations.group_by_city.buckets}}</ul>"
2017-03-28 17:23:01 -04:00
}
}
}
}
}
--------------------------------------------------
// CONSOLE
2017-06-27 20:16:51 -04:00
<1> The email body can include Mustache templates to reference data in the watch payload. By default,it will be <<email-html-sanitization, sanitized>> to block dangerous content.
2017-03-28 17:23:01 -04:00
<2> Replace the `from` address with the email address you configured in `elasticsearch.yml`.
<3> Replace the `to` address with your email address to receive notifications.
2017-06-27 20:16:51 -04:00
Now that you've created your watch, you can use the
{ref}/watcher-api-execute-watch.html[`_execute` API] to run it without waiting for the schedule to trigger execution:
2017-03-28 17:23:01 -04:00
[source,js]
--------------------------------------------------
2018-12-08 13:57:16 -05:00
POST _watcher/watch/meetup/_execute
2017-03-28 17:23:01 -04:00
--------------------------------------------------
// CONSOLE
// TEST[continued]