This topic provides some sample scripts to show you how to call and test an alert API.
Call an OCP alert API
The following script shows how to call the API that pushes alerts to OCP, so that OCP can process the alerts.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import base64
import requests
import json
data = {
"alarmType": "your_alarm_type",
"labels": {"key":"value"},
"target":"alarm_target",
}
base64userpass = base64.b64encode('{0}:{1}'.format('username', 'password'))
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
"Authorization": "Basic %s" % base64userpass,
}
resp = requests.post(url='http://xxx.xxx.xxx.xxx:8080/api/v2/alarm/alarms', headers=headers, data=json.dumps(data))
jresp = json.loads(resp.text)
print(jresp)
Send an alert from OCP
After an alert is generated in OCP, the alert message is sent through a specified alert channel. You can use a shell script or a Python scrip to configure an alert channel. The following example shows how to send an alert message through an alert channel. The send_alarm function retrieves the values of alert-related environment variables from the os.environ object. For a list of supported environment variables, see OCP alert template variables.
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():
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())
Test an alert
To test if an alert can be pushed to a third-party platform, take one of the following approaches.
You can trigger an alert in OCP by performing the following steps:
Suspend OCP Agent to trigger an agent-unavailable alert, for example, the
no_enough_exporteralert.Check if the third-party platform receives the alert.
The Alert Channel Configuration module of OCP provides a testing feature. You can start a test by clicking
Send Test Message . For more information, see Create an alert channel.You can call the API that pushes alerts to OCP, and then use an alert channel script to send the alert to a third-party platform.
View alerts through APIs
This topic describes how to view OceanBase Cloud Platform (OCP) alerts on your alert platform.
Background
You must call APIs to query OCP alerts on your alert platform. For more information, see Query the alert event list.
Scenario 1: View real-time alerts
The following sample code shows how to view real-time OCP alerts. You can call the corresponding API on a regular basis to query the alert event list and learn of the alert objects in real time.
Sample code
curl 'http://OCP-IP:8080/api/v2/alarm/alarms?isSubscribedByMe=false&status=Active&page=1&size=10' \
--user username:password \
--compressed \
--insecure
The data.page.totalPages field indicates the total pages of real-time alerts. You can view the alerts on a specific page. For example, you can set the page field to 2 and view the alerts on Page 2:
curl 'http://OCP-IP:8080/api/v2/alarm/alarms?isSubscribedByMe=false&status=Active&page=2&size=10' \
--user username:password \
--compressed \
--insecure
Key fields
| Field | Description |
|---|---|
| alarmType | The name of the alert rule. |
| activeAt | The time when the alert was triggered, in Greenwich Mean Time (GMT). You need to convert it to the local time before use. |
| updatedAt | The time when the alert was updated, in GMT. You need to convert it to the local time before use. |
| target | The alert object, such as a tenant of a cluster. |
| description | The description of the alert. |
| summary | The summary of the alert. |
| level | The alert level. |
| labels | Other information, such as the cluster (obregion or ob_cluster in previous versions), tenant (tenant_name), or host IP address (svr_ip). A labels field suffixed with _1, _2, or _3 is in non-standard format. We recommend that you use this format only when necessary. This field is not used in later versions of OCP. For more information about supported labels, see Examples of alert channel configuration. |
Scenario 2: Connect to your alert platform
Based on the sample code in Scenario 1, you can call the corresponding API on a regular basis to query the alert event list and update the status of the alert object (target).
The following information is updated:
The last alert time. You can calculate the duration of an alert based on the time when the alert was triggered.
The alert status. If the alert of a target is not returned for each batch, the alert is set to the cleared state. When you query multiple pages, alerts on these pages are returned in a batch.
The number of alerts of each alert level in the Alerting state in real time.
Scenario 3: Verify alert availability
To prevent hidden dangers due to alert unavailability, you can trigger an alert on a regular basis based on the following sample code to verify the alert trace availability.
Sample code
You can trigger a log alert. For example, you can write the following ERROR log to the runtime log of an OBServer:
echo '[2035-01-02 15:04:05.666666] ERROR [CLOG] update_free_quota (ob_log_file_pool.cpp:413) [1994][2072][Y0-0000000000000000] [lt=19] [dc=0] test ob error for ocp alarm, just ignore. ret=-999999' >> /home/admin/oceanbase/log/observer.log.wfRun the following command to call the API and verify the log alert:
Set the keyword in the request to the log content.
If the alert is displayed on your alert platform, the alert trace is working.