execute-watch.asciidoc 13 KB

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