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

统计事件告警信息

功能介绍

该接口用于分段统计指定条件下的事件、告警。

调用方法

请参见如何调用API

URI

POST /v2/{project_id}/events/statistic

表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参数

参数

参数类型

描述

step

Long

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

timestamps

Array of longs

统计结果对应的时间序列。

series

Array of EventSeries objects

事件或者告警不同级别相同时间序列对应的统计结果。

summary

Map<String,Integer>

各类告警信息的数量汇总

表8 EventSeries

参数

参数类型

描述

event_severity

String

事件或者告警级别枚举类型。

枚举值:

  • Critical

  • Major

  • Minor

  • Info

values

Array of integers

事件或者告警统计结果。

状态码: 400

表9 响应Body参数

参数

参数类型

描述

error_code

String

响应码。

error_msg

String

调用失败响应信息描述。

error_type

String

调用失败类型。

trace_id

String

请求id

状态码: 401

表10 响应Body参数

参数

参数类型

描述

error_code

String

响应码。

error_msg

String

调用失败响应信息描述。

error_type

String

调用失败类型。

trace_id

String

请求id

状态码: 403

表11 响应Body参数

参数

参数类型

描述

error_code

String

响应码。

error_msg

String

调用失败响应信息描述。

error_type

String

调用失败类型。

trace_id

String

请求id

状态码: 500

表12 响应Body参数

参数

参数类型

描述

error_code

String

响应码。

error_msg

String

调用失败响应信息描述。

error_type

String

调用失败类型。

trace_id

String

请求id

状态码: 503

表13 响应Body参数

参数

参数类型

描述

error_code

String

响应码。

error_msg

String

调用失败响应信息描述。

error_type

String

调用失败类型。

trace_id

String

请求id

请求示例

以步长step查询当前时间范围(time_range)内的事件、告警统计信息。

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

{
  "time_range" : "-1.-1.5",
  "step" : 60000
}

响应示例

状态码: 200

OK 请求响应成功。

{
  "series" : [ {
    "event_severity" : "Critical",
    "values" : [ 2, 3, 3, 1, 0 ]
  }, {
    "event_severity" : "Major",
    "values" : [ 4, 3, 5, 4, 0 ]
  }, {
    "event_severity" : "Minor",
    "values" : [ 3, 1, 1, 1, 0 ]
  }, {
    "event_severity" : "Info",
    "values" : [ 0, 0, 0, 0, 0 ]
  } ],
  "step" : 60000,
  "summary" : {
    "critical_count" : 9,
    "info_count" : 0,
    "major_count" : 16,
    "minor_count" : 6
  },
  "timestamps" : [ 1711788600000, 1711788660000, 1711788720000, 1711788780000, 1711788840000 ]
}

状态码: 400

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

{
  "error_code" : "AOM.08033002",
  "error_message" : "The request body is illegal",
  "trace_id" : ""
}

状态码: 401

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

{
  "error_code" : "AOM.0403",
  "error_message" : "auth failed.",
  "error_type" : "AUTH_FAILED",
  "trace_id" : null
}

状态码: 403

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

{
  "error_code" : "AOM.0403",
  "error_message" : "auth failed.",
  "error_type" : "AUTH_FAILED",
  "trace_id" : null
}

状态码: 500

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

{
  "error_code" : "APM.00000500",
  "error_message" : "Internal Server Error",
  "trace_id" : ""
}

状态码: 503

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

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

SDK代码示例

SDK代码示例如下。

以步长step查询当前时间范围(time_range)内的事件、告警统计信息。

 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
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.*;


public class CountEventsSolution {

    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();
        CountEventsRequest request = new CountEventsRequest();
        request.withType(CountEventsRequest.TypeEnum.fromValue("<type>"));
        EventQueryParam body = new EventQueryParam();
        body.withStep(60000L);
        body.withTimeRange("-1.-1.5");
        request.withBody(body);
        try {
            CountEventsResponse response = client.countEvents(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());
        }
    }
}

以步长step查询当前时间范围(time_range)内的事件、告警统计信息。

 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
# 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"]

    credentials = BasicCredentials(ak, sk)

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

    try:
        request = CountEventsRequest()
        request.type = "<type>"
        request.body = EventQueryParam(
            step=60000,
            time_range="-1.-1.5"
        )
        response = client.count_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)

以步长step查询当前时间范围(time_range)内的事件、告警统计信息。

 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
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.CountEventsRequest{}
	typeRequest:= model.GetCountEventsRequestTypeEnum().<TYPE>
	request.Type = &typeRequest
	request.Body = &model.EventQueryParam{
		Step: int64(60000),
		TimeRange: "-1.-1.5",
	}
	response, err := client.CountEvents(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 被请求的服务无效。建议直接修改该请求,不要重试该请求。

错误码

请参见错误码