X-Pack Watcher for Alibaba Cloud Elasticsearch can perform actions when specific conditions are met. For example, you can configure the system to automatically send a DingTalk message when an error log appears in the logs index. This topic describes how to configure a DingTalk chatbot to receive X-Pack Watcher alerts.
Prerequisites
An Alibaba Cloud Elasticsearch instance is created. For more information, see Create an Alibaba Cloud Elasticsearch instance.
NoteFor the old network architecture, X-Pack Watcher supports only single-zone Elasticsearch instances, not multi-zone instances.
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.
The X-Pack Watcher feature is enabled for your Elasticsearch instance. For more information, see Configure YML parameters.
An ECS instance is created in your Virtual Private Cloud (VPC). For more information, see Create an instance by using the custom purchase method.
NoteX-Pack Watcher for Alibaba Cloud Elasticsearch cannot communicate directly with the public internet. Instead, it must communicate through the instance's private endpoint within a Virtual Private Cloud (VPC). Therefore, the ECS instance in your VPC must have a public IP address or use source network address translation (SNAT) to access the public internet. For more information, see Associate an elastic IP address (EIP) or Configure SNAT.
Procedure
Step 1: Create and configure a DingTalk chatbot
Create a DingTalk group to receive alerts.
In the upper-right corner of the group, click the
icon. In the Group Settings panel, click Robot.In the Robot Management dialog box, click Add Bot, select Custom, and then click Add.
Enter a Bot Name, select Custom Keywords, and enter the keywords.
ImportantThe keywords must be included in the alert message that you configure.
Read and select Custom Robot Services and Disclaimers, and then click Complete.
Click Webhook next to Copy to copy the webhook URL of the chatbot.
Keep the webhook URL secure. Do not share it publicly to avoid security risks.
Step 2: Configure the Nginx proxy and security group
Configure the Nginx proxy on the ECS instance.
The Nginx proxy forwards alert messages. The X-Pack Watcher service sends alert messages to Nginx, which then forwards the messages to services such as 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:server { listen 8080;# The listening port. server_name localhost;# The domain name. index index.html index.htm index.php; root /usr/local/webserver/nginx/html;# The website 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 chatbot.
Reload the modified 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 security group for the ECS instance.
Allow the Nginx proxy on the ECS instance to receive alert messages from the Elasticsearch 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 Group List tab, click the ID of the target security group.
On the Inbound tab, click Add Rule.
Set the following parameters.
Parameter
Description
Action
Select Permitted.
Priority
Retain the default value.
Protocol
Select Custom TCP.
Source
Add the IP addresses of all nodes in your Alibaba Cloud Elasticsearch instance.
NoteTo obtain the IP addresses of the nodes, see View the basic information of a node.
Destination
Enter the port that Nginx is configured to listen on. In this example, port 8080 is used.
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.
In this example, a log_error_watch document is created. Every 10s, the document queries the logs index for error logs and triggers an alarm if the log count exceeds 0.
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://<your_ECS_IP_address>:8080", "body" : "{\"msgtype\": \"text\", \"text\": { \"content\": \"An error log is found. Please handle it as soon as possible.\"}}" } } } }Table 1. Key parameter descriptions
Parameter
Network architecture
Description
url
New network architecture
Set this parameter to the domain name of the endpoint. To obtain the endpoint domain name, see Configure private communication for an Alibaba Cloud Elasticsearch instance.
Old network architecture
Set this parameter to the following address:
The private IP address of the ECS instance where the Nginx proxy is running.
The public webhook URL of the DingTalk chatbot is not supported in this architecture.
body
New network architecture
Old network architecture
Configure this parameter based on the security settings of your DingTalk chatbot.
For example, if you set the Security Settings to Custom Keywords and add the custom keyword error in Step 1: Create and configure a DingTalk chatbot, the content field in the body must contain error for the DingTalk chatbot to accept and display the alert.
NoteIf you receive the
No handler found for uri [/_xpack/watcher/watch/log_error_watch_2] and method [PUT]error when you run the command, it indicates that X-Pack Watcher is not enabled for your Alibaba Cloud Elasticsearch instance. Enable this feature and run the command again. For more information, see Configure YML parameters.
Step 4: Verify the results
If the configuration is correct, you will receive an alert message such as An error log is found. Please handle it as soon as possible. in your DingTalk group when your cluster's data meets the conditions defined in Step 3.
If you no longer require the watch, run the following command to delete it.
DELETE _xpack/watcher/watch/log_error_watch