execute-watch.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. [role="xpack"]
  2. [[watcher-api-execute-watch]]
  3. === Execute Watch API
  4. The execute watch API forces the execution of a stored watch. It can be used to
  5. force execution of the watch outside of its triggering logic, or to simulate the
  6. watch execution for debugging purposes.
  7. [float]
  8. ==== Request
  9. `POST _xpack/watcher/watch/<watch_id>/_execute` +
  10. `POST _xpack/watcher/watch/_execute`
  11. [float]
  12. ==== Description
  13. For testing and debugging purposes, you also have fine-grained control on how
  14. the watch runs. You can execute the watch without executing all of its actions
  15. or alternatively by simulating them. You can also force execution by ignoring
  16. the watch condition and control whether a watch record would be written to the
  17. watch history after execution.
  18. [float]
  19. [[watcher-api-execute-inline-watch]]
  20. ===== Inline Watch Execution
  21. You can use the Execute API to execute watches that are not yet registered by
  22. specifying the watch definition inline. This serves as great tool for testing
  23. and debugging your watches prior to adding them to {watcher}.
  24. [float]
  25. ==== Path Parameters
  26. `watch_id`::
  27. (string) Identifier for the watch.
  28. [float]
  29. ==== Query Parameters
  30. `debug`::
  31. (boolean) Defines whether the watch runs in debug mode. The default value is
  32. `false`.
  33. [float]
  34. ==== Request Body
  35. This API supports the following fields:
  36. [cols=",^,^,", options="header"]
  37. |======
  38. | Name | Required | Default | Description
  39. | `trigger_data` | no | | This structure is parsed as the data of the trigger event
  40. that will be used during the watch execution
  41. | `ignore_condition` | no | false | When set to `true`, the watch execution uses the
  42. {xpack-ref}/condition-always.html[Always Condition].
  43. This can also be specified as a HTTP parameter.
  44. | `alternative_input` | no | null | When present, the watch uses this object as a payload
  45. instead of executing its own input.
  46. | `action_modes` | no | null | Determines how to handle the watch actions as part of the
  47. watch execution. See <<watcher-api-execute-watch-action-mode, Action Execution Modes>>
  48. for more information.
  49. | `record_execution` | no | false | When set to `true`, the watch record representing the watch
  50. execution result is persisted to the `.watcher-history`
  51. index for the current time. In addition, the status of the
  52. watch is updated, possibly throttling subsequent executions.
  53. This can also be specified as a HTTP parameter.
  54. | `watch` | no | null | When present, this
  55. {xpack-ref}/how-watcher-works.html#watch-definition[watch] is used
  56. instead of the one specified in the request. This watch is
  57. not persisted to the index and record_execution cannot be set.
  58. |======
  59. [float]
  60. [[watcher-api-execute-watch-action-mode]]
  61. ===== Action Execution Modes
  62. Action modes define how actions are handled during the watch execution. There
  63. are five possible modes an action can be associated with:
  64. [options="header"]
  65. |======
  66. | Name | Description
  67. | `simulate` | The action execution is simulated. Each action type
  68. define its own simulation operation mode. For example, the
  69. {xpack-ref}/actions-email.html[email] action creates
  70. the email that would have been sent but does not actually
  71. send it. In this mode, the action might be throttled if the
  72. current state of the watch indicates it should be.
  73. | `force_simulate` | Similar to the the `simulate` mode, except the action is
  74. not be throttled even if the current state of the watch
  75. indicates it should be.
  76. | `execute` | Executes the action as it would have been executed if the
  77. watch would have been triggered by its own trigger. The
  78. execution might be throttled if the current state of the
  79. watch indicates it should be.
  80. | `force_execute` | Similar to the `execute` mode, except the action is not
  81. throttled even if the current state of the watch indicates
  82. it should be.
  83. | `skip` | The action is skipped and is not executed or simulated.
  84. Effectively forces the action to be throttled.
  85. |======
  86. [float]
  87. ==== Authorization
  88. You must have `manage_watcher` cluster privileges to use this API. For more
  89. information, see {xpack-ref}/security-privileges.html[Security Privileges].
  90. [float]
  91. ==== Security Integration
  92. When {security} is enabled on your Elasticsearch cluster, then watches will be
  93. executed with the privileges of the user that stored the watches. If your user
  94. is allowed to read index `a`, but not index `b`, then the exact same set of
  95. rules will apply during execution of a watch.
  96. When using the execute watch API, the authorization data of the user that
  97. called the API will be used as a base, instead of of the information who stored
  98. the watch.
  99. [float]
  100. ==== Examples
  101. The following example executes the `my_watch` watch:
  102. [source,js]
  103. --------------------------------------------------
  104. POST _xpack/watcher/watch/my_watch/_execute
  105. --------------------------------------------------
  106. // CONSOLE
  107. // TEST[setup:my_active_watch]
  108. The following example shows a comprehensive example of executing the `my-watch` watch:
  109. [source,js]
  110. --------------------------------------------------
  111. POST _xpack/watcher/watch/my_watch/_execute
  112. {
  113. "trigger_data" : { <1>
  114. "triggered_time" : "now",
  115. "scheduled_time" : "now"
  116. },
  117. "alternative_input" : { <2>
  118. "foo" : "bar"
  119. },
  120. "ignore_condition" : true, <3>
  121. "action_modes" : {
  122. "my-action" : "force_simulate" <4>
  123. },
  124. "record_execution" : true <5>
  125. }
  126. --------------------------------------------------
  127. // CONSOLE
  128. // TEST[setup:my_active_watch]
  129. <1> The triggered and schedule times are provided.
  130. <2> The input as defined by the watch is ignored and instead the provided input
  131. is used as the execution payload.
  132. <3> The condition as defined by the watch is ignored and is assumed to
  133. evaluate to `true`.
  134. <4> Forces the simulation of `my-action`. Forcing the simulation means that
  135. throttling is ignored and the watch is simulated by {watcher} instead of
  136. being executed normally.
  137. <5> The execution of the watch creates a watch record in the watch history,
  138. and the throttling state of the watch is potentially updated accordingly.
  139. This is an example of the output:
  140. [source,js]
  141. --------------------------------------------------
  142. {
  143. "_id": "my_watch_0-2015-06-02T23:17:55.124Z", <1>
  144. "watch_record": { <2>
  145. "watch_id": "my_watch",
  146. "node": "my_node",
  147. "messages": [],
  148. "trigger_event": {
  149. "type": "manual",
  150. "triggered_time": "2015-06-02T23:17:55.124Z",
  151. "manual": {
  152. "schedule": {
  153. "scheduled_time": "2015-06-02T23:17:55.124Z"
  154. }
  155. }
  156. },
  157. "state": "executed",
  158. "status": {
  159. "version": 1,
  160. "execution_state": "executed",
  161. "state": {
  162. "active": true,
  163. "timestamp": "2015-06-02T23:17:55.111Z"
  164. },
  165. "last_checked": "2015-06-02T23:17:55.124Z",
  166. "last_met_condition": "2015-06-02T23:17:55.124Z",
  167. "actions": {
  168. "test_index": {
  169. "ack": {
  170. "timestamp": "2015-06-02T23:17:55.124Z",
  171. "state": "ackable"
  172. },
  173. "last_execution": {
  174. "timestamp": "2015-06-02T23:17:55.124Z",
  175. "successful": true
  176. },
  177. "last_successful_execution": {
  178. "timestamp": "2015-06-02T23:17:55.124Z",
  179. "successful": true
  180. }
  181. }
  182. }
  183. },
  184. "input": {
  185. "simple": {
  186. "payload": {
  187. "send": "yes"
  188. }
  189. }
  190. },
  191. "condition": {
  192. "always": {}
  193. },
  194. "result": { <3>
  195. "execution_time": "2015-06-02T23:17:55.124Z",
  196. "execution_duration": 12608,
  197. "input": {
  198. "type": "simple",
  199. "payload": {
  200. "foo": "bar"
  201. },
  202. "status": "success"
  203. },
  204. "condition": {
  205. "type": "always",
  206. "met": true,
  207. "status": "success"
  208. },
  209. "actions": [
  210. {
  211. "id": "test_index",
  212. "index": {
  213. "response": {
  214. "index": "test",
  215. "type": "test2",
  216. "version": 1,
  217. "created": true,
  218. "result": "created",
  219. "id": "AVSHKzPa9zx62AzUzFXY"
  220. }
  221. },
  222. "status": "success",
  223. "type": "index"
  224. }
  225. ]
  226. },
  227. "user": "test_admin" <4>
  228. }
  229. }
  230. --------------------------------------------------
  231. // TESTRESPONSE[s/my_watch_0-2015-06-02T23:17:55.124Z/$body._id/]
  232. // TESTRESPONSE[s/"triggered_time": "2015-06-02T23:17:55.124Z"/"triggered_time": "$body.watch_record.trigger_event.triggered_time"/]
  233. // TESTRESPONSE[s/"scheduled_time": "2015-06-02T23:17:55.124Z"/"scheduled_time": "$body.watch_record.trigger_event.manual.schedule.scheduled_time"/]
  234. // TESTRESPONSE[s/"execution_time": "2015-06-02T23:17:55.124Z"/"execution_time": "$body.watch_record.result.execution_time"/]
  235. // TESTRESPONSE[s/"timestamp": "2015-06-02T23:17:55.111Z"/"timestamp": "$body.watch_record.status.state.timestamp"/]
  236. // TESTRESPONSE[s/"timestamp": "2015-06-02T23:17:55.124Z"/"timestamp": "$body.watch_record.status.actions.test_index.ack.timestamp"/]
  237. // TESTRESPONSE[s/"last_checked": "2015-06-02T23:17:55.124Z"/"last_checked": "$body.watch_record.status.last_checked"/]
  238. // TESTRESPONSE[s/"last_met_condition": "2015-06-02T23:17:55.124Z"/"last_met_condition": "$body.watch_record.status.last_met_condition"/]
  239. // TESTRESPONSE[s/"execution_duration": 12608/"execution_duration": "$body.watch_record.result.execution_duration"/]
  240. // TESTRESPONSE[s/"id": "AVSHKzPa9zx62AzUzFXY"/"id": "$body.watch_record.result.actions.0.index.response.id"/]
  241. // TESTRESPONSE[s/"node": "my_node"/"node": "$body.watch_record.node"/]
  242. <1> The id of the watch record as it would be stored in the `.watcher-history` index.
  243. <2> The watch record document as it would be stored in the `.watcher-history` index.
  244. <3> The watch execution results.
  245. <4> The user used to execute the watch.
  246. You can set a different execution mode for every action by associating the mode
  247. name with the action id:
  248. [source,js]
  249. --------------------------------------------------
  250. POST _xpack/watcher/watch/my_watch/_execute
  251. {
  252. "action_modes" : {
  253. "action1" : "force_simulate",
  254. "action2" : "skip"
  255. }
  256. }
  257. --------------------------------------------------
  258. // CONSOLE
  259. // TEST[setup:my_active_watch]
  260. You can also associate a single execution mode with all the actions in the watch
  261. using `_all` as the action id:
  262. [source,js]
  263. --------------------------------------------------
  264. POST _xpack/watcher/watch/my_watch/_execute
  265. {
  266. "action_modes" : {
  267. "_all" : "force_execute"
  268. }
  269. }
  270. --------------------------------------------------
  271. // CONSOLE
  272. // TEST[setup:my_active_watch]
  273. The following example shows how to execute a watch inline:
  274. [source,js]
  275. --------------------------------------------------
  276. POST _xpack/watcher/watch/_execute
  277. {
  278. "watch" : {
  279. "trigger" : { "schedule" : { "interval" : "10s" } },
  280. "input" : {
  281. "search" : {
  282. "request" : {
  283. "indices" : [ "logs" ],
  284. "body" : {
  285. "query" : {
  286. "match" : { "message": "error" }
  287. }
  288. }
  289. }
  290. }
  291. },
  292. "condition" : {
  293. "compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}
  294. },
  295. "actions" : {
  296. "log_error" : {
  297. "logging" : {
  298. "text" : "Found {{ctx.payload.hits.total}} errors in the logs"
  299. }
  300. }
  301. }
  302. }
  303. }
  304. --------------------------------------------------
  305. // CONSOLE
  306. All other settings for this API still apply when inlining a watch. In the
  307. following snippet, while the inline watch defines a `compare` condition,
  308. during the execution this condition will be ignored:
  309. [source,js]
  310. --------------------------------------------------
  311. POST _xpack/watcher/watch/_execute
  312. {
  313. "ignore_condition" : true,
  314. "watch" : {
  315. "trigger" : { "schedule" : { "interval" : "10s" } },
  316. "input" : {
  317. "search" : {
  318. "request" : {
  319. "indices" : [ "logs" ],
  320. "body" : {
  321. "query" : {
  322. "match" : { "message": "error" }
  323. }
  324. }
  325. }
  326. }
  327. },
  328. "condition" : {
  329. "compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}
  330. },
  331. "actions" : {
  332. "log_error" : {
  333. "logging" : {
  334. "text" : "Found {{ctx.payload.hits.total}} errors in the logs"
  335. }
  336. }
  337. }
  338. }
  339. }
  340. --------------------------------------------------
  341. // CONSOLE