getting-started.asciidoc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. [role="xpack"]
  2. [[watcher-getting-started]]
  3. == Getting started with {watcher}
  4. TIP: To complete these steps, you must obtain a license that includes the
  5. {alert-features}. For more information about Elastic license levels, see
  6. https://www.elastic.co/subscriptions and
  7. {stack-ov}/license-management.html[License management].
  8. [[watch-log-data]]
  9. To set up a watch to start sending alerts:
  10. * <<log-add-input, Schedule the watch and define an input>>.
  11. * <<log-add-condition, Add a condition>> that checks to see if an alert
  12. needs to be sent.
  13. * <<log-take-action, Configure an action>> to send an alert when the
  14. condition is met.
  15. [float]
  16. [[log-add-input]]
  17. === Schedule the watch and define an input
  18. A watch <<trigger-schedule,schedule>> controls how often a watch is triggered.
  19. The watch <<input,input>> gets the data that you want to evaluate.
  20. To periodically search log data and load the results into the
  21. watch, you could use an <<schedule-interval,interval>> schedule and a
  22. <<input-search,search>> input. For example, the following Watch searches
  23. the `logs` index for errors every 10 seconds:
  24. [source,console]
  25. ------------------------------------------------------------
  26. PUT _watcher/watch/log_error_watch
  27. {
  28. "trigger" : {
  29. "schedule" : { "interval" : "10s" } <1>
  30. },
  31. "input" : {
  32. "search" : {
  33. "request" : {
  34. "indices" : [ "logs" ],
  35. "body" : {
  36. "query" : {
  37. "match" : { "message": "error" }
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44. ------------------------------------------------------------
  45. <1> Schedules are typically configured to run less frequently. This example sets
  46. the interval to 10 seconds so you can easily see the watches being triggered.
  47. Since this watch runs so frequently, don't forget to <<log-delete, delete the watch>>
  48. when you're done experimenting.
  49. If you check the watch history you'll see that the watch is being triggered every
  50. 10 seconds. However, the search isn't returning any results so nothing is loaded
  51. into the watch payload.
  52. For example, the following request retrieves the last ten watch executions (watch
  53. records) from the watch history:
  54. [source,console]
  55. ------------------------------------------------------------
  56. GET .watcher-history*/_search?pretty
  57. {
  58. "sort" : [
  59. { "result.execution_time" : "desc" }
  60. ]
  61. }
  62. ------------------------------------------------------------
  63. // TEST[continued]
  64. [float]
  65. [[log-add-condition]]
  66. === Add a condition
  67. A <<condition,condition>> evaluates the data you've loaded into the watch and
  68. determines if any action is required. Now that you've loaded log errors into
  69. the watch, you can define a condition that checks to see if any errors were
  70. found.
  71. For example, the following compare condition simply checks to see if the
  72. search input returned any hits.
  73. [source,console]
  74. --------------------------------------------------
  75. PUT _watcher/watch/log_error_watch
  76. {
  77. "trigger" : { "schedule" : { "interval" : "10s" }},
  78. "input" : {
  79. "search" : {
  80. "request" : {
  81. "indices" : [ "logs" ],
  82. "body" : {
  83. "query" : {
  84. "match" : { "message": "error" }
  85. }
  86. }
  87. }
  88. }
  89. },
  90. "condition" : {
  91. "compare" : { "ctx.payload.hits.total.value" : { "gt" : 0 }} <1>
  92. }
  93. }
  94. --------------------------------------------------
  95. <1> The <<condition-compare,compare>> condition lets you easily compare against
  96. values in the execution context.
  97. For this compare condition to evaluate to `true`, you need to add an event
  98. to the `logs` index that contains an error. For example, the following request
  99. adds a 404 error to the `logs` index:
  100. [source,console]
  101. --------------------------------------------------
  102. POST logs/event
  103. {
  104. "timestamp" : "2015-05-17T18:12:07.613Z",
  105. "request" : "GET index.html",
  106. "status_code" : 404,
  107. "message" : "Error: File not found"
  108. }
  109. --------------------------------------------------
  110. // TEST[continued]
  111. Once you add this event, the next time the watch executes its condition will
  112. evaluate to `true`. The condition result is recorded as part of the
  113. `watch_record` each time the watch executes, so you can verify whether or
  114. not the condition was met by searching the watch history:
  115. [source,console]
  116. --------------------------------------------------
  117. GET .watcher-history*/_search?pretty
  118. {
  119. "query" : {
  120. "bool" : {
  121. "must" : [
  122. { "match" : { "result.condition.met" : true }},
  123. { "range" : { "result.execution_time" : { "from" : "now-10s" }}}
  124. ]
  125. }
  126. }
  127. }
  128. --------------------------------------------------
  129. // TEST[continued]
  130. [float]
  131. [[log-take-action]]
  132. === Configure an action
  133. Recording watch records in the watch history is nice, but the real power of
  134. {watcher} is being able to do something when the watch condition is met. A
  135. watch's <<actions,actions>> define what to do when the watch condition
  136. evaluates to `true`. You can send emails, call third-party webhooks, write
  137. documents to an Elasticsearch index, or log messages to the standard
  138. Elasticsearch log files.
  139. For example, the following action writes a message to the Elasticsearch
  140. log when an error is detected.
  141. [source,console]
  142. --------------------------------------------------
  143. PUT _watcher/watch/log_error_watch
  144. {
  145. "trigger" : { "schedule" : { "interval" : "10s" }},
  146. "input" : {
  147. "search" : {
  148. "request" : {
  149. "indices" : [ "logs" ],
  150. "body" : {
  151. "query" : {
  152. "match" : { "message": "error" }
  153. }
  154. }
  155. }
  156. }
  157. },
  158. "condition" : {
  159. "compare" : { "ctx.payload.hits.total.value" : { "gt" : 0 }}
  160. },
  161. "actions" : {
  162. "log_error" : {
  163. "logging" : {
  164. "text" : "Found {{ctx.payload.hits.total.value}} errors in the logs"
  165. }
  166. }
  167. }
  168. }
  169. --------------------------------------------------
  170. [float]
  171. [[log-delete]]
  172. === Delete the Watch
  173. Since the `log_error_watch` is configured to run every 10 seconds, make sure you
  174. delete it when you're done experimenting. Otherwise, the noise from this sample
  175. watch will make it hard to see what else is going on in your watch history and
  176. log file.
  177. To remove the watch, use the <<watcher-api-delete-watch,delete watch API>>:
  178. [source,console]
  179. --------------------------------------------------
  180. DELETE _watcher/watch/log_error_watch
  181. --------------------------------------------------
  182. // TEST[continued]
  183. [float]
  184. [[required-security-privileges]]
  185. === Required security privileges
  186. To enable users to create and manipulate watches, assign them the `watcher_admin`
  187. security role. Watcher admins can also view watches, watch history, and triggered
  188. watches.
  189. To allow users to view watches and the watch history, assign them the `watcher_user`
  190. security role. Watcher users cannot create or manipulate watches; they are only
  191. allowed to execute read-only watch operations.
  192. [float]
  193. [[next-steps]]
  194. === Where to go next
  195. * See <<how-watcher-works>> for more information about the
  196. anatomy of a watch and the watch lifecycle.
  197. * See <<example-watches>> for more examples of setting up
  198. a watch.
  199. * See the https://github.com/elastic/examples/tree/master/Alerting[Example
  200. Watches] in the Elastic Examples repo for additional sample watches you can use
  201. as a starting point for building custom watches.