X-Pack Watcher lets you trigger actions on your Alibaba Cloud Elasticsearch instance when specific conditions are met. For example, you can configure a watch that automatically sends a DingTalk message when an error log appears in your logs index.
Feature overview
X-Pack Watcher consists of four main components: a trigger that defines how often the watch runs, an input that retrieves data from the cluster, a condition that evaluates whether the data meets the alert threshold, and actions that execute when the condition is met, such as sending a webhook notification.
Prerequisites
You have created an Alibaba Cloud Elasticsearch instance. For more information, see Create an Alibaba Cloud Elasticsearch instance.
NoteFor the old network architecture, X-Pack Watcher supports only single-availability-zone Elasticsearch instances. Multi-availability-zone instances are not supported.
For the new network architecture, you must configure a PrivateLink connection for the instance to bypass network restrictions. For more information, see Configure a PrivateLink connection for an instance.
For more information about network architectures, see [Notice] Network architecture adjustment.
You have enabled X-Pack Watcher for your Elasticsearch instance. For more information, see Configure YML parameters.
You have created an ECS instance in your virtual private cloud (VPC). For more information, see Create an instance by using custom settings.
NoteX-Pack Watcher in Alibaba Cloud Elasticsearch cannot access the public internet directly. It communicates through the private endpoint of your instance within a VPC. Therefore, the ECS instance in your VPC must have public internet access, either by assigning an elastic IP address (EIP) or by configuring source network address translation (SNAT). For more information, see Associate an EIP or Configure SNAT.
Procedure
Step 1: Create and configure a DingTalk bot
Create a DingTalk group to receive alerts.
In the upper-right corner of the group, click the
icon. In the Group Settings panel, click Bot.In the Robot Management dialog box, click Add Robot, select Custom, and then click Add.
Enter a Robot Name, select Custom Keywords, and then enter your keywords.
ImportantThe keywords must be included in the alert message that you configure.
Select I have read and agree to the "Custom Robot Service and Disclaimer Terms", and click Complete.
Next to Webhook, click Copy to copy the bot's webhook URL.
Keep the webhook URL secure. Sharing it on external websites creates a security risk.
Step 2: Configure NGINX proxy and ECS security group
Configure the NGINX proxy on your ECS instance.
X-Pack Watcher sends alerts to an NGINX proxy on your ECS instance, which then forwards them to DingTalk or WeCom.
Install NGINX on the ECS instance.
Configure the nginx.conf file.
Replace the
serversection in the nginx.conf file with the following configuration.server { listen 8080;# Listener port server_name localhost;# Domain name index index.html index.htm index.php; root /usr/local/webserver/nginx/html;# Site directory location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ { expires 30d; # access_log off; } location / { proxy_pass <Webhook URL>; } location ~ .*\.(js|css)?$ { expires 15d; # access_log off; } access_log off; }Replace <Webhook URL> with the webhook URL of the DingTalk bot that you copied in Step 1.
Reload the configuration file and restart NGINX.
/usr/local/webserver/nginx/sbin/nginx -s reload # Reload the configuration file /usr/local/webserver/nginx/sbin/nginx -s reopen # Restart NGINX
Configure the ECS security group.
Allow inbound traffic from your Alibaba Cloud Elasticsearch instance to the NGINX proxy on the ECS instance.
Log on to the Alibaba Cloud ECS console.
In the left-side navigation pane, choose .
On the Cluster page, click the name of the target instance.
Click the Security Group tab.
On the Security Groups tab, click the name of the target security group.
On the Inbound tab, click Add Rule.
Configure the parameters.
Parameter
Description
Action
Select Permitted.
Priority
Keep the default value.
Protocol
Select Custom TCP.
Access Source
Enter the IP addresses of all nodes in your Alibaba Cloud Elasticsearch instance.
NoteTo get the IP addresses of the nodes, see View basic information about nodes.
Destination
Enter the port that NGINX is configured to listen on. This topic uses port 8080 as an example.
Description
Enter a description for the rule.
Click OK.
Step 3: Configure a Watcher alert
Log on to the Kibana console of your Elasticsearch cluster.
For instructions, see Log on to the Kibana console.
NoteExamples here use Elasticsearch V6.7.0. Operations may vary slightly for other versions.
In the left navigation menu, choose .
In the Console, run the following command to create a watch.
The following example creates a watch named
log_error_watch. It queries thelogsindex every 10 seconds for logs that containerror. If more than 0 matching logs are found, an alert is triggered.PUT _xpack/watcher/watch/log_error_watch { "trigger": { "schedule": { "interval": "10s" } }, "input": { "search": { "request": { "indices": ["logs"], "body": { "query": { "match": { "message": "error" } } } } } }, "condition": { "compare": { "ctx.payload.hits.total": { "gt": 0 } } }, "actions" : { "test_issue" : { "webhook" : { "method" : "POST", "url" : "http://<yourAddress>:8080", "body" : "{\"msgtype\": \"text\", \"text\": { \"content\": \"An error log has appeared. Please handle it as soon as possible.\"}}" } } } }Table 1. Key parameters
Parameter
Network type
Configuration
urlNew network architecture
Set this to the endpoint domain name. To obtain the endpoint domain name, see Configure a PrivateLink connection for an Alibaba Cloud Elasticsearch instance.
Old network architecture
Set this to one of the following addresses:
The IP address of the NGINX proxy: Requests are forwarded by the NGINX proxy in the same VPC over the public internet.
The webhook URL of the DingTalk bot.
bodyNew network architecture
Old network architecture
Configure this parameter based on the security settings of the DingTalk bot.
For example, in Step 1: Create and configure a DingTalk bot, if you set Security Settings to Custom Keywords and add the custom keyword error, the DingTalk bot pushes alert messages only if the content field in the body contains error.
NoteIf you see the error
No handler found for uri [/_xpack/watcher/watch/log_error_watch_2] and method [PUT]when you run the command, X-Pack Watcher is not enabled for your Alibaba Cloud Elasticsearch instance. You must enable it and then run the command again. For more information, see Configure YML parameters.
Step 4: Verify the alert
After the configuration is complete, your DingTalk group receives a message like An error log has appeared. Please handle it as soon as possible. whenever the alert condition from Step 3 is met.
If you no longer need the watch, you can run the following command to delete it.
DELETE _xpack/watcher/watch/log_error_watch