文档首页> 应用运维管理 AOM> API参考> API> 告警> 上报事件告警信息
更新时间:2024-05-14 GMT+08:00

上报事件告警信息

功能介绍

该接口用于上报对应用户的事件、告警。

调用方法

请参见如何调用API

URI

PUT /v2/{project_id}/push/events

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

租户从IAM申请到的projectid,一般为32位字符串。

表2 Query参数

参数

是否必选

参数类型

描述

action

String

接口请求动作。action=clear代表清除告警,不传或者传其他值默认为上报动作。

枚举值:

  • clear

请求参数

表3 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

从IAM服务获取的用户Token。

Content-Type

String

指定类型为application/json。

枚举值:

  • application/json

x-enterprise-prject-id

String

告警所属的企业项目id。

表4 请求Body参数

参数

是否必选

参数类型

描述

events

Array of EventModel objects

事件或者告警详情。

表5 EventModel

参数

是否必选

参数类型

描述

starts_at

Long

事件或者告警产生的时间,CST毫秒级时间戳。

ends_at

Long

事件或者告警清除的时间,CST毫秒级时间戳,为0时表示未删除。

timeout

Long

告警自动清除时间。毫秒数,例如一分钟则填写为60000。默认清除时间为3天,对应数字为 4320 * 1000(即:3天 * 24小时 * 60分钟 * 1000毫秒)。

metadata

Map<String,String>

事件或者告警的详细信息,为键值对形式。必须字段为:

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

  • event_severity:事件级别枚举值。类型为String,四种类型 "Critical", "Major", "Minor", "Info";

  • event_type:事件类别枚举值。类型为String,event为告警事件,alarm为普通告警;

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

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

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

annotations

Map<String,Object>

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

attach_rule

Map<String,Object>

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

id

String

事件或者告警id,系统会自动生成,上报无须填写该字段。

响应参数

状态码: 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
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");

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

        AomClient client = AomClient.newBuilder()
                .withCredential(auth)
                .withRegion(AomRegion.valueOf("<YOUR REGION>"))
                .build();
        PushEventsRequest request = new PushEventsRequest();
        request.withAction(PushEventsRequest.ActionEnum.fromValue("<action>"));
        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
# coding: utf-8

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 = __import__('os').getenv("CLOUD_SDK_AK")
    sk = __import__('os').getenv("CLOUD_SDK_SK")

    credentials = BasicCredentials(ak, sk) \

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

    try:
        request = PushEventsRequest()
        request.action = "<action>"
        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")

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

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

    request := &model.PushEventsRequest{}
	actionRequest:= model.GetPushEventsRequestActionEnum().<ACTION>
	request.Action = &actionRequest
	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 被请求的服务无效。建议直接修改该请求,不要重试该请求。

错误码

请参见错误码