watching-time-series-data.asciidoc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. [role="xpack"]
  2. [[watching-time-series-data]]
  3. === Watching time series data
  4. If you are indexing time series data such as logs, RSS feeds, or network traffic,
  5. you can use {watcher} to send notifications when certain events occur.
  6. For example, you could index an RSS feed of posts on Stack Overflow that are
  7. tagged with Elasticsearch, Logstash, Beats, or Kibana, set up a watch to check
  8. daily for new posts about a problem or failure, and send an email if any are
  9. found.
  10. The simplest way to index an RSS feed is to use https://www.elastic.co/products/logstash[Logstash].
  11. To install Logstash and set up the RSS input plugin:
  12. . https://www.elastic.co/downloads/logstash[Download Logstash] and unpack the
  13. archive file.
  14. . Go to the `logstash-{version}` directory and install the
  15. {logstash-ref}/plugins-inputs-rss.html[RSS input] plugin:
  16. +
  17. [source,sh]
  18. ----------------------------------------------------------
  19. cd logstash-<logstash_version>
  20. bin/logstash-plugin install logstash-input-rss
  21. ----------------------------------------------------------
  22. . Create a Logstash configuration file that uses the RSS input plugin to get
  23. data from an RSS/atom feed and outputs the data to Elasticsearch. For example,
  24. the following `rss.conf` file gets events from the Stack Overflow feed that
  25. are tagged with `elasticsearch`, `logstash`, `beats` or `kibana`.
  26. +
  27. [source,ruby]
  28. ----------------------------------------------------------
  29. input {
  30. rss {
  31. url => "http://stackoverflow.com/feeds/tag/elasticsearch+or+logstash+or+beats+or+kibana"
  32. interval => 3600 <1>
  33. }
  34. }
  35. output {
  36. elasticsearch { }
  37. stdout { }
  38. }
  39. ----------------------------------------------------------
  40. <1> Checks the feed every hour.
  41. +
  42. For more information see {logstash-ref}/plugins-outputs-elasticsearch.html[Elasticsearch output]
  43. in the Logstash Reference.
  44. . Run Logstash with the `rss.conf` config file to start indexing the feed:
  45. +
  46. [source,she]
  47. ----------------------------------------------------------
  48. bin/logstash -f rss.conf
  49. ----------------------------------------------------------
  50. Once you have Logstash set up to input data from the RSS feed into Elasticsearch,
  51. you can set up a daily watch that runs at noon to check for new posts that
  52. contain the words "error" or "problem".
  53. To set up the watch:
  54. . Define the watch trigger--a daily schedule that runs at 12:00 UTC:
  55. +
  56. [source,js]
  57. --------------------------------------------------
  58. "trigger" : {
  59. "schedule" : {
  60. "daily" : { "at" : "12:00" }
  61. }
  62. }
  63. --------------------------------------------------
  64. +
  65. NOTE: In {watcher}, you specify times in UTC time. Don't forget to do the
  66. conversion from your local time so the schedule triggers at the time
  67. you intend.
  68. . Define the watch input--a search that uses a filter to constrain the results
  69. to the past day.
  70. +
  71. [source,js]
  72. --------------------------------------------------
  73. "input" : {
  74. "search" : {
  75. "request" : {
  76. "indices" : [ "logstash*" ],
  77. "body" : {
  78. "query" : {
  79. "bool" : {
  80. "must" : { "match" : { "message": "error problem" }},
  81. "filter" : { "range" : { "@timestamp" : { "gte" : "now-1d" }}}
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. --------------------------------------------------
  89. . Define a watch condition to check the payload to see if the input search
  90. returned any hits. If it did, the condition resolves to `true` and the watch
  91. actions will be executed.
  92. +
  93. You define the condition with the following script:
  94. +
  95. [source,text]
  96. --------------------------------------------------
  97. return ctx.payload.hits.total.value > threshold
  98. --------------------------------------------------
  99. +
  100. If you store the script in a file at `$ES_HOME/config/scripts/threshold_hits.painless`,
  101. you can then reference it by name in the watch condition.
  102. +
  103. [source,js]
  104. --------------------------------------------------
  105. "condition" : {
  106. "script" : {
  107. "id" : "threshold_hits",
  108. "params" : {
  109. "threshold" : 0 <1>
  110. }
  111. }
  112. }
  113. --------------------------------------------------
  114. <1> The threshold parameter value you want to pass to the script.
  115. +
  116. . Define a watch action to send an email that contains the relevant messages
  117. from the past day as an attachment.
  118. +
  119. [source,js]
  120. --------------------------------------------------
  121. "actions" : {
  122. "send_email" : {
  123. "email" : {
  124. "to" : "username@example.org",
  125. "subject" : "Somebody needs help with the Elastic Stack",
  126. "body" : "The attached Stack Overflow posts were tagged with Elasticsearch, Logstash, Beats or Kibana and mentioned an error or problem.",
  127. "attachments" : {
  128. "attached_data" : {
  129. "data" : {
  130. "format" : "json"
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. --------------------------------------------------
  138. +
  139. NOTE: To use the email action, you must configure at least one email account in
  140. `elasticsearch.yml`. If you configure multiple email accounts, you need to
  141. specify which one you want to send the email with. For more information, see
  142. <<configuring-email>>.
  143. The complete watch looks like this:
  144. [source,console]
  145. --------------------------------------------------
  146. PUT _watcher/watch/rss_watch
  147. {
  148. "trigger" : {
  149. "schedule" : {
  150. "daily" : { "at" : "12:00" }
  151. }
  152. },
  153. "input" : {
  154. "search" : {
  155. "request" : {
  156. "indices" : [ "logstash*" ],
  157. "body" : {
  158. "query" : {
  159. "bool" : {
  160. "must" : { "match" : { "message": "error problem" }},
  161. "filter" : { "range" : { "@timestamp" : { "gte" : "now-1d" }}}
  162. }
  163. }
  164. }
  165. }
  166. }
  167. },
  168. "condition" : {
  169. "script" : {
  170. "id" : "threshold_hits",
  171. "params" : {
  172. "threshold" : 0
  173. }
  174. }
  175. },
  176. "actions" : {
  177. "send_email" : {
  178. "email" : {
  179. "to" : "username@example.org", <1>
  180. "subject" : "Somebody needs help with the Elastic Stack",
  181. "body" : "The attached Stack Overflow posts were tagged with Elasticsearch, Logstash, Beats or Kibana and mentioned an error or problem.",
  182. "attachments" : {
  183. "attached_data" : {
  184. "data" : {}
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. --------------------------------------------------
  192. // TEST[s/"id" : "threshold_hits"/"source": "return ctx.payload.hits.total.value > params.threshold"/]
  193. <1> Replace `username@example.org` with your email address to receive
  194. notifications.
  195. [TIP]
  196. =================================================
  197. To execute a watch immediately (without waiting for the schedule to trigger),
  198. use the {ref}/watcher-api-execute-watch.html[`_execute` API]:
  199. [source,console]
  200. --------------------------------------------------
  201. POST _watcher/watch/rss_watch/_execute
  202. {
  203. "ignore_condition" : true,
  204. "action_modes" : {
  205. "_all" : "force_execute"
  206. },
  207. "record_execution" : true
  208. }
  209. --------------------------------------------------
  210. // TEST[continued]
  211. =================================================