You can call the CreateAlert operation to create alert rules.
Prerequisites
Simple Log Service is activated.
Simple Log Service SDK for Python is initialized.
Parameters
def create_alert(self, project, detail):Request parameters
Parameter | Type | Required | Description | Example |
project | String | Yes | The name of the project. | demo-test-project |
detail | Dictionary | Yes | The configuration that includes the settings to create an alert rule. | |
Response parameters
For more information about response parameters, see CreateAlert.
Sample code
import os
from aliyun.log import LogClient
alert = {
"name": "alert-id-1234",
"displayName": "Alert for testing",
"type": "Alert",
"status": "Enabled",
"schedule": {
"type": "FixedRate",
"interval": "1m"
},
"configuration": {
"version": "2.0",
"type": "default",
"dashboard": "internal-alert-analysis",
"queryList": [{
"storeType": "log",
"region": "ap-southeast-1",
"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():
# In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
access_key_id = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID", "")
access_key_secret = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET", "")
# Specify a Simple Log Service endpoint.
endpoint = "ap-southeast-1.log.aliyuncs.com"
client = LogClient(endpoint, access_key_id, access_key_secret)
# The name of the project.
project = "demo-test-project"
res = client.create_alert(project, alert)
res.log_print()
if __name__ == "__main__":
main()
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.