actions.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. [[actions]]
  2. == Actions
  3. When a watch's condition is met, its actions are executed unless it is being
  4. <<actions-ack-throttle, throttled>>. A watch can perform multiple actions.
  5. The actions are executed one at a time and each action executes independently.
  6. Any failures encountered while executing an action are recorded in the
  7. action result and in the watch history.
  8. NOTE: If no actions are defined for a watch, no actions are executed.
  9. However, a `watch_record` is still written to the watch history.
  10. Actions have access to the payload in the execution context. They can use it to
  11. support their execution in any way they need. For example, the payload might
  12. serve as a model for a templated email body.
  13. {watcher} supports the following types of actions:
  14. <<actions-email, email>>, <<actions-webhook, webhook>>, <<actions-index, index>>,
  15. <<actions-logging, logging>>, <<actions-hipchat, hipchat>>, <<actions-slack,
  16. Slack>>, and <<actions-pagerduty, pagerduty>>.
  17. [float]
  18. [[actions-ack-throttle]]
  19. === Acknowledgement and Throttling
  20. During the watch execution, once the condition is met, a decision is made per
  21. configured action as to whether it should be throttled. The main purpose of
  22. action throttling is to prevent too many executions of the same action for the
  23. same watch.
  24. For example, suppose you have a watch that detects errors in an application's log
  25. entries. The watch is triggered every five minutes and searches for errors during
  26. the last hour. In this case, if there are errors, there is a period of time where
  27. the watch is checked and its actions are executed multiple times based on the same
  28. errors. As a result, the system administrator receives multiple notifications about
  29. the same issue, which can be annoying.
  30. To address this issue, {watcher} supports time-based throttling. You can define
  31. a throttling period as part of the action configuration to limit how often the
  32. action is executed. When you set a throttling period, {watcher} prevents repeated
  33. execution of the action if it has already executed within the throttling period
  34. time frame (`now - throttling period`).
  35. The following snippet shows a watch for the scenario described above - associating
  36. a throttle period with the `email_administrator` action:
  37. [source,js]
  38. --------------------------------------------------
  39. PUT _watcher/watch/error_logs_alert
  40. {
  41. "metadata" : {
  42. "color" : "red"
  43. },
  44. "trigger" : {
  45. "schedule" : {
  46. "interval" : "5m"
  47. }
  48. },
  49. "input" : {
  50. "search" : {
  51. "request" : {
  52. "indices" : "log-events",
  53. "body" : {
  54. "size" : 0,
  55. "query" : { "match" : { "status" : "error" } }
  56. }
  57. }
  58. }
  59. },
  60. "condition" : {
  61. "compare" : { "ctx.payload.hits.total.value" : { "gt" : 5 }}
  62. },
  63. "actions" : {
  64. "email_administrator" : {
  65. "throttle_period": "15m", <1>
  66. "email" : { <2>
  67. "to" : "sys.admino@host.domain",
  68. "subject" : "Encountered {{ctx.payload.hits.total.value}} errors",
  69. "body" : "Too many error in the system, see attached data",
  70. "attachments" : {
  71. "attached_data" : {
  72. "data" : {
  73. "format" : "json"
  74. }
  75. }
  76. },
  77. "priority" : "high"
  78. }
  79. }
  80. }
  81. }
  82. --------------------------------------------------
  83. // CONSOLE
  84. <1> There will be at least 15 minutes between subsequent `email_administrator`
  85. action executions.
  86. <2> See <<actions-email, Email Action>> for more information.
  87. You can also define a throttle period at the watch level. The watch-level
  88. throttle period serves as the default throttle period for all of the actions
  89. defined in the watch:
  90. [source,js]
  91. --------------------------------------------------
  92. PUT _watcher/watch/log_event_watch
  93. {
  94. "trigger" : {
  95. "schedule" : { "interval" : "5m" }
  96. },
  97. "input" : {
  98. "search" : {
  99. "request" : {
  100. "indices" : "log-events",
  101. "body" : {
  102. "size" : 0,
  103. "query" : { "match" : { "status" : "error" } }
  104. }
  105. }
  106. }
  107. },
  108. "condition" : {
  109. "compare" : { "ctx.payload.hits.total.value" : { "gt" : 5 }}
  110. },
  111. "throttle_period" : "15m", <1>
  112. "actions" : {
  113. "email_administrator" : {
  114. "email" : {
  115. "to" : "sys.admino@host.domain",
  116. "subject" : "Encountered {{ctx.payload.hits.total.value}} errors",
  117. "body" : "Too many error in the system, see attached data",
  118. "attachments" : {
  119. "attached_data" : {
  120. "data" : {
  121. "format" : "json"
  122. }
  123. }
  124. },
  125. "priority" : "high"
  126. }
  127. },
  128. "notify_pager" : {
  129. "webhook" : {
  130. "method" : "POST",
  131. "host" : "pager.service.domain",
  132. "port" : 1234,
  133. "path" : "/{{watch_id}}",
  134. "body" : "Encountered {{ctx.payload.hits.total.value}} errors"
  135. }
  136. }
  137. }
  138. }
  139. --------------------------------------------------
  140. // CONSOLE
  141. <1> There will be at least 15 minutes between subsequent action executions
  142. (applies to both `email_administrator` and `notify_pager` actions)
  143. If you do not define a throttle period at the action or watch level, the global
  144. default throttle period is applied. Initially, this is set to 5 seconds. To
  145. change the global default, configure the `xpack.watcher.execution.default_throttle_period`
  146. setting in `elasticsearch.yml`:
  147. [source,yaml]
  148. --------------------------------------------------
  149. xpack.watcher.execution.default_throttle_period: 15m
  150. --------------------------------------------------
  151. {watcher} also supports acknowledgement-based throttling. You can acknowledge a
  152. watch using the {ref}/watcher-api-ack-watch.html[Ack Watch API] to prevent the
  153. watch actions from being executed again while the watch condition remains `true`.
  154. This essentially tells {watcher} "I received the notification and I'm handling
  155. it, please do not notify me about this error again". An acknowledged watch action
  156. remains in the `acked` state until the watch's condition evaluates to `false`.
  157. When that happens, the action's state changes to `awaits_successful_execution`.
  158. To acknowledge an action, you use the
  159. {ref}/watcher-api-ack-watch.html[Ack Watch API]:
  160. [source,js]
  161. ----------------------------------------------------------------------
  162. POST _watcher/watch/<id>/_ack/<action_ids>
  163. ----------------------------------------------------------------------
  164. // CONSOLE
  165. // TEST[skip:https://github.com/elastic/x-plugins/issues/2513]
  166. Where `<id>` is the id of the watch and `<action_ids>` is a comma-separated list
  167. of the action ids you want to acknowledge. To acknowledge all actions, omit the
  168. `actions` parameter.
  169. The following diagram illustrates the throttling decisions made for each action
  170. of a watch during its execution:
  171. image::images/action-throttling.jpg[align="center"]
  172. [[action-conditions]]
  173. === Adding conditions to actions
  174. When a watch is triggered, its condition determines whether or not to execute the
  175. watch actions. Within each action, you can also add a condition per action. These
  176. additional conditions enable a single alert to execute different actions depending
  177. on a their respective conditions. The following watch would always send an email, when
  178. hits are found from the input search, but only trigger the `notify_pager` action when
  179. there are more than 5 hits in the search result.
  180. [source,js]
  181. --------------------------------------------------
  182. PUT _watcher/watch/log_event_watch
  183. {
  184. "trigger" : {
  185. "schedule" : { "interval" : "5m" }
  186. },
  187. "input" : {
  188. "search" : {
  189. "request" : {
  190. "indices" : "log-events",
  191. "body" : {
  192. "size" : 0,
  193. "query" : { "match" : { "status" : "error" } }
  194. }
  195. }
  196. }
  197. },
  198. "condition" : {
  199. "compare" : { "ctx.payload.hits.total.value" : { "gt" : 0 } }
  200. },
  201. "actions" : {
  202. "email_administrator" : {
  203. "email" : {
  204. "to" : "sys.admino@host.domain",
  205. "subject" : "Encountered {{ctx.payload.hits.total.value}} errors",
  206. "body" : "Too many error in the system, see attached data",
  207. "attachments" : {
  208. "attached_data" : {
  209. "data" : {
  210. "format" : "json"
  211. }
  212. }
  213. },
  214. "priority" : "high"
  215. }
  216. },
  217. "notify_pager" : {
  218. "condition": { <1>
  219. "compare" : { "ctx.payload.hits.total.value" : { "gt" : 5 } }
  220. },
  221. "webhook" : {
  222. "method" : "POST",
  223. "host" : "pager.service.domain",
  224. "port" : 1234,
  225. "path" : "/{{watch_id}}",
  226. "body" : "Encountered {{ctx.payload.hits.total.value}} errors"
  227. }
  228. }
  229. }
  230. }
  231. --------------------------------------------------
  232. // CONSOLE
  233. <1> A `condition` that only applies to the `notify_pager` action, which
  234. restricts its execution to when the condition succeeds (at least 5 hits in this case).
  235. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/actions/email.asciidoc
  236. include::actions/email.asciidoc[]
  237. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/actions/webhook.asciidoc
  238. include::actions/webhook.asciidoc[]
  239. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/actions/index.asciidoc
  240. include::actions/index.asciidoc[]
  241. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/actions/logging.asciidoc
  242. include::actions/logging.asciidoc[]
  243. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/actions/hipchat.asciidoc
  244. include::actions/hipchat.asciidoc[]
  245. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/actions/slack.asciidoc
  246. include::actions/slack.asciidoc[]
  247. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/actions/pagerduty.asciidoc
  248. include::actions/pagerduty.asciidoc[]
  249. :edit_url: https://github.com/elastic/elasticsearch/edit/{branch}/x-pack/docs/en/watcher/actions/jira.asciidoc
  250. include::actions/jira.asciidoc[]
  251. [float]
  252. [[actions-ssl-openjdk]]
  253. === Using SSL/TLS with OpenJDK
  254. As each distributor is free to choose how to package OpenJDK, it may happen,
  255. that even despite the exact same version, an OpenJDK distribution contains
  256. different parts under different Linux distributions.
  257. This can lead to issues with any action or input that uses TLS, like the `jira`,
  258. `pagerduty`, `slack`, `hipchat` or `webhook` one, because of missing CA certs.
  259. If you encounter TLS errors, when writing watches that connect to TLS endpoints,
  260. you should try to upgrade to the latest available OpenJDK distribution for your
  261. platform and if that does not help, try to upgrade to Oracle JDK.