Create or update a watch Generally available

PUT /_watcher/watch/{id}

When a watch is registered, a new document that represents the watch is added to the .watches index and its trigger is immediately registered with the relevant trigger engine. Typically for the schedule trigger, the scheduler is the trigger engine.

IMPORTANT: You must use Kibana or this API to create a watch. Do not add a watch directly to the .watches index by using the Elasticsearch index API. If Elasticsearch security features are enabled, do not give users write privileges on the .watches index.

When you add a watch you can also define its initial active state by setting the active parameter.

When Elasticsearch security features are enabled, your watch can index or search only on indices for which the user that stored the watch has privileges. If the user is able to read index a, but not index b, the same will apply when the watch runs.

Required authorization

  • Cluster privileges: manage_watcher

Path parameters

  • id string Required

    The identifier for the watch.

Query parameters

  • active boolean

    The initial state of the watch. The default value is true, which means the watch is active by default.

  • if_primary_term number

    only update the watch if the last operation that has changed the watch has the specified primary term

  • if_seq_no number

    only update the watch if the last operation that has changed the watch has the specified sequence number

  • version number

    Explicit version number for concurrency control

application/json

Body

  • actions object

    The list of actions that will be run if the condition matches.

    Hide actions attribute Show actions attribute object
    • * object Additional properties
      Hide * attributes Show * attributes object
      • action_type string

        Values are email, webhook, index, logging, slack, or pagerduty.

      • condition object
        Hide condition attributes Show condition attributes object
        • always object
        • array_compare object
          Hide array_compare attribute Show array_compare attribute object
          • * object Additional properties
            Hide * attribute Show * attribute object
            • path string Required
        • compare object
          Hide compare attribute Show compare attribute object
          • * object Additional properties
        • never object
        • script object
          Hide script attributes Show script attributes object
          • lang string

            Any of:

            Values are painless, expression, mustache, or java.

          • params object
            Hide params attribute Show params attribute object
            • * object Additional properties
          • source string | object

            One of:
          • id string
      • foreach string
      • max_iterations number
      • name string
      • throttle_period string

        A duration. Units can be nanos, micros, ms (milliseconds), s (seconds), m (minutes), h (hours) and d (days). Also accepts "0" without a unit and "-1" to indicate an unspecified value.

      • throttle_period_in_millis number

        Time unit for milliseconds

      • transform object
        Hide transform attributes Show transform attributes object
        • chain array[object]
        • script object
          Hide script attributes Show script attributes object
          • lang string
          • params object
            Hide params attribute Show params attribute object
            • * object Additional properties
          • source string | object

            One of:
          • id string
      • index object
        Hide index attributes Show index attributes object
        • index string Required
        • doc_id string
        • refresh string

          Values are true, false, or wait_for.

        • op_type string

          Values are index or create.

        • timeout string

          A duration. Units can be nanos, micros, ms (milliseconds), s (seconds), m (minutes), h (hours) and d (days). Also accepts "0" without a unit and "-1" to indicate an unspecified value.

        • execution_time_field string

          Path to field or array of paths. Some API's support wildcards in the path to select multiple fields.

      • logging object
        Hide logging attributes Show logging attributes object
        • level string
        • text string Required
        • category string
      • email object
        Hide email attributes Show email attributes object
      • pagerduty object
        Hide pagerduty attributes Show pagerduty attributes object
        • account string
        • attach_payload boolean Required
        • client string
        • client_url string
        • contexts array[object]
          Hide contexts attributes Show contexts attributes object
          • href string
          • src string
          • type string Required

            Values are link or image.

        • description string Required
        • event_type string

          Values are trigger, resolve, or acknowledge.

        • incident_key string Required
        • proxy object
          Hide proxy attributes Show proxy attributes object
          • host string
          • port number
      • slack object
        Hide slack attributes Show slack attributes object
        • account string
        • message object Required
          Hide message attributes Show message attributes object
          • attachments array[object] Required
            Hide attachments attributes Show attachments attributes object
            • author_icon string
            • author_name string Required
            • color string
            • fallback string
            • fields array[object]
            • image_url string
            • pretext string
            • text string
            • thumb_url string
            • title string Required
            • ts
          • dynamic_attachments object
            Hide dynamic_attachments attributes Show dynamic_attachments attributes object
            • attachment_template object Required
              Hide attachment_template attributes Show attachment_template attributes object
              • author_icon string
              • author_name string Required
              • color string
              • fallback string
              • fields array[object]
              • image_url string
              • pretext string
              • text string
              • thumb_url string
              • title string Required
              • ts
            • list_path string Required
          • from string Required
          • icon string
          • text string Required
          • to array[string] Required
      • webhook object
        Hide webhook attributes Show webhook attributes object
        • auth object
          Hide auth attribute Show auth attribute object
          • basic object Required
            Hide basic attributes Show basic attributes object
            • password string Required
            • username string Required
        • body string
        • connection_timeout string

          A duration. Units can be nanos, micros, ms (milliseconds), s (seconds), m (minutes), h (hours) and d (days). Also accepts "0" without a unit and "-1" to indicate an unspecified value.

        • headers object
          Hide headers attribute Show headers attribute object
          • * string Additional properties
        • host string
        • method string

          Values are head, get, post, put, or delete.

        • params object
          Hide params attribute Show params attribute object
          • * string Additional properties
        • path string
        • port number
        • proxy object
          Hide proxy attributes Show proxy attributes object
          • host string Required
          • port number Required
        • read_timeout string

          A duration. Units can be nanos, micros, ms (milliseconds), s (seconds), m (minutes), h (hours) and d (days). Also accepts "0" without a unit and "-1" to indicate an unspecified value.

        • scheme string

          Values are http or https.

        • url string
  • condition object
    Hide condition attributes Show condition attributes object
    • always object
    • array_compare object
      Hide array_compare attribute Show array_compare attribute object
      • * object Additional properties
        Hide * attribute Show * attribute object
        • path string Required
    • compare object
      Hide compare attribute Show compare attribute object
      • * object Additional properties
    • never object
    • script object
      Hide script attributes Show script attributes object
      • lang string

        Any of:

        Values are painless, expression, mustache, or java.

      • params object
        Hide params attribute Show params attribute object
        • * object Additional properties
      • source string | object

        One of:
      • id string
  • input object
    Hide input attributes Show input attributes object
    • chain object
      Hide chain attribute Show chain attribute object
      • inputs array[object] Required
        Hide inputs attribute Show inputs attribute object
        • * object
    • http object
      Hide http attributes Show http attributes object
      • extract array[string]
      • request object
        Hide request attributes Show request attributes object
        • auth object
          Hide auth attribute Show auth attribute object
          • basic object Required
            Hide basic attributes Show basic attributes object
            • password string Required
            • username string Required
        • body string
        • connection_timeout string

          A duration. Units can be nanos, micros, ms (milliseconds), s (seconds), m (minutes), h (hours) and d (days). Also accepts "0" without a unit and "-1" to indicate an unspecified value.

        • headers object
          Hide headers attribute Show headers attribute object
          • * string Additional properties
        • host string
        • method string

          Values are head, get, post, put, or delete.

        • params object
          Hide params attribute Show params attribute object
          • * string Additional properties
        • path string
        • port number
        • proxy object
          Hide proxy attributes Show proxy attributes object
          • host string Required
          • port number Required
        • read_timeout string

          A duration. Units can be nanos, micros, ms (milliseconds), s (seconds), m (minutes), h (hours) and d (days). Also accepts "0" without a unit and "-1" to indicate an unspecified value.

        • scheme string

          Values are http or https.

        • url string
      • response_content_type string

        Values are json, yaml, or text.

    • simple object
      Hide simple attribute Show simple attribute object
      • * object Additional properties
  • metadata object
    Hide metadata attribute Show metadata attribute object
    • * object Additional properties
  • throttle_period string

    A duration. Units can be nanos, micros, ms (milliseconds), s (seconds), m (minutes), h (hours) and d (days). Also accepts "0" without a unit and "-1" to indicate an unspecified value.

  • throttle_period_in_millis number

    Time unit for milliseconds

  • transform object
    Hide transform attributes Show transform attributes object
    • chain array[object]
    • script object
      Hide script attributes Show script attributes object
      • lang string
      • params object
        Hide params attribute Show params attribute object
        • * object Additional properties
      • source string | object

        One of:
      • id string
  • trigger object
    Hide trigger attribute Show trigger attribute object
    • schedule object
      Hide schedule attributes Show schedule attributes object
      • timezone string
      • cron string
      • daily object
        Hide daily attribute Show daily attribute object
        • at array[string | object] Required

          A time of day, expressed either as hh:mm, noon, midnight, or an hour/minutes structure.

          A time of day, expressed either as hh:mm, noon, midnight, or an hour/minutes structure.

          One of:
      • hourly object
        Hide hourly attribute Show hourly attribute object
        • minute array[number] Required
      • interval string

        A duration. Units can be nanos, micros, ms (milliseconds), s (seconds), m (minutes), h (hours) and d (days). Also accepts "0" without a unit and "-1" to indicate an unspecified value.

      • monthly object | array[object]

        One of:
        Hide attributes Show attributes object
        • at array[string] Required
        • on array[number] Required
      • weekly object | array[object]

        One of:
        Hide attributes Show attributes object
        • at array[string] Required
        • on array[string] Required

          Values are sunday, monday, tuesday, wednesday, thursday, friday, or saturday.

      • yearly object | array[object]

        One of:
        Hide attributes Show attributes object
        • at array[string] Required
        • int array[string] Required

          Values are january, february, march, april, may, june, july, august, september, october, november, or december.

        • on array[number] Required

Responses

  • 200 application/json
    Hide response attributes Show response attributes object
    • created boolean Required
    • _id string Required
    • _primary_term number Required
    • _seq_no number Required
    • _version number Required
PUT _watcher/watch/my-watch
{
  "trigger" : {
    "schedule" : { "cron" : "0 0/1 * * * ?" }
  },
  "input" : {
    "search" : {
      "request" : {
        "indices" : [
          "logstash*"
        ],
        "body" : {
          "query" : {
            "bool" : {
              "must" : {
                "match": {
                  "response": 404
                }
              },
              "filter" : {
                "range": {
                  "@timestamp": {
                    "from": "{{ctx.trigger.scheduled_time}}||-5m",
                    "to": "{{ctx.trigger.triggered_time}}"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "condition" : {
    "compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}
  },
  "actions" : {
    "email_admin" : {
      "email" : {
        "to" : "[email protected]",
        "subject" : "404 recently encountered"
      }
    }
  }
}
resp = client.watcher.put_watch(
    id="my-watch",
    trigger={
        "schedule": {
            "cron": "0 0/1 * * * ?"
        }
    },
    input={
        "search": {
            "request": {
                "indices": [
                    "logstash*"
                ],
                "body": {
                    "query": {
                        "bool": {
                            "must": {
                                "match": {
                                    "response": 404
                                }
                            },
                            "filter": {
                                "range": {
                                    "@timestamp": {
                                        "from": "{{ctx.trigger.scheduled_time}}||-5m",
                                        "to": "{{ctx.trigger.triggered_time}}"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    condition={
        "compare": {
            "ctx.payload.hits.total": {
                "gt": 0
            }
        }
    },
    actions={
        "email_admin": {
            "email": {
                "to": "[email protected]",
                "subject": "404 recently encountered"
            }
        }
    },
)
const response = await client.watcher.putWatch({
  id: "my-watch",
  trigger: {
    schedule: {
      cron: "0 0/1 * * * ?",
    },
  },
  input: {
    search: {
      request: {
        indices: ["logstash*"],
        body: {
          query: {
            bool: {
              must: {
                match: {
                  response: 404,
                },
              },
              filter: {
                range: {
                  "@timestamp": {
                    from: "{{ctx.trigger.scheduled_time}}||-5m",
                    to: "{{ctx.trigger.triggered_time}}",
                  },
                },
              },
            },
          },
        },
      },
    },
  },
  condition: {
    compare: {
      "ctx.payload.hits.total": {
        gt: 0,
      },
    },
  },
  actions: {
    email_admin: {
      email: {
        to: "[email protected]",
        subject: "404 recently encountered",
      },
    },
  },
});
response = client.watcher.put_watch(
  id: "my-watch",
  body: {
    "trigger": {
      "schedule": {
        "cron": "0 0/1 * * * ?"
      }
    },
    "input": {
      "search": {
        "request": {
          "indices": [
            "logstash*"
          ],
          "body": {
            "query": {
              "bool": {
                "must": {
                  "match": {
                    "response": 404
                  }
                },
                "filter": {
                  "range": {
                    "@timestamp": {
                      "from": "{{ctx.trigger.scheduled_time}}||-5m",
                      "to": "{{ctx.trigger.triggered_time}}"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "condition": {
      "compare": {
        "ctx.payload.hits.total": {
          "gt": 0
        }
      }
    },
    "actions": {
      "email_admin": {
        "email": {
          "to": "[email protected]",
          "subject": "404 recently encountered"
        }
      }
    }
  }
)
$resp = $client->watcher()->putWatch([
    "id" => "my-watch",
    "body" => [
        "trigger" => [
            "schedule" => [
                "cron" => "0 0/1 * * * ?",
            ],
        ],
        "input" => [
            "search" => [
                "request" => [
                    "indices" => array(
                        "logstash*",
                    ),
                    "body" => [
                        "query" => [
                            "bool" => [
                                "must" => [
                                    "match" => [
                                        "response" => 404,
                                    ],
                                ],
                                "filter" => [
                                    "range" => [
                                        "@timestamp" => [
                                            "from" => "{{ctx.trigger.scheduled_time}}||-5m",
                                            "to" => "{{ctx.trigger.triggered_time}}",
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
        "condition" => [
            "compare" => [
                "ctx.payload.hits.total" => [
                    "gt" => 0,
                ],
            ],
        ],
        "actions" => [
            "email_admin" => [
                "email" => [
                    "to" => "[email protected]",
                    "subject" => "404 recently encountered",
                ],
            ],
        ],
    ],
]);
curl -X PUT -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d '{"trigger":{"schedule":{"cron":"0 0/1 * * * ?"}},"input":{"search":{"request":{"indices":["logstash*"],"body":{"query":{"bool":{"must":{"match":{"response":404}},"filter":{"range":{"@timestamp":{"from":"{{ctx.trigger.scheduled_time}}||-5m","to":"{{ctx.trigger.triggered_time}}"}}}}}}}}},"condition":{"compare":{"ctx.payload.hits.total":{"gt":0}}},"actions":{"email_admin":{"email":{"to":"[email protected]","subject":"404 recently encountered"}}}}' "$ELASTICSEARCH_URL/_watcher/watch/my-watch"
Request example
Run `PUT _watcher/watch/my-watch` add a watch. The watch schedule triggers every minute. The watch search input looks for any 404 HTTP responses that occurred in the last five minutes. The watch condition checks if any search hits where found. When found, the watch action sends an email to an administrator.
{
  "trigger" : {
    "schedule" : { "cron" : "0 0/1 * * * ?" }
  },
  "input" : {
    "search" : {
      "request" : {
        "indices" : [
          "logstash*"
        ],
        "body" : {
          "query" : {
            "bool" : {
              "must" : {
                "match": {
                  "response": 404
                }
              },
              "filter" : {
                "range": {
                  "@timestamp": {
                    "from": "{{ctx.trigger.scheduled_time}}||-5m",
                    "to": "{{ctx.trigger.triggered_time}}"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "condition" : {
    "compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}
  },
  "actions" : {
    "email_admin" : {
      "email" : {
        "to" : "[email protected]",
        "subject" : "404 recently encountered"
      }
    }
  }
}