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

查询事件告警信息

功能介绍

该接口用于查询对应用户的事件、告警。

调用方法

请参见如何调用API

URI

POST /v2/{project_id}/events

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

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

表2 Query参数

参数

是否必选

参数类型

描述

type

String

查询类型。type=active_alert代表查询活动告警,type=history_alert代表查询历史告警。不传或者传其他值则返回指定查询条件的所有信息。

枚举值:

  • history_alert

  • active_alert

请求参数

表3 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

从IAM服务获取的用户Token。

Content-Type

String

指定类型为application/json。

枚举值:

  • application/json

表4 请求Body参数

参数

是否必选

参数类型

描述

time_range

String

timeRange用于指标查询时间范围,主要用于解决客户端时间和服务端时间不一致情况下,查询最近N分钟的数据。另可用于精确查询某一段时间的数据。

如:

  • -1.-1.60(表示最近60分钟),不管当前客户端是什么时间,都以服务端时间为准查询最近60分钟。

  • 1650852000000.1650852300000.5(表示GMT+8 2022-04-25 10:00:00至2022-04-25 10:05:00指定的5分钟)

格式:

startTimeInMillis.endTimeInMillis.durationInMinutes

参数解释:

  • startTimeInMillis: 查询的开始时间,格式为UTC毫秒,如果指定为-1,服务端将按(endTimeInMillis - durationInMinutes * 60 * 1000)计算开始时间。如-1.1650852300000.5,则相当于1650852000000.1650852300000.5

  • endTimeInMillis: 查询的结束时间,格式为UTC毫秒,如果指定为-1,服务端将按(startTimeInMillis + durationInMinutes * 60 * 1000)计算结束时间,如果计算出的结束时间大于当前系统时间,则使用当前系统时间。如1650852000000.-1.5,则相当于1650852000000.1650852300000.5

  • durationInMinutes:查询时间的跨度分钟数。 取值范围大于0并且大于等于(endTimeInMillis - startTimeInMillis) / (60 * 1000) - 1。当开始时间与结束时间都设置为-1时,系统会将结束时间设置为当前时间UTC毫秒值,并按(endTimeInMillis - durationInMinutes * 60 * 1000)计算开始时间。如:-1.-1.60(表示最近60分钟)

约束:

单次请求中,查询时长与周期需要满足以下条件: durationInMinutes * 60 / period <= 1440

step

Long

统计步长。毫秒数,例如一分钟则填写为60000。

search

String

模糊查询匹配字段,可以为空。如果值不为空,可以模糊匹配。metadata字段为必选字段。

sort

sort object

返回列表的排序方式,可以为空。

metadata_relation

Array of RelationModel objects

查询条件组合,可以为空。

表5 sort

参数

是否必选

参数类型

描述

order_by

Array of strings

排序字段列表。会根据列表中定义顺序对返回列表最排序。

order

String

排序方式枚举值。asc代表正序,desc代表倒序。

枚举值:

  • asc

  • desc

表6 RelationModel

参数

是否必选

参数类型

描述

key

String

指定查询字段的key,对应metadata里面的key 。

value

Array of strings

查询条件中指定key的值。

relation

String

该条件与其他条件的组合方式。 AND:必须满足所有条件; OR:可以满足其中一个条件; NOT:必须不满足所有条件。

枚举值:

  • AND

  • OR

  • NOT

响应参数

状态码: 200

表7 响应Body参数

参数

参数类型

描述

events

Array of EventModel objects

事件或者告警详情。

表8 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

表9 响应Body参数

参数

参数类型

描述

error_code

String

响应码。

枚举值:

  • AOM.0403

error_msg

String

调用失败响应信息描述。

枚举值:

  • Invalid projectId

error_type

String

调用失败类型。

枚举值:

  • Forbidden

状态码: 401

表10 响应Body参数

参数

参数类型

描述

error_code

String

响应码。

枚举值:

  • AOM.0403

error_msg

String

调用失败响应信息描述。

枚举值:

  • Invalid projectId

error_type

String

调用失败类型。

枚举值:

  • Forbidden

状态码: 403

表11 响应Body参数

参数

参数类型

描述

error_code

String

响应码。

枚举值:

  • AOM.0403

error_msg

String

调用失败响应信息描述。

枚举值:

  • Invalid projectId

error_type

String

调用失败类型。

枚举值:

  • Forbidden

状态码: 500

表12 响应Body参数

参数

参数类型

描述

error_code

String

响应码。

枚举值:

  • AOM.0403

error_msg

String

调用失败响应信息描述。

枚举值:

  • Invalid projectId

error_type

String

调用失败类型。

枚举值:

  • Forbidden

状态码: 503

表13 响应Body参数

参数

参数类型

描述

error_code

String

响应码。

枚举值:

  • AOM.0403

error_msg

String

调用失败响应信息描述。

枚举值:

  • Invalid projectId

error_type

String

调用失败类型。

枚举值:

  • Forbidden

请求示例

查询对应用户{project_id}的事件、告警列表。

https://{endpoint}/v2/{project_id}/events

{
  "time_range" : "-1.-1.30",
  "metadata_relation" : [ {
    "key" : "event_type",
    "relation" : "AND",
    "value" : [ "alarm" ]
  }, {
    "key" : "event_severity",
    "relation" : "AND",
    "value" : [ "Critical", "Major", "Minor", "Info" ]
  } ],
  "search" : "",
  "sort" : {
    "order_by" : [ "starts_at" ],
    "order" : "desc"
  }
}

响应示例

状态码: 200

OK 请求响应成功。

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

