创建告警规则

调用CreateAlert接口创建告警规则。

前提条件

您已完成以下操作:

参数说明

def create_alert(self, project, detail):

请求参数

名称

类型

是否必填

描述

示例值

project

String

Project名称。

demo-test-project

detail

Dictionary

包含创建告警规则的配置信息。

{
    "name": "alert-id-223456",
    "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
        }
    }
}

detail参数说明

名称

类型

是否必填

描述

示例值

name

String

告警名称,project下唯一。

alert-123456

displayName

String

告警显示名称。

test-alert

description

String

告警描述。

一条告警规则。

schedule

Dictionary

告警调度配置。

{
    "type": "FixedRate",
    "interval": "1m"
}

configuration

Dictionary

告警详细配置。

{
    "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
    }
}

schedule参数说明:

名称

类型

是否必填

描述

示例值

type

String

告警调度配置类型。支持FixedRateCron枚举值。

Cron

cronExpression

String

Cron表达式

0/5 * * * *

runImmediately

bool

定时任务是否立即执行。

False

timeZone

String

Cron 表达式所在时区,默认为空,表示东八区。

+0800

delay

int

定时类任务延迟执行(单位是秒:s)。

4

interval

String

固定间隔。

1m

configuration参数说明:

名称

类型

是否必填

描述

示例值

muteUntil

int

临时关闭时间。

1698907508

version

String

版本。

2.0

type

String

告警规则类型。

default

dashboard

String

告警绑定的仪表盘ID。

dasnboardExample

threshold

int

连续触发阈值。

1

noDataFire

bool

是否开启无数据告警。

False

noDataSeverity

int

无数据告警严重度。

6

sendResolved

bool

是否发送恢复告警。

False

autoAnnotation

bool

是否开启自动标注。

False

queryList

List

查询语句列表。列表变量说明,请参见AlertQuery

[{
    "storeType": "log",
    "region": "cn-hangzhou",
    "project": "demo-test-project",
    "store": "test-logstore",
    "query": "* | select count(*) cnt",
    "timeSpanType": "Truncated",
    "start": "-1m",
    "end": "absolute",
    "powerSqlMode": "auto"
}]

annotations

List

告警标注。列表变量说明,请参见AlertTag

[{
    "key": "title",
    "value": "Nginx Status Error"
}, {
    "key": "desc",
    "value": "Nginx Status Error, count: ${cnt}"
}]

labels

List

告警标签。列表变量说明,请参见AlertTag

[{
    "key": "service",
    "value": "nginx"
}]

templateConfiguration

Dictionary

告警模板配置。参数说明,请参见TemplateConfiguration

{
    "id": "sls.app.ack.ip.not_enough",
    "type": "sys",
    "lang": "cn"
}

conditionConfiguration

Dictionary

告警触发条件。参数说明,请参见ConditionConfiguration

{
    "condition": "cnt > 100",
    "countCondition": ""
}

severityConfigurations

List

告警严重度配置。列表变量说明,请参见SeverityConfiguration

[{
    "severity": 6,
    "evalCondition": {
        "condition": "cnt > 0",
        "countCondition": ""
    }
}]

joinConfigurations

List

告警查询分析语句集合操作配置。列表变量说明,请参见JoinConfiguration

[{
    "type": "left_join",
    "condition": "$0.id == $1.id"
}]

groupConfiguration

Dictionary

分组评估配置。参数说明,请参见GroupConfiguration

{
    "type": "no_group",
    "fields": []
}

policyConfiguration

Dictionary

SLS通知配置。参数说明,请参见PolicyConfiguration

{
    "alertPolicyId": "sls.builtin.dynamic",
    "actionPolicyId": "test-action-policy",
    "repeatInterval": "1m",
    "useDefault": False
}

sinkEventStore

Dictionary

告警发送给事件库配置。参数说明,请参见SinkEventStoreConfiguration

{
    "enabled": TRUE,
    "endpoint": "cn-shanghai-intranet.log.aliyuncs.com",
    "project": "exampleProject",
    "eventStore": "exampleStore",
    "roleArn": "acs:ram::123456789:role/aliyunlogetlrole"
}

sinkCms

Dictionary

告警发送给云监控事件中心配置。参数说明,请参见SinkCmsConfiguration

{
    "enabled": True
}

sinkAlerthub

Dictionary

告警发送给告警管理服务配置。参数说明,请参见SinkAlerthubConfiguration

{
    "enabled": True
}

tags

List

告警自定义标签。

["host"]

返回参数

返回参数说明,请参见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()

相关文档