
# 查询活动或历史告警列表 - ListActiveOrHistoryAlarms
#### 功能介绍
查询活动或历史告警列表。您可以在请求参数中灵活设置筛选条件，例如：告警的时间范围、告警级别和告警来源等，系统接收到请求后，会利用高效的索引机制对告警数据库进行快速检索，精准筛选出符合条件的活动告警或历史告警记录。
#### 调用方法
请参见[如何调用API](https://support.huaweicloud.com/api-lts/lts_api_0003.html)。
#### 授权信息
账号具备所有API的调用权限，如果使用账号下的IAM用户调用当前API，该IAM用户需具备调用API所需的权限。
- 如果使用角色与策略授权，具体权限要求请参见[权限和授权项](https://support.huaweicloud.com/api-lts/lts_api_0046.html)。
- 如果使用身份策略授权，需具备如下身份策略权限。
  
  | 授权项                 | 访问级别 | 资源类型（\*为必须） | 条件键 | 别名                | 依赖的授权项 |
  |:---|:---|:---|:---|:---|:---|
  | lts:alarm:listAlarm | List | -           | -   | lts:sqlalarms:get | -      |
     
  
 
#### URI
POST /v2/{project_id}/{domain_id}/lts/alarms/sql-alarm/query
表1路径参数 
| 参数         | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                             |
|:---|:---|:---|:---|
| project_id | 是    | String | **参数解释：** 项目ID，可以从调用API处获取，也可以从控制台获取。获取方式请参见：[获取项目ID](https://support.huaweicloud.com/api-lts/lts_api_0006.html)。 **约束限制：** 不涉及。 **取值范围：** 不涉及。 **默认取值：** 不涉及。 |
| domain_id  | 是    | String | **参数解释：** 账号ID，获取方式请参见：[获取账号ID](https://support.huaweicloud.com/api-lts/lts_api_0012.html)。 **约束限制：** 不涉及。 **取值范围：** 不涉及。 **默认取值：** 不涉及。                       |
   
表2Query参数 
| 参数     | 是否必选 | 参数类型    | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
|:---|:---|:---|:---|
| type   | 是    | String  | **参数解释：** 查询告警类型，有活动告警（active_alert）和历史告警（history_alert）。该参数不传或者传其他值时，则会返回满足其他查询条件的所有活动告警和历史告警。 **约束限制：** 不涉及。 **取值范围：** - active_alert：活动告警，指触发告警后，未被清除的告警。   - history_alert：历史告警，指被[手动清除](https://support.huaweicloud.com/usermanual-aom/aom_02_0004.html)的告警。    **默认取值：** 不涉及。 |
| marker | 否    | String  | **参数解释：** 分页标记，后续值为返回体中的next_marker参数。 **约束限制：** 不涉及。 **取值范围：** 不涉及。 **默认取值：** 0：分页标记的起始标识为0，表示从0开始对告警列表进行分页显示。                                                                                                                                                                                                                                                                                                                                                                                                         |
| limit  | 否    | Integer | **参数解释：** 查询告警数量限制值。 **约束限制：** 不涉及。 **取值范围：** - 最小值：0   - 最大值：1000    **默认取值：** 1000：依次返回1000个告警。                                                                                                                                                                                   |
   
#### 请求参数
表3请求Header参数 
| 参数           | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                      |
|:---|:---|:---|:---|
| X-Auth-Token | 是    | String | **参数解释：** 从IAM服务获取的用户Token，获取方式请参见：[获取用户Token](https://support.huaweicloud.com/api-lts/lts_api_0004.html)。 **约束限制：** 不涉及。 **取值范围：** 不涉及。 **默认取值：** 不涉及。 |
| Content-Type | 是    | String | **参数解释：** 用于定义消息体的格式，该字段填为：application/json;charset=utf8。 **约束限制：** 不涉及。 **取值范围：** 不涉及。 **默认取值：** 不涉及。                                                  |
   
表4请求Body参数 
| 参数                   | 是否必选 | 参数类型                                       | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
|:---|:---|:---|:---|
| whether_custom_field | 是    | Boolean                                    | **参数解释：** 是否自定义查询时间段。 **约束限制：** 不涉及。 **取值范围：** - true：自定义查询时间段，start_time和end_time字段生效，可自行设置时间区间。   - false：非自定义查询时间段，time_range字段生效，根据当前时间的相对时间进行告警查询。    **默认取值：** false                                                                                                                                                                                              |
| start_time           | 否    | Long                                       | **参数解释：** 自定义时间段查询告警的起始时间。 参数start_time和end_time组成的时间区间，既包含起始时间也包含结束时间。 **约束限制：** start_time和end_time取值不能相同。 **取值范围：** 毫秒级时间戳，表示从1970-1-1 00:00:00 UTC开始计算的毫秒数。 **默认取值：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                                              |
| end_time             | 否    | Long                                       | **参数解释：** 自定义时间段查询告警的结束时间。 参数start_time和end_time组成的时间区间，既包含起始时间也包含结束时间。 **约束限制：** start_time和end_time取值不能相同。 **取值范围：** 毫秒级时间戳，表示从1970-1-1 00:00:00 UTC开始计算的毫秒数。 **默认取值：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                                              |
| time_range           | 否    | String                                     | **参数解释：** 查询某一段时间的告警，主要用于解决客户端时间和服务端时间不一致的情况。 例如： - -1.-1.60(表示最近60分钟)，不管当前客户端是什么时间，都以服务端时间为准查询最近60分钟。   - 1650852000000.1650852300000.5(表示GMT+8 2022-04-25 10:00:00至2022-04-25 10:05:00指定的5分钟)。    **约束限制：** 单次请求中，查询时长与周期需要满足以下条件: durationInMinutes\*60/period\<=1440。 **取值范围：** 不涉及。 **默认取值：** 不涉及。 |
| search               | 否    | String                                     | **参数解释：** 模糊查询匹配字段，可以为空。如果值不为空，可以模糊匹配。 **约束限制：** 不涉及。 **取值范围：** metadata任意字段。 **默认取值：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| alarm_level_ids      | 否    | Array of strings                           | **参数解释：** 告警级别。支持的告警级别分别为Critical（紧急）、Major（重要）、Minor（次要）、Info（提示）。 **约束限制：** 只能填写支持的告警级别。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| sort                 | 否    | [Sort] object | **参数解释：** 响应结果列表的排序方式，可以为空。当sort为空时，默认按告警产生的时间（starts_at）进行排序。 **约束限制：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| step                 | 否    | Long                                       | **参数解释：** 统计步长（单位为毫秒），例如：一分钟则填写为60000。 **约束限制：** 不涉及。 **取值范围：** 不涉及。 **默认取值：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
   
 表5Sort 
| 参数       | 是否必选 | 参数类型             | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
|:---|:---|:---|:---|
| order_by | 是    | Array of strings | **参数解释：** 排序字段，根据告警产生时间（starts_at）对返回列表进行排序。数组长度为1，该参数只能填写starts_at。 **约束限制：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                   |
| order    | 是    | String           | **参数解释：** 排序方式。 **约束限制：** 不涉及。 **取值范围：** - asc：正序。   - desc：倒序。    **默认取值：** desc |
   
#### 响应参数
**状态码：200**
表6响应Body参数 
| 参数        | 参数类型                                                      | 描述                                              |
|:---|:---|:---|
| events    | Array of [Events] objects | **参数解释：** 告警详情。 |
| page_info | [PageInfo] object       | **参数解释：** 分页信息。 |
   
 表7Events 
| 参数          | 参数类型                                                      | 描述                                                                                                                                                                                                                                                                                                                                                                                                               |
|:---|:---|:---|
| annotations | [Annotations] object | **参数解释：** 告警附加字段。                                                                                                                                                                                                                                                                                                                                                                |
| metadata    | [Metadata] object       | **参数解释：** 告警信息。                                                                                                                                                                                                                                                                                                                                                                  |
| arrives_at  | Long                                                      | **参数解释：** 告警上报时间（单位为毫秒时间戳）。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                       |
| ends_at     | Long                                                      | **参数解释：** 告警清除时间（单位为毫秒时间戳），为0时表示未删除。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                              |
| id          | String                                                    | **参数解释：** 告警ID。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                   |
| starts_at   | Long                                                      | **参数解释：** 告警产生时间（单位为毫秒时间戳）。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                       |
| timeout     | Long                                                      | **参数解释：** 告警自动清除时间（单位为毫秒数），例如：一分钟则填写为60000。默认清除时间为5天，对应数字为7200 \* 60000（即：5天 \* 24小时 \* 60分钟 \* 60000毫秒）。 **取值范围：** 不涉及。                                                                                                                                                                                         |
| type        | String                                                    | **参数解释：** 告警规则类型。 **取值范围：** - SQL   - 关键词    |
   
 表8Annotations 
| 参数                              | 参数类型                                                        | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                      |
|:---|:---|:---|
| message                         | String                                                      | **参数解释：** 告警触发详情。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                        |
| log_info                        | String                                                      | **参数解释：** 日志组/日志流详情。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                     |
| current_value                   | String                                                      | **参数解释：** 条件表达式的当前值。例如：条件表达式为pv \> 0，则current_value取值为：{"pv":100}。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                       |
| old_annotations                 | String                                                      | **参数解释：** 告警规则详情。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                        |
| alarm_action_rule_name          | String                                                      | **参数解释：** 告警行动规则名称。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                      |
| alarm_rule_alias                | String                                                      | **参数解释：** 告警规则别名。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                        |
| alarm_rule_url                  | String                                                      | **参数解释：** 告警规则url。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                       |
| alarm_status                    | String                                                      | **参数解释：** 告警触发状态。 **取值范围：** - 触发(firing)   - 恢复(resolved)           |
| condition_expression            | String                                                      | **参数解释：** 告警触发条件表达式。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                     |
| condition_expression_with_value | String                                                      | **参数解释：** 告警触发表达式带值。例如：条件表达式为pv \> 0，则condition_expression_with_value取值为：100 \> 0。 **取值范围：** 不涉及。                                                                                                                                                                                                                                       |
| notification_frequency          | String                                                      | **参数解释：** 通知频率。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                          |
| record_id                       | String                                                      | **参数解释：** 告警ID。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                          |
| recovery_policy                 | Boolean                                                     | **参数解释：** 是否开启告警恢复开关。 **取值范围：** - true： 开启告警恢复   - false： 关闭告警恢复    |
| results                         | Array of [Results] objects | **参数解释：** 告警通知的详细信息。                                                                                                                                                                                                                                                                                                                                                                                    |
| frequency                       | String                                                      | **参数解释：** 告警统计周期。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                        |
| type                            | String                                                      | **参数解释：** 告警规则类型。 **取值范围：** - sql： sql告警   - keywords： 关键词告警        |
   
 表9Results 
| 参数                    | 参数类型   | 描述                                                                                                                                      |
|:---|:---|:---|
| eps_id                | String | **参数解释：** 企业项目ID。 **取值范围：** 不涉及。        |
| log_group_name        | String | **参数解释：** 日志组名称。 **取值范围：** 不涉及。         |
| log_group_name_alias  | String | **参数解释：** 日志组别名。 **取值范围：** 不涉及。         |
| log_stream_name       | String | **参数解释：** 日志流名称。 **取值范围：** 不涉及。         |
| log_stream_name_alias | String | **参数解释：** 日志流别名。 **取值范围：** 不涉及。         |
| time                  | String | **参数解释：** 告警统计周期，例如：1小时。 **取值范围：** 不涉及。 |
   
 表10Metadata 
| 参数                | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
|:---|:---|:---|
| event_type        | String | **参数解释：** 告警类型。 **取值范围：** alarm                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| event_id          | String | **参数解释：** 告警规则ID。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| event_severity    | String | **参数解释：** 告警级别。 **取值范围：** - Info：提示   - Minor：次要   - Major：重要   - Critical：紧急    |
| event_name        | String | **参数解释：** 告警规则名称。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| resource_type     | String | **参数解释：** 资源类型。 **取值范围：** 日志组/流                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| resource_id       | String | **参数解释：** 日志组/日志流原始名称。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| resource_provider | String | **参数解释：** 告警源。 **取值范围：** LTS                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| lts_alarm_type    | String | **参数解释：** 告警规则类型。 **取值范围：** - sql： sql告警   - keywords：关键词告警   - search_analysis：搜索分析告警                                                                                                             |
| log_group_name    | String | **参数解释：** 日志组原始名称。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| log_stream_name   | String | **参数解释：** 日志流原始名称。 **取值范围：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| event_subtype     | String | **参数解释：** 告警类型。 **取值范围：** - sql： sql告警   - keywords：关键词告警                                                                                                                                                                                                                                                            |
   
 表11PageInfo 
| 参数              | 参数类型   | 描述                                                                                                                                                |
|:---|:---|:---|
| next_marker     | String | **参数解释：** 返回下一页查询地址（为空时，代表后面没有数据）。 **取值范围：** 不涉及。 |
| previous_marker | String | **参数解释：** 返回前一页查询地址。 **取值范围：** 不涉及。               |
| current_count   | String | **参数解释：** 当前页告警总数 **取值范围：** 不涉及。                  |
   
#### 请求示例
查询活动或历史告警列表。
```
/v2/{project_id}/{domain_id}/lts/alarms/sql-alarm/query?type=active_alert
{
  "whether_custom_field" : false,
  "start_time" : 0,
  "end_time" : 0,
  "time_range" : "30",
  "search" : "",
  "alarm_level_ids" : [ "Critical", "Major", "Minor", "Info" ],
  "sort" : {
    "order_by" : [ "starts_at" ],
    "order" : "desc"
  }
}
```
#### 响应示例
**状态码：200**
请求响应成功。
```
{
  "events" : [ {
    "annotations" : {
      "alarm_action_rule_name" : "***",
      "alarm_rule_alias" : "***",
      "alarm_rule_url" : "***",
      "alarm_status" : "触发",
      "condition_expression" : "匹配条数>1",
      "condition_expression_with_value" : "***>1",
      "current_value" : "{\"error\":***}",
      "frequency" : "每10分钟",
      "log_info" : "[{\"eps_id\":\"0\",\"log_group_id\":\"***\",\"log_group_name\":\"***\",\"log_group_name_alias\":\"***\",\"log_stream_id\":\"***\",\"log_stream_name\":\"***\",\"log_stream_name_alias\":\"***\"}]",
      "message" : "{\"alarm_action_rule_name\":\"***\",\"alarm_rule_alias\":\"***\",\"alarm_rule_description\":\"testxunjan\",\"alarm_rule_id\":\"***\",\"alarm_rule_name\":\"***\",\"alarm_send\":false,\"closed_alarm_time\":0,\"condition_expression\":\">\",\"create_time\":0,\"domain_id\":\"***\",\"enterprise_project_id\":\"--\",\"frequency\":{\"day_of_week\":1,\"fixed_rate\":10,\"fixed_rate_unit\":\"minute\",\"hour_of_day\":0,\"type\":\"FIXED_RATE\"},\"is_css_sql\":false,\"ltsAlarmInfos\":[{\"condition\":\">\",\"conditions\":[{\"condition\":\">\",\"number\":1}],\"eps_id\":\"0\",\"is_time_range_relative\":true,\"keywords\":\"***\",\"log_group_id\":\"***\",\"log_group_name\":\"***\",\"log_group_name_alias\":\"***\",\"log_stream_id\":\"***\",\"log_stream_name\":\"***\",\"log_stream_name_alias\":\"***\",\"number\":1,\"search_time_range\":15,\"search_time_range_unit\":\"minute\",\"time_range\":{\"end_time\":***,\"end_time_lt\":false,\"start_time\":***,\"start_time_gt\":true}}],\"notification_frequency\":0,\"projectId\":\"\",\"route_group_rule\":\"\",\"status\":\"RUNNING\",\"tags\":[],\"topics\":[],\"type\":\"keywords\",\"update_time\":0,\"whether_recovery_policy\":false}",
      "notification_frequency" : "0分钟",
      "old_annotations" : "{\"alarm_action_rule_name\":\"***\",\"closed_alarm_time\":0,\"condition_expression\":\">\",\"condition_expressions\":[],\"console_url\":\"***\",\"create_time\":0,\"domain_id\":\"***\",\"frequency\":{\"day_of_week\":1,\"fixed_rate\":10,\"fixed_rate_unit\":\"minute\",\"hour_of_day\":0,\"type\":\"FIXED_RATE\"},\"keywords_alarm_rule_description\":\"testxunjan\",\"keywords_alarm_rule_id\":\"***\",\"keywords_alarm_rule_name\":\"***\",\"keywords_requests\":[{\"condition\":\">\",\"conditions\":[],\"eps_id\":\"0\",\"is_time_range_relative\":true,\"keywords\":\"***\",\"log_group_id\":\"***\",\"log_group_name\":\"***\",\"log_group_name_alias\":\"***\",\"log_group_url\":\"***\",\"log_stream_id\":\"***\",\"log_stream_name\":\"***\",\"log_stream_name_alias\":\"***\",\"log_stream_url\":\"***\",\"number\":1,\"search_time_range\":15,\"search_time_range_unit\":\"minute\",\"time_range\":{\"end_time\":***,\"end_time_lt\":false,\"start_time\":***,\"start_time_gt\":true},\"url\":\"***\"}],\"language\":\"zh-cn\",\"notification_frequency\":0,\"recovery_policy\":3,\"route_group_rule\":\"***\",\"tags\":[],\"trigger_condition_count\":1,\"trigger_condition_frequency\":1,\"update_time\":0,\"whether_recovery_policy\":false}",
      "record_id" : "***",
      "recovery_policy" : false,
      "results" : [ {
        "eps_id" : "0",
        "log_group_name" : "***",
        "log_group_name_alias" : "***",
        "log_stream_name" : "***",
        "log_stream_name_alias" : "***",
        "time" : "15分钟(相对时间)"
      } ],
      "type" : "keywords"
    },
    "arrives_at" : 0,
    "ends_at" : 0,
    "id" : "***",
    "metadata" : {
      "log_group_name" : "***",
      "log_stream_name" : "***",
      "event_type" : "alarm",
      "event_id" : "***",
      "lts_alarm_type" : "keywords",
      "event_severity" : "Minor",
      "resource_type" : "日志组/流",
      "event_name" : "***",
      "resource_id" : "***",
      "event_subtype" : "keywords",
      "resource_provider" : "LTS"
    },
    "starts_at" : 0,
    "timeout" : 1296000000,
    "type" : "keywords"
  } ],
  "page_info" : {
    "current_count" : "2",
    "next_marker" : "",
    "previous_marker" : "0"
  }
}
```
#### SDK代码示例
SDK代码示例如下。
- [Java]
  查询活动或历史告警列表。
  ```
  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.lts.v2.region.LtsRegion;
  import com.huaweicloud.sdk.lts.v2.*;
  import com.huaweicloud.sdk.lts.v2.model.*;
  import java.util.List;
  import java.util.ArrayList;
  public class ListActiveOrHistoryAlarmsSolution {
      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);
          LtsClient client = LtsClient.newBuilder()
                  .withCredential(auth)
                  .withRegion(LtsRegion.valueOf("<YOUR REGION>"))
                  .build();
          ListActiveOrHistoryAlarmsRequest request = new ListActiveOrHistoryAlarmsRequest();
          request.withDomainId("{domain_id}");
          ListActiveOrHistoryAlarmsRequestBody body = new ListActiveOrHistoryAlarmsRequestBody();
          List<String> listSortOrderBy = new ArrayList<>();
          listSortOrderBy.add("starts_at");
          Sort sortbody = new Sort();
          sortbody.withOrderBy(listSortOrderBy)
              .withOrder(Sort.OrderEnum.fromValue("desc"));
          List<String> listbodyAlarmLevelIds = new ArrayList<>();
          listbodyAlarmLevelIds.add("Critical");
          listbodyAlarmLevelIds.add("Major");
          listbodyAlarmLevelIds.add("Minor");
          listbodyAlarmLevelIds.add("Info");
          body.withSort(sortbody);
          body.withAlarmLevelIds(listbodyAlarmLevelIds);
          body.withSearch("");
          body.withTimeRange("30");
          body.withEndTime(0L);
          body.withStartTime(0L);
          body.withWhetherCustomField(false);
          request.withBody(body);
          try {
              ListActiveOrHistoryAlarmsResponse response = client.listActiveOrHistoryAlarms(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());
          }
      }
  }
  ```
- [Python]
  查询活动或历史告警列表。
  ```
  # coding: utf-8
  import os
  from huaweicloudsdkcore.auth.credentials import BasicCredentials
  from huaweicloudsdklts.v2.region.lts_region import LtsRegion
  from huaweicloudsdkcore.exceptions import exceptions
  from huaweicloudsdklts.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 = LtsClient.new_builder() \
          .with_credentials(credentials) \
          .with_region(LtsRegion.value_of("<YOUR REGION>")) \
          .build()
      try:
          request = ListActiveOrHistoryAlarmsRequest()
          request.domain_id = "{domain_id}"
          listOrderBySort = [
              "starts_at"
          ]
          sortbody = Sort(
              order_by=listOrderBySort,
              order="desc"
          )
          listAlarmLevelIdsbody = [
              "Critical",
              "Major",
              "Minor",
              "Info"
          ]
          request.body = ListActiveOrHistoryAlarmsRequestBody(
              sort=sortbody,
              alarm_level_ids=listAlarmLevelIdsbody,
              search="",
              time_range="30",
              end_time=0,
              start_time=0,
              whether_custom_field=False
          )
          response = client.list_active_or_history_alarms(request)
          print(response)
      except exceptions.ClientRequestException as e:
          print(e.status_code)
          print(e.request_id)
          print(e.error_code)
          print(e.error_msg)
  ```
- [Go]
  查询活动或历史告警列表。
  ```
  package main
  import (
  "fmt"
  "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
      lts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2"
  "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/model"
      region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/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, err := basic.NewCredentialsBuilder().
          WithAk(ak).
          WithSk(sk).
          SafeBuild()
      if err != nil {
          fmt.Println(err)
          return
      }
      hcClient, err := lts.LtsClientBuilder().
           WithRegion(region.ValueOf("<YOUR REGION>")).
           WithCredential(auth).
           SafeBuild()
      if err != nil {
          fmt.Println(err)
          return
      }
      client := lts.NewLtsClient(hcClient)
      request := &model.ListActiveOrHistoryAlarmsRequest{}
  request.DomainId = "{domain_id}"
  var listOrderBySort = []string{
          "starts_at",
      }
  sortbody := &model.Sort{
  OrderBy: listOrderBySort,
  Order: model.GetSortOrderEnum().DESC,
  }
  var listAlarmLevelIdsbody = []string{
          "Critical",
      "Major",
      "Minor",
      "Info",
      }
  searchListActiveOrHistoryAlarmsRequestBody:= ""
  timeRangeListActiveOrHistoryAlarmsRequestBody:= "30"
  endTimeListActiveOrHistoryAlarmsRequestBody:= int64(0)
  startTimeListActiveOrHistoryAlarmsRequestBody:= int64(0)
  request.Body = &model.ListActiveOrHistoryAlarmsRequestBody{
  Sort: sortbody,
  AlarmLevelIds: &listAlarmLevelIdsbody,
  Search: &searchListActiveOrHistoryAlarmsRequestBody,
  TimeRange: &timeRangeListActiveOrHistoryAlarmsRequestBody,
  EndTime: &endTimeListActiveOrHistoryAlarmsRequestBody,
  StartTime: &startTimeListActiveOrHistoryAlarmsRequestBody,
  WhetherCustomField: false,
  }
  response, err := client.ListActiveOrHistoryAlarms(request)
  if err == nil {
          fmt.Printf("%+v\n", response)
      } else {
          fmt.Println(err)
      }
  }
  ```
- [更多]
  更多编程语言的SDK代码示例，请参见[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/LTS/sdk?api=ListActiveOrHistoryAlarms)的代码示例页签，可生成自动对应的SDK代码示例。
#### 状态码
| 状态码 | 描述      |
|:---|:---|
| 200 | 请求响应成功。 |
   
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-lts/ErrorCode.html)。
