put-watch.asciidoc 5.6 KB

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