ListAlertsRetrieve APIを呼び出して、指定したプロジェクトのアラートルールのリストを取得します。
前提条件
Simple Log Serviceが有効化されています。 詳細については、「
Simple Log Serviceの有効化Python用のSimple Log Service SDKが初期化されます。 詳細については、「Simple Log Service SDK For Pythonの初期化」をご参照ください。
パラメータ説明
def list_alert(self, project, offset=0, size=10):リクエストパラメーター
パラメーター | 型 | 必須 / 任意 | 説明 | 例 |
project | String | 対象 | プロジェクトの名前。 | demo-test-project |
オフセット | int | 非対象 | クエリの開始元となる行。 デフォルト値:0 | 0 |
サイズ | int | 非対象 | ページ化されたクエリに設定されたページあたりの行数。 最大値:200。 デフォルト値は 10 です。 | 10 |
レスポンスパラメーター
レスポンスパラメーターの詳細については、「ListAlerts」をご参照ください。
サンプルコード
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()
レスポンスの例
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関連ドキュメント
アラートルールに関連するAPIの詳細については、以下のトピックを参照してください。
詳細なサンプルコードについては、GitHubの「Alibaba Cloud Simple Log Service SDK For Python」をご参照ください。