put-watch.asciidoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. [role="xpack"]
  2. [[watcher-api-put-watch]]
  3. === Create or update watch API
  4. ++++
  5. <titleabbrev>Create or update watch</titleabbrev>
  6. ++++
  7. .New API reference
  8. [sidebar]
  9. --
  10. For the most up-to-date API details, refer to {api-es}/group/endpoint-watcher[{watcher} APIs].
  11. --
  12. Either registers a new watch in {watcher} or updates an existing one.
  13. [[watcher-api-put-watch-request]]
  14. ==== {api-request-title}
  15. `PUT _watcher/watch/<watch_id>`
  16. [[watcher-api-put-watch-prereqs]]
  17. ==== {api-prereq-title}
  18. * You must have `manage_watcher` cluster privileges to use this API. For more
  19. information, see <<security-privileges>>.
  20. [[watcher-api-put-watch-desc]]
  21. ==== {api-description-title}
  22. When a watch is registered, a new document that represents the watch is added to
  23. the `.watches` index and its trigger is immediately registered with the relevant
  24. trigger engine. Typically for the `schedule` trigger, the scheduler is the
  25. trigger engine.
  26. IMPORTANT: You must use {kib} or this API to create a watch. Do not add a watch
  27. directly to the `.watches` index using the Elasticsearch index API.
  28. If {es} {security-features} are enabled, do not give users `write`
  29. privileges on the `.watches` index.
  30. When adding a watch you can also define its initial
  31. <<watch-active-state,active state>>. You do that by setting the `active`
  32. parameter.
  33. [[watcher-api-put-watch-security]]
  34. ===== Security integration
  35. When {es} {security-features} are enabled, your watch can index or search only
  36. on indices for which the user that stored the watch has privileges. If the user
  37. is able to read index `a`, but not index `b`, the same will apply, when the watch
  38. is executed.
  39. [[watcher-api-put-watch-path-params]]
  40. ==== {api-path-parms-title}
  41. `<watch_id>`::
  42. (Required, string) Identifier for the watch.
  43. [[watcher-api-put-watch-query-params]]
  44. ==== {api-query-parms-title}
  45. `active`::
  46. (Optional, Boolean) Defines whether the watch is active or inactive by default.
  47. The default value is `true`, which means the watch is active by default.
  48. [[watcher-api-put-watch-request-body]]
  49. ==== {api-request-body-title}
  50. A watch has the following fields:
  51. [options="header"]
  52. |======
  53. | Name | Description
  54. | `trigger` | The <<trigger,trigger>> that defines when
  55. the watch should run.
  56. | `input` | The <<input,input>> that defines the input
  57. that loads the data for the watch.
  58. | `condition` | The <<condition,condition>> that defines if
  59. the actions should be run.
  60. | `actions` | The list of <<actions,actions>> that will be
  61. run if the condition matches
  62. | `transform` | The <<transform,transform>> that processes the
  63. watch payload to prepare it for the watch actions.
  64. | `metadata` | Metadata json that will be copied into the history entries.
  65. | `throttle_period` | The minimum time between actions being run, the default
  66. for this is 5 seconds. This default can be changed in the
  67. config file with the setting
  68. `xpack.watcher.throttle.period.default_period`. If both
  69. this value and the `throttle_period_in_millis` parameter
  70. are specified, {watcher} uses the last parameter
  71. included in the request.
  72. | `throttle_period_in_millis` | Minimum time in milliseconds between actions
  73. being run. Defaults to `5000`. If both this
  74. value and the `throttle_period` parameter are
  75. specified, {watcher} uses the last parameter
  76. included in the request.
  77. |======
  78. //[[watcher-api-put-watch-response-body]]
  79. //==== {api-response-body-title}
  80. //[[watcher-api-put-watch-response-codes]]
  81. //==== {api-response-codes-title}
  82. [[watcher-api-put-watch-example]]
  83. ==== {api-examples-title}
  84. The following example adds a watch with the `my-watch` id that has the following
  85. characteristics:
  86. * The watch schedule triggers every minute.
  87. * The watch search input looks for any 404 HTTP responses that occurred in the
  88. last five minutes.
  89. * The watch condition checks if any search hits where found.
  90. * When found, the watch action sends an email to an administrator.
  91. [source,console]
  92. --------------------------------------------------
  93. PUT _watcher/watch/my-watch
  94. {
  95. "trigger" : {
  96. "schedule" : { "cron" : "0 0/1 * * * ?" }
  97. },
  98. "input" : {
  99. "search" : {
  100. "request" : {
  101. "indices" : [
  102. "logstash*"
  103. ],
  104. "body" : {
  105. "query" : {
  106. "bool" : {
  107. "must" : {
  108. "match": {
  109. "response": 404
  110. }
  111. },
  112. "filter" : {
  113. "range": {
  114. "@timestamp": {
  115. "from": "{{ctx.trigger.scheduled_time}}||-5m",
  116. "to": "{{ctx.trigger.triggered_time}}"
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. },
  126. "condition" : {
  127. "compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}
  128. },
  129. "actions" : {
  130. "email_admin" : {
  131. "email" : {
  132. "to" : "admin@domain.host.com",
  133. "subject" : "404 recently encountered"
  134. }
  135. }
  136. }
  137. }
  138. --------------------------------------------------
  139. When you add a watch you can also define its initial
  140. <<watch-active-state,active state>>. You do that
  141. by setting the `active` parameter. The following command adds a watch and sets
  142. it to be inactive by default:
  143. [source,js]
  144. --------------------------------------------------
  145. PUT _watcher/watch/my-watch?active=false
  146. --------------------------------------------------
  147. NOTE: If you omit the `active` parameter, the watch is active by default.