调用CreateAlert接口创建告警规则。
前提条件
您已完成以下操作:
参数说明
def create_alert(self, project, detail):
请求参数
名称 | 类型 | 是否必填 | 描述 | 示例值 |
project | String | 是 | Project名称。 | demo-test-project |
detail | Dictionary | 是 | 包含创建告警规则的配置信息。 |
|
返回参数
返回参数说明,请参见CreateAlert - 创建告警规则。
示例代码
import os
from aliyun.log import LogClient
alert = {
"name": "alert-id-1234",
"displayName": "Alert for testing",
"type": "Alert",
"state": "Enabled",
"schedule": {
"type": "FixedRate",
"interval": "1m"
},
"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
}
}
}
def main():
# 本示例从环境变量中获取AccessKey ID和AccessKey Secret
access_key_id = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID", "")
access_key_secret = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET", "")
# 日志服务的服务接入点
endpoint = "cn-hangzhou.log.aliyuncs.com"
client = LogClient(endpoint, access_key_id, access_key_secret)
# Project名称
project = "demo-test-project"
res = client.create_alert(project, alert)
res.log_print()
if __name__ == "__main__":
main()
相关文档
文档内容是否对您有帮助?