Call the ListAlertsRetrieve API to retrieve a list of alert rules for a specified project.
Prerequisites
Simple Log Service is activated. For more information, see Activate Simple Log Service.
Simple Log Service SDK for Python is initialized. For more information, see Initialize Simple Log Service SDK for Python.
Parameter description
def list_alert(self, project, offset=0, size=10):
Request parameters
Parameter | Type | Required | Description | Example |
project | String | Yes | The name of the project. | demo-test-project |
offset | int | No | The line from which the query starts. Default value: 0. | 0 |
size | int | No | The number of rows per page set for paged query. Maximum value: 200. Default value: 10. | 10 |
Response parameters
For information about the response parameters, see ListAlerts.
Sample code
from aliyun.log import LogClient
import os
def main():
# In this example, the AccessKey ID and AccessKey Secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# Specify a Simple Log Service endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
project = 'demo-test-project'
client = LogClient(endpoint, accessKeyId, accessKey)
res = client.list_alert(project)
res.log_print()
if __name__ == '__main__':
main()
Sample response
ListResponse for alerts:
headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/json', 'Content-Length': '2917', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Mon, 04 Nov 2024 09:24:39 GMT', 'x-log-time': '1730712279', 'x-log-requestid': '672892D70EF2CAE571955AD9'}
count: 2
total: 2
alerts [{'name': 'alert-id-223456', 'type': 'Alert', 'apiType': 'Alert', 'status': 'ENABLED', 'description': '', 'displayName': 'Alert for testing', 'state': 'Enabled', 'recyclable': False, 'schedule': {'type': 'FixedRate', 'delay': 0, 'interval': '1m', 'runImmediately': False, 'timeZone': ''}, 'configuration': {'version': '2.0', 'type': 'default', 'dashboard': 'internal-alert-analysis', 'queryList': [{'storeType': 'log', 'region': 'cn-hangzhou', 'project': 'demo-test-project', 'store': 'test-logstore', 'query': '* | select count(*) cnt', 'timeSpanType': 'Truncated', 'start': '-1m', 'end': 'absolute', 'powerSqlMode': 'auto'}], 'groupConfiguration': {'type': 'no_group', 'fields': []}, 'joinConfigurations': [], 'severityConfigurations': [{'severity': 6, 'evalCondition': {'condition': 'cnt > 0', 'countCondition': ''}}], 'labels': [{'key': 'service', 'value': 'nginx'}], 'annotations': [{'key': 'title', 'value': 'Nginx Status Error'}, {'key': 'desc', 'value': 'Nginx Status Error, count: ${cnt}'}], 'autoAnnotation': True, 'sendResolved': False, 'threshold': 1, 'noDataFire': False, 'noDataSeverity': 6, 'policyConfiguration': {'alertPolicyId': 'sls.builtin.dynamic', 'actionPolicyId': 'test-action-policy', 'repeatInterval': '1m', 'useDefault': False}}, 'scheduleId': '668e7eee7c127******52cecb6817343', 'createTime': 1730712225, 'lastModifiedTime': 1730712225}, {'name': 'alert-id-123456', 'type': 'Alert', 'apiType': 'Alert', 'status': 'ENABLED', 'description': '', 'displayName': 'Nginx Status Error', 'state': 'Enabled', 'recyclable': False, 'schedule': {'type': 'FixedRate', 'delay': 0, 'interval': '1m', 'runImmediately': True, 'timeZone': ''}, 'configuration': {'annotations': [{'key': 'title', 'value': 'Nginx Status Error'}, {'key': 'desc', 'value': 'Nginx Status Error, count: ${cnt}'}], 'autoAnnotation': True, 'condition': '', 'conditionConfiguration': {}, 'dashboard': 'internal-alert-analysis', 'groupConfiguration': {'fields': [], 'type': 'no_group'}, 'joinConfigurations': [], 'labels': [{'key': 'service', 'value': 'nginx'}], 'noDataFire': False, 'noDataSeverity': 6, 'notifyThreshold': 1, 'policyConfiguration': {'actionPolicyId': 'test-action-policy', 'alertPolicyId': 'sls.builtin.dynamic', 'repeatInterval': '1m', 'useDefault': False}, 'queryList': [{'chartTitle': '', 'dashboardId': '', 'end': 'absolute', 'powerSqlMode': 'auto', 'project': 'demo-test-project', 'query': '* | select count(*) cnt', 'region': 'cn-hangzhou', 'start': '-1m', 'store': 'test-logstore', 'storeType': 'log', 'timeSpanType': 'Truncated', 'ui': '{}'}], 'sendRecoveryMessage': False, 'sendResolved': False, 'severityConfigurations': [{'evalCondition': {'condition': 'cnt > 0', 'countCondition': ''}, 'severity': 6}], 'sinkAlerthub': {'enabled': True}, 'sinkCms': {'enabled': False}, 'sinkEventStore': {'enabled': False, 'endpoint': '', 'eventStore': '', 'project': '', 'roleArn': ''}, 'tags': [], 'templateConfiguration': {'annotations': {}, 'tokens': {}}, 'threshold': 1, 'throttling': '1m', 'type': 'default', 'version': '2.0'}, 'scheduleId': '70d9a3a28ede4******461eff13ff8d5', 'createTime': 1730711770, 'lastModifiedTime': 1730711843}]
Process finished with exit code 0
References
For more information about the APIs related to alert rules, see the following topics:
For more sample code, see Alibaba Cloud Simple Log Service SDK for Python on GitHub.