This topic provides examples of alert channel configuration for your reference.
DingTalk group messages
DingTalk group messages can be sent through the Webhook of a DingTalk Chatbot.
Procedure
Configure a chatbot for a DingTalk group.
To do this, you need to record the Webhook URL of the chatbot. A custom chatbot can be used only after you have completed the security settings. The HTTP channel of the OceanBase Cloud Platform (OCP) alerts supports only custom keywords and IP address (CIDR block) settings. The security settings for DingTalk Chatbot are shown in the following figure.
Set the custom keywords . The custom keywords must match the message template configured in the OCP alert channel. If the first line of the default template contains "OCP Alert Notification", you can set the keyword to "OCP Alert".
Set the IP address (CIDR block) . Note that the IP address must be the public IP address of the OCP server. You can run the
curl -w "\n\n" -s http://whatismijnip.nlcommand to view the public IP address of the OCP server. If your server does not have a fixed public IP address, contact your network administrator for the public CIDR block.
When you create an alert channel, select HTTP for the channel type, Phone Number for the Recipient field, and Yes for the Group Message Channel.
Configure the notification content.
Configure the channel.
Request method: POST.
URL template:
Content-Type:application/json; charset=utf-8
Note
The URL template is the Webhook URL recorded in Step 1.
Header template:
Content-Type:application/json; charset=utf-8Body template
{ "msgtype": "text", "text": { "content": ${message_json} }, "at": { "atMobiles": ${recipients_json_array}, "isAtAll": false } }
Note
The request body is in JSON format. When you reference variables that end with _json orjson_array, do not enclose them in quotation marks (") because the variable values have been serialized in JSON format and the quotation marks (") will result in a JSON format error. However, when you reference non-JSON variables, you must enclose them in quotation marks ("). For example "{message}".
DingTalk group messages in Markdown format
DingTalk supports messages in Markdown format. The following example uses Markdown to format rich-text messages. Except for several templates, the rich-text messages are configured in the same way as the text messages. For example, you can change the font color for messages by referencing ${alarm_level_color}.
For message types supported by DingTalk, see DingTalk Developer Guide.
Message template
<font color=${alarm_level_color}>OCP Alert Notification - Single Alert</font> > - Name: `${alarm_name}` > - Level: '${alarm_level}' > - Alert target: '${alarm_target}' > - Summary: '${alarm_summary}' > - Generation time: `${alarm_active_at}` > - Description: `${alarm_description}` > - [View in OCP](${alarm_url})Message aggregation template
<font color=${alarm_level_color}>OCP Alert Notification - Multiple Alerts</font> > - Name: `${alarm_name}` > - Level: `${alarm_level}` > - Alerts: '${alarm_count}' > - Aggregation group: '${alarm_group_by}' > - Alert target: `${alarm_target} > - Generation time: `${alarm_active_at}`Body template
{ "msgtype": "markdown", "markdown": { "title": "OCP DingTak message alert markdown test", "text": ${message_json} } }
Custom script channel
The custom script channel is designed for special scenarios. It is typically used when you cannot configure alerts in the HTTP channel or you have special alert format requirements. You can customize some special logic in the script. For example, you can customize a script channel to change the alert time or add default variables to third-party alert gateways.
Basic Information: Select Custom Script for the Channel Type, and set other options as needed.
Message template
OCP Alert Notification - Single Alert - Name: ${alarm_name} - Level: ${alarm_level} - Alert target: ${alarm_target} - Summary: ${alarm_summary} - Generation time: ${alarm_active_at} - Description: ${alarm_description} - OCP URL: ${alarm_url}Message aggregation template
OCP Alert Notification - Multiple Alerts - Name: ${alarm_name} - Level: ${alarm_level} - Alerts: ${alarm_count} - Aggregation group: ${alarm_group_by} - Alert target: ${alarm_target} - Generation time: ${alarm_active_at}Script file name: The system provides a default custom script sample
alarm_send_script_demo.sh. You can modify this script or refer to Python script example and Bash script example to create custom scripts. When you modify or create a script, make sure that the script file is stored in the/home/admindirectory of each OCP.Send test messages: When you debug a script, you can send a test message for testing. By default, the message contains the variable ${message} in the script. Make sure that the variable is included in the body of the message sent. The logic for determining whether a custom script is sent depends on the actual situation. The system determines that a script is sent as long as it is executed. However, in the developer mode of your browser, you can debug a script based on the response to the _test request of the test message sent.

Python script example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Note: The first line of the script must use a shebang to specify the program to use. Only Python and bash are supported.
import os
import sys
def send_alarm():
"""
You can use the os.environ function to obtain the values of alert-related variables from environment variables.
For more information about the supported variables, see Appendix 4 OCP alert template variables in the User Guide of OCP V3.1.2.
"""
message = os.environ['message']
print("Alert message: {}".format(message))
with open("./alarm_output.txt", "a") as file_object:
file_object.write("{}\n".format(message))
def main():
"""
Return 0 if succeeded.
Return a non-zero value if failed. The error is printed to stderr.
"""
try:
send_alarm()
print("send alarm success")
return 0
except Exception as e:
sys.stderr.write(str(e))
return 1
if __name__ == "__main__":
sys.exit(main())
Bash script example
#!/bin/env bash
# should contain shebang in the first line. Only python/bash are supported.
echo "Below is a list of alarm paras"
# The variables below can be referenced by prefix "$", for example, $alarm_name or ${alarm_name}
echo "alarm_name:$alarm_name"
echo "app_type:$app_type"
echo "alarm_threshold:$alarm_threshold"
echo "alarm_time=$alarm_time"
echo "alarm_last_interval:$alarm_last_interval"
echo "alarm_time:$alarm_time"
echo "alarm_level:$alarm_level"
echo "alarm_type:$alarm_type"
echo "alarm_summary:$alarm_summary"
echo "alarm_url:$alarm_url"
echo "app:$app"
echo "alarm_duration:$alarm_duration"
echo "alarm_status:$alarm_status"
echo "alarm_scope:$alarm_scope"
echo "alarm_active_at:$alarm_active_at"
echo "alarm_target:$alarm_target"
echo "alarm_description:$alarm_description"
echo "message:$message"
echo "receiver:$receiver"
echo "alarm_id:$alarm_id"
# This function defines to how to assemble custom requests.
# This demo shows you how to send alerts to DingTalk.
function send() {
# This token is a DingTalk group token. You need to apply and assign it to a variable token.
token=''
# URL="https://oapi.dingtalk.com/robot/send?access_token=$token"
URL=''
curl -X POST ${URL} -H 'Content-Type: application/json' -d '{"msgtype":"text","text":{"content":"'"${message}"'"}}'
return $?
}
# invoke function to
send
return $?