文档首页/ 应用运维管理 AOM/ API参考/ API/ 告警/ 上报事件或告警信息
更新时间:2025-03-10 GMT+08:00

上报事件或告警信息

功能介绍

该接口用于上报事件或告警至AOM,同时支持清除告警信息。

调用方法

请参见如何调用API

URI

PUT /v2/{project_id}/push/events

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

项目ID,可以从控制台获取,也可以从调用API处获取。获取方式请参见:获取项目ID

表2 Query参数

参数

是否必选

参数类型

描述

action

String

接口请求动作:

  • 不传或者传其他值:代表上报告警或事件动作。该参数值默认为空,即默认上报告警或事件。

  • clear:代表清除告警动作。

枚举值:

  • clear

请求参数

表3 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

从IAM服务获取的用户Token。获取方式请参见:获取Token

Content-Type

String

消息体的类型(格式)。指定类型为“application/json”。

枚举值:

  • application/json

enterprise-project-id

String

告警所属的企业项目id。获取方式请参见:获取企业项目ID

如果不传该参数值,默认为default企业项目,ID为0。

表4 请求Body参数

参数

是否必选

参数类型

描述

events

Array of EventModel objects

事件或者告警详情。

表5 EventModel

参数

是否必选

参数类型

描述

starts_at

Long

指定上报的事件或者告警产生的时间。仅支持CST毫秒级时间戳。

例如:2024-10-16 16:03:01需要通过工具转换成CST毫秒级时间戳:1729065781000

当action值为空时,即上报事件或告警时需要时指定该参数。

ends_at

Long

指定清除的事件或者告警清除的时间。仅支持CST毫秒级时间戳。默认值为0,表示没有清除告警。

例如:2024-10-16 16:03:01需要通过工具转换成CST毫秒级时间戳:1729065781000

当action值为clear时,即清除告警时需要时指定该参数。

timeout

Long

指定AOM自动清除超期告警的时间段。单位:毫秒数。例如一分钟则填写为60000。默认清除超期5天的告警。对应毫秒数:7200 * 60000(即:5天 * 24小时 * 60分钟 * 60000毫秒)。最长时间不超过15天。当action值为空时,即上报告警时需要时指定该参数。

metadata

Map<String,String>

待上报的事件或者告警的详细信息,为key:value键值对形式。支持如下必填字段:

  • event_name:事件或者告警名称,类型为String;

  • event_severity:事件或告警级别。类型为String,支持四种级别:

    • Critical:紧急

    • Major:重要

    • Minor:次要

    • Info;:提示

  • event_type:事件或告警类别。类型为String,支持两种类别:

    • event:告警事件

    • alarm:普通告警

  • resource_provider:事件对应云服务名称。类型为String;

  • resource_type:事件对应资源类型。类型为String;

  • resource_id:事件对应资源信息。类型为String。

    metadata中的value长度为1到2048字符串。

annotations

Map<String,Object>

事件或者告警附加字段,可以为空。

attach_rule

Map<String,Object>

事件或者告警预留字段,可以为空。

id

String

事件或者告警id,产生事件或告警时,系统会自动生成。

当action值为clear时,即清除警时需要时指定该参数。上报事件或告警时无需传入该参数。

响应参数

状态码:400

表6 响应Body参数

参数

参数类型

描述

error_code

String

调用失败响应码。

error_msg

String

调用失败响应信息描述。

error_type

String

调用失败类型。

trace_id

String

请求id。

请求示例

上报一条名称为“test”、事件等级为“Major”且告警源为“ecs”的告警。

https://{EndPoint}/v2/{project_id}/push/events

{
  "events" : [ {
    "starts_at" : 1605232501000,
    "timeout" : 60000,
    "metadata" : {
      "event_name" : "test",
      "event_severity" : "Major",
      "event_type" : "alarm",
      "resource_provider" : "ecs",
      "resource_type" : "vm",
      "resource_id" : "ecs123"
    },
    "annotations" : {
      "alarm_probableCause_zh_cn" : "可能原因",
      "alarm_fix_suggestion_zh_cn" : "修复建议",
      "message" : "告警详情"
    },
    "attach_rule" : { }
  } ]
}

响应示例

状态码:400

BadRequest 非法请求。建议直接修改该请求,不要重试该请求。

{
  "error_code" : "AOM.0400",
  "error_msg" : "parse eventParam failed",
  "error_type" : "SC_BAD_REQUEST"
}

SDK代码示例

SDK代码示例如下。

上报一条名称为“test”、事件等级为“Major”且告警源为“ecs”的告警。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.aom.v2.region.AomRegion;
import com.huaweicloud.sdk.aom.v2.*;
import com.huaweicloud.sdk.aom.v2.model.*;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

