This topic provides examples of alert channel configuration for your reference.
DingTalk alert channel
You can send group messages based on the webhook of a DingTalk group chatbot. To configure a DingTalk alert channel, perform the following two operations:
- Configure a chatbot for the DingTalk group for receiving alert notifications.
- Configure a DingTalk alert channel in OceanBase Cloud Platform (OCP).
Configure a chatbot for a DingTalk group
Open the DingTalk group for receiving alert notifications on your computer.
In the top navigation pane, click Group Settings.
In the panel that appears, choose Group Assistant > Add Robot.
In the ChatBot dialog box, click
and select the chatbot that you want to add.Click Custom. In the Robot details dialog box, click Add.
In the Add Robot dialog box, specify Chatbot name and Security Settings.
Options for Security Settings include Custom Keywords, Additional Signature, and IP Address. Select one of them as needed. For more information, see Custom chatbot security settings. In this example, select Custom Keywords.
Select I have read and accepted DingTalk Custom Robot Service Terms of Service and click Finished.
In the Add Robot dialog box, copy the webhook URL of the chatbot and click Finished.
Configure a DingTalk alert channel
Log on to the OCP console.
In the left-side navigation pane, click Alert Center.
Click the Alert Channels tab.
On the Alert Channels tab, click Create Channel.
Specify the parameters in the Basic Information section of the channel.
Parameter Description Channel Type Select DingTalk. Channel Name The name of the alert channel. Webhook URL The webhook URL generated after you configure the DingTalk chatbot, which is the URL that you copied in Step 8 in the Configure a chatbot for a DingTalk group section. Signature Key Optional. The signature key obtained after you select Additional Signature for Security Settings. Specified User Optional. The phone number of a DingTalk group member. Proxy Optional. Specify the proxy server that sends alerts, in the format of http:ip:port. In the format,iprepresents the IP address of the proxy server, andportrepresents the port that is assigned to send alerts. For example, if the IP address of the proxy server is xxx.xxx.xxx.xxx and port 80 is assigned, you can set Proxy tohttp:xxx.xxx.xxx.xxx:80.
Click Send Test Message.
If the message is sent, click Submit.
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 Channel Type, and set other options as needed.
Message template
OCP Alert Notification - Single Alert - Name: ${alarm_name} - Level: ${alarm_level} - Alert object: ${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 object: ${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 json
import requests
import os
import sys
ACCESS_TOKEN = "Ding Talk Token"
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 OCP alert template variables.
"""
message = os.environ['message']
data = {
"msgtype": "markdown",
"markdown": {
"text": message
}
}
resp = requests.post("https://oapi.dingtalk.com/robot/send?access_token=" + ACCESS_TOKEN, json=data)
resp.close()
response = json.loads(resp.text)
# Output the response to the standard error (stderr) or standard output (stdout) to verify whether the alert is successfully sent. stderr is verified first.
if not response['errcode'] == 0:
sys.stderr.write(resp.text)
else:
print(resp.text)
def main():
"""
Return 0 if succeeded.
Return a non-zero value if failed. The error is printed to stderr.
"""
try:
send_alarm()
return 0
except Exception as e:
sys.stderr.write(str(e))
return 1
if __name__ == "__main__":
sys.exit(main())
Bash script example
#!/usr/bin/env bash
# should contains shebang in first line, only python/bash are supported
# this function defines to how to assembly request by yourself according to your requirements
# this demo shows you how to send alarm to ding ding
function send() {
# this token is ding ding group token, please apply and assign it to variable token
token='Ding Talk Token'
URL="https://oapi.dingtalk.com/robot/send?access_token=$token"
# if message is json format, use "'"${message}"'" or "${message}", do not wrapper a new json body
# if message is not json format, use "${message}"
# do not use '${message}'
# print the response to stderr or stdout, which will be validated if success, validate stderr firstly.
curl -s -X POST ${URL} -H 'Content-Type: application/json' -d '{"msgtype":"text","text":{"content":"'"${message}"'"}}'
return $?
}
# invoke function to
send
Note
The following shebangs are supported: python, bash, sh, /bin/bash, /bin/sh, /usr/bin/python, /usr/bin/bash, /usr/bin/env python, and /usr/bin/env. The correct writing of a bash shebang is #!/bin/sh. We recommend that you use #!/usr/bin/env bash. The first matching command will be found in $PATH for execution.