状态码: 400

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

{
  "error_code" : "AOM.0400",
  "error_message" : "param error",
  "error_type" : "SC_BAD_REQUEST"
}

状态码: 401

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

{
  "error_code" : "AOM.0401",
  "error_message" : "you dont have permission",
  "error_type" : "SC_UNAUTHORIZED"
}

状态码: 403

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

{
  "error_code" : "AOM.0403",
  "error_message" : "you dont have permission",
  "error_type" : "SC_FORBIDDEN"
}

状态码: 500

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

{
  "error_code" : "AOM.0500",
  "error_message" : "SC_INTERNAL_SERVER_ERROR",
  "error_type" : "SC_INTERNAL_SERVER_ERROR"
}

状态码: 503

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

{
  "error_code" : "AOM.0503",
  "error_message" : "SC_NOT_IMPLEMENTED",
  "error_type" : "SC_NOT_IMPLEMENTED"
}

SDK代码示例

SDK代码示例如下。

查询对应用户{project_id}的事件、告警列表。

 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
75
76
77
78
79
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;

public class ListEventsSolution {

    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();
        ListEventsRequest request = new ListEventsRequest();
        request.withType(ListEventsRequest.TypeEnum.fromValue("<type>"));
        EventQueryParam2 body = new EventQueryParam2();
        List<String> listMetadataRelationValue = new ArrayList<>();
        listMetadataRelationValue.add("Critical");
        listMetadataRelationValue.add("Major");
        listMetadataRelationValue.add("Minor");
        listMetadataRelationValue.add("Info");
        List<String> listMetadataRelationValue1 = new ArrayList<>();
        listMetadataRelationValue1.add("alarm");
        List<RelationModel> listbodyMetadataRelation = new ArrayList<>();
        listbodyMetadataRelation.add(
            new RelationModel()
                .withKey("event_type")
                .withValue(listMetadataRelationValue1)
                .withRelation(RelationModel.RelationEnum.fromValue("AND"))
        );
        listbodyMetadataRelation.add(
            new RelationModel()
                .withKey("event_severity")
                .withValue(listMetadataRelationValue)
                .withRelation(RelationModel.RelationEnum.fromValue("AND"))
        );
        List<String> listSortOrderBy = new ArrayList<>();
        listSortOrderBy.add("starts_at");
        EventQueryParam2Sort sortbody = new EventQueryParam2Sort();
        sortbody.withOrderBy(listSortOrderBy)
            .withOrder(EventQueryParam2Sort.OrderEnum.fromValue("desc"));
        body.withMetadataRelation(listbodyMetadataRelation);
        body.withSort(sortbody);
        body.withSearch("");
        body.withTimeRange("-1.-1.30");
        request.withBody(body);
        try {
            ListEventsResponse response = client.listEvents(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());
        }
    }
}

查询对应用户{project_id}的事件、告警列表。

 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
# 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 = os.getenv("CLOUD_SDK_AK")
    sk = 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 = ListEventsRequest()
        request.type = "<type>"
        listValueMetadataRelation = [
            "Critical",
            "Major",
            "Minor",
            "Info"
        ]
        listValueMetadataRelation1 = [
            "alarm"
        ]
        listMetadataRelationbody = [
            RelationModel(
                key="event_type",
                value=listValueMetadataRelation1,
                relation="AND"
            ),
            RelationModel(
                key="event_severity",
                value=listValueMetadataRelation,
                relation="AND"
            )
        ]
        listOrderBySort = [
            "starts_at"
        ]
        sortbody = EventQueryParam2Sort(
            order_by=listOrderBySort,
            order="desc"
        )
        request.body = EventQueryParam2(
            metadata_relation=listMetadataRelationbody,
            sort=sortbody,
            search="",
            time_range="-1.-1.30"
        )
        response = client.list_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)

查询对应用户{project_id}的事件、告警列表。

 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
75
76
77
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.ListEventsRequest{}
	typeRequest:= model.GetListEventsRequestTypeEnum().<TYPE>
	request.Type = &typeRequest
	var listValueMetadataRelation = []string{
        "Critical",
	    "Major",
	    "Minor",
	    "Info",
    }
	var listValueMetadataRelation1 = []string{
        "alarm",
    }
	keyMetadataRelation:= "event_type"
	relationMetadataRelation:= model.GetRelationModelRelationEnum().AND
	keyMetadataRelation1:= "event_severity"
	relationMetadataRelation1:= model.GetRelationModelRelationEnum().AND
	var listMetadataRelationbody = []model.RelationModel{
        {
            Key: &keyMetadataRelation,
            Value: &listValueMetadataRelation1,
            Relation: &relationMetadataRelation,
        },
        {
            Key: &keyMetadataRelation1,
            Value: &listValueMetadataRelation,
            Relation: &relationMetadataRelation1,
        },
    }
	var listOrderBySort = []string{
        "starts_at",
    }
	orderSort:= model.GetEventQueryParam2SortOrderEnum().DESC
	sortbody := &model.EventQueryParam2Sort{
		OrderBy: &listOrderBySort,
		Order: &orderSort,
	}
	searchEventQueryParam2:= ""
	request.Body = &model.EventQueryParam2{
		MetadataRelation: &listMetadataRelationbody,
		Sort: sortbody,
		Search: &searchEventQueryParam2,
		TimeRange: "-1.-1.30",
	}
	response, err := client.ListEvents(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

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

状态码

状态码

描述

200

OK 请求响应成功。

400

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

401

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

403

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

500

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

503

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

错误码

请参见错误码