public class PushEventsSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");
        String projectId = "{project_id}";

        ICredential auth = new BasicCredentials()
                .withProjectId(projectId)
                .withAk(ak)
                .withSk(sk);

        AomClient client = AomClient.newBuilder()
                .withCredential(auth)
                .withRegion(AomRegion.valueOf("<YOUR REGION>"))
                .build();
        PushEventsRequest request = new PushEventsRequest();
        EventList body = new EventList();
        Map<String, Object> listEventsAnnotations = new HashMap<>();
        listEventsAnnotations.put("alarm_probableCause_zh_cn", "可能原因");
        listEventsAnnotations.put("alarm_fix_suggestion_zh_cn", "修复建议");
        listEventsAnnotations.put("message", "告警详情");
        Map<String, String> listEventsMetadata = new HashMap<>();
        listEventsMetadata.put("event_name", "test");
        listEventsMetadata.put("event_severity", "Major");
        listEventsMetadata.put("event_type", "alarm");
        listEventsMetadata.put("resource_provider", "ecs");
        listEventsMetadata.put("resource_type", "vm");
        listEventsMetadata.put("resource_id", "ecs123");
        List<EventModel> listbodyEvents = new ArrayList<>();
        listbodyEvents.add(
            new EventModel()
                .withStartsAt(1605232501000L)
                .withTimeout(60000L)
                .withMetadata(listEventsMetadata)
                .withAnnotations(listEventsAnnotations)
                .withAttachRule()
        );
        body.withEvents(listbodyEvents);
        request.withBody(body);
        try {
            PushEventsResponse response = client.pushEvents(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

上报一条名称为“test”、事件等级为“Major”且告警源为“ecs”的告警。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkaom.v2.region.aom_region import AomRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkaom.v2 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]
    projectId = "{project_id}"

    credentials = BasicCredentials(ak, sk, projectId)

    client = AomClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(AomRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = PushEventsRequest()
        listAnnotationsEvents = {
            "alarm_probableCause_zh_cn": "可能原因",
            "alarm_fix_suggestion_zh_cn": "修复建议",
            "message": "告警详情"
        }
        listMetadataEvents = {
            "event_name": "test",
            "event_severity": "Major",
            "event_type": "alarm",
            "resource_provider": "ecs",
            "resource_type": "vm",
            "resource_id": "ecs123"
        }
        listEventsbody = [
            EventModel(
                starts_at=1605232501000,
                timeout=60000,
                metadata=listMetadataEvents,
                annotations=listAnnotationsEvents,
            )
        ]
        request.body = EventList(
            events=listEventsbody
        )
        response = client.push_events(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

上报一条名称为“test”、事件等级为“Major”且告警源为“ecs”的告警。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    aom "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")
    projectId := "{project_id}"

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithProjectId(projectId).
        Build()

    client := aom.NewAomClient(
        aom.AomClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.PushEventsRequest{}
	var listAnnotationsEvents = map[string]interface{}{
        "alarm_probableCause_zh_cn": "可能原因",
        "alarm_fix_suggestion_zh_cn": "修复建议",
        "message": "告警详情",
    }
	var listMetadataEvents = map[string]string{
        "event_name": "test",
        "event_severity": "Major",
        "event_type": "alarm",
        "resource_provider": "ecs",
        "resource_type": "vm",
        "resource_id": "ecs123",
    }
	startsAtEvents:= int64(1605232501000)
	timeoutEvents:= int64(60000)
	var listEventsbody = []model.EventModel{
        {
            StartsAt: &startsAtEvents,
            Timeout: &timeoutEvents,
            Metadata: listMetadataEvents,
            Annotations: listAnnotationsEvents,
        },
    }
	request.Body = &model.EventList{
		Events: listEventsbody,
	}
	response, err := client.PushEvents(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

204

OK 请求响应成功。

400

BadRequest 非法请求。建议直接修改该请求,不要重试该请求。

401

Unauthorized 在客户端提供认证信息后,返回该状态码,表明服务端指出客户端所提供的认证信息不正确或非法。

403

Forbidden 请求被拒绝访问。返回该状态码,表明请求能够到达服务端,且服务端能够理解用户请求,但是拒绝做更多的事情,因为该请求被设置为拒绝访问,建议直接修改该请求,不要重试该请求。

500

InternalServerError 表明服务端能被请求访问到,但是不能理解用户的请求。

503

ServiceUnavailable 被请求的服务无效。建议直接修改该请求,不要重试该请求。

错误码

请参见错误码