To set up a Watcher in Elasticsearch to alert you based on a specific query, you need to create a watch that monitors your data and triggers an alert when certain conditions are met. Here's a step-by-step guide to help you achieve this:
Prerequisites
- Elasticsearch and Kibana: Ensure you have Elasticsearch and Kibana installed and running.
- Watcher Enabled: Watcher is a feature of the Elastic Stack that requires a Gold or Platinum license. Ensure you have the appropriate license, or you’re within the trial period.
- Permissions: You need sufficient permissions to create watches. Typically, you should have the
manage_watcher cluster privilege.
Step 1: Define Your Query Criteria
Before creating a watch, determine the query that represents the condition you want to monitor. This could be:
- A search for specific log messages.
- An aggregation that computes metrics (like counts or averages).
- Any Elasticsearch query that represents an event or condition you're interested in.
Example Query: Suppose you want to alert when there are more than 10 error logs in the last 5 minutes.
Step 2: Create the Watch Using the Dev Tools Console
You can create watches using the Kibana Dev Tools Console or via the Elasticsearch API.
Example Watch Configuration
Below is an example watch that sends an email alert when the number of error logs exceeds a threshold:
PUT _watcher/watch/error_log_alert
{
"trigger": {
"schedule": {
"interval": "5m"
}
},
"input": {
"search": {
"request": {
"indices": [
"logs-*"
],
"body": {
"query": {
"bool": {
"filter": [
{
"range": {
"@timestamp": {
"from": "now-5m"
}
}
},
{
"match": {
"log_level": "error"
}
}
]
}
}
}
}
}
},
"condition": {
"compare": {
"ctx.payload.hits.total.value": {
"gt": 10
}
}
},
"actions": {
"send_email": {
"email": {
"profile": "standard",
"to": [
"your_email@example.com"
],
"subject": "Error Alert: High Number of Errors Detected",
"body": {
"text": "There have been more than 10 errors in the last 5 minutes."
}
}
}
}
}
Explanation:
-
Trigger:
- The watch runs every 5 minutes (
"interval": "5m").
-
Input:
- Executes a search request on indices matching
logs-*.
- The search looks for documents with:
- A
@timestamp in the last 5 minutes.
- A
log_level of error.
-
Condition:
- Checks if the total number of hits (
ctx.payload.hits.total.value) is greater than 10.
-
Actions:
- If the condition is met, it sends an email to
your_email@example.com.
Step 3: Customize the Watch for Your Needs
Modify the example watch to suit your specific requirements:
- Indices: Replace
"logs-*" with the indices you want to monitor.
- Query: Adjust the query to match the conditions you're interested in.
- Condition: Change the comparison operator and value as needed.
- Actions: Define what should happen when the condition is met (e.g., email, webhook, Slack message).
Step 4: Save the Watch
After customizing the watch, save it by running the PUT command in the Kibana Dev Tools Console.
Step 5: Test the Watch
Before relying on the watch, test it to ensure it works as expected.
Simulate the Watch
You can execute the watch immediately using the following command:
POST _watcher/watch/error_log_alert/_execute
This will run the watch and display the results, including whether the condition was met and what actions were taken.
Step 6: Monitor and Manage Watches
List All Watches
To see all configured watches:
GET _watcher/watch
Get a Specific Watch
GET _watcher/watch/error_log_alert
Delete a Watch
If you need to remove a watch:
DELETE _watcher/watch/error_log_alert
Update a Watch
To update an existing watch, use the PUT command with the watch ID and the new configuration.
Alternative: Use Kibana's Watcher UI
If you prefer using a graphical interface:
- Open Kibana and go to Management > Watcher.
- Click Create a watch.
- Use the Threshold Alert wizard or the Advanced watch editor.
- Follow the prompts to define your trigger, input, condition, and actions.
Advanced Watch Features
- Multiple Actions: You can define multiple actions (e.g., email, log, webhook) to execute when the condition is met.
- Transformations: Modify the payload data before evaluating the condition or executing actions.
- Throttling: Prevent sending too many alerts in a short period by configuring throttling settings.
- Scripts: Use scripts in conditions for complex logic.
Example: Watch with Aggregation and Slack Notification
Here's an example of a watch that uses an aggregation and sends a Slack message:
PUT _watcher/watch/high_cpu_alert
{
"trigger": {
"schedule": {
"interval": "1m"
}
},
"input": {
"search": {
"request": {
"indices": [
"metrics-*"
],
"body": {
"size": 0,
"query": {
"range": {
"@timestamp": {
"from": "now-1m"
}
}
},
"aggs": {
"avg_cpu": {
"avg": {
"field": "cpu_usage"
}
}
}
}
}
}
},
"condition": {
"compare": {
"ctx.payload.aggregations.avg_cpu.value": {
"gt": 80
}
}
},
"actions": {
"notify_slack": {
"slack": {
"message": {
"to": [
"#alerts"
],
"text": "High CPU Usage Alert: Average CPU usage is above 80%."
}
}
}
}
}
Additional Tips
- Use Time-Based Indices: If your data is time-based, ensure your queries filter by time to optimize performance.
- Security Considerations: Secure your watches, especially when they contain sensitive information like email addresses or authentication tokens.
- Logging Actions: Use the
logging action to write alerts to Elasticsearch, which you can then visualize in Kibana.
Further Reading and Resources
Need More Help?
If you have specific requirements or need assistance with complex watches, consider reaching out to the Elastic community forums or consulting the Elastic Stack documentation tailored to your version.
By following these steps, you should be able to set up a watcher in Elasticsearch that alerts you based on a query you define.