告警规则管理

更新时间:
复制 MD 格式

通过 CLI 工具 aliyun cms2管理云监控 2.0 告警规则(alert rule)、告警模板(alert template)和告警历史(alert history)。支持规则的增删改查、批量启停、导出为 Prometheus YAML,以及基于模板批量生成规则。

子命令概览

子命令

说明

alert rule create

创建告警规则

alert rule get

按 ID 查询单条规则

alert rule list

查询规则列表(支持 JSON / Prometheus YAML 输出)

alert rule update

全量替换规则(PUT 语义)

alert rule patch

增量更新规则字段(PATCH 语义)

alert rule delete

按 ID 删除规则

alert rule enable/disable

批量启用 / 禁用规则

alert template

管理告警模板(create/get/list/update/delete/apply)

alert history list

查询告警历史记录

前提条件

告警规则(alert rule)

alert rule create — 创建规则

通过 --body(内联 JSON 或 @file.json)提供规则内容,action 字段由 CLI 自动置为 CREATE。

# 从内联 JSON 创建(使用服务端 canonical 类型值)
aliyun cms2 alert rule create --body '{
  "workspace": "default-cms-12345-cn-hangzhou",
  "displayName": "ecs-cpu-high",
  "enabled": true,
  "queryConfig": {"type": "PROMETHEUS_SINGLE_QUERY", "promQl": "avg by (instanceId) (cpu_total)"},
  "conditionConfig": {"type": "PROMETHEUS_SIMPLE_CONDITION", "operator": "GT", "threshold": 90, "severity": "WARN"},
  "datasourceConfig": {"type": "PROMETHEUS", "instanceId": "prom-xxx"},
  "notifyConfig": {"type": "DIRECT_NOTIFY", "channels": [{"type": "GROUP", "identifiers": ["<contact-group-id>"]}]}
}'

# 从 JSON 文件创建
aliyun cms2 alert rule create --body @./rule.json

# 查看示例 body 与请求 schema
aliyun cms2 alert rule create --show-example-body
aliyun cms2 alert rule create --show-schema

# 本地校验而不实际创建
aliyun cms2 alert rule create --body @./rule.json --dry-run

参数

类型

说明

--body

string

规则 body,内联 JSON 或 @file.json(必填)

--dry-run

bool

仅做本地静态校验,不调用服务端

--show-example-body

bool

打印示例请求 body 后退出

--show-schema

bool

打印请求 body 的 JSON schema 后退出

Body 必需字段

字段

说明

workspace

工作空间标识

displayName

规则显示名

queryConfig

查询配置(Prometheus 规则用 type=PROMETHEUS_SINGLE_QUERY + promQl)

conditionConfig

触发条件(operator / threshold / severity 等)

datasourceConfig

数据源(Prometheus 规则用 type=PROMETHEUS + instanceId)

notifyConfig.channels

通知渠道,实际必填;空 channels 会被后端拒绝

canonical 类型值

  • datasourceConfig.type:PROMETHEUS | SLS | APM | UMODEL

  • queryConfig.type:PROMETHEUS_SINGLE_QUERY | PROMETHEUS_MULTI_QUERY | SLS_MULTI_QUERY | APM_MULTI_QUERY | UMODEL_METRICSET_QUERY

  • channel.type:GROUP | CONTACT | DINGTALK | FEISHU | WEIXIN | SLACK | WEBHOOK

说明

--dry-run 仅做本地静态校验(JSON 结构、枚举、PromQL 语法),不等同于服务端校验;本地放行的旧式类型别名仍可能被后端拒绝。

alert rule get — 查询单条规则

aliyun cms2 alert rule get --alert-rule-id ar-abc123

# JSON 输出
aliyun cms2 alert rule get --alert-rule-id ar-abc123 -o json

参数

类型

说明

--alert-rule-id

string

告警规则 ID

alert rule list — 查询规则列表

支持两种输入模式(互斥):结构化 flag,或 --body 传完整 QueryAlertRulesInput。

# 按 workspace 查询
aliyun cms2 alert rule list --workspace default-cms-12345-cn-hangzhou

# 按名称搜索 + 仅启用规则
aliyun cms2 alert rule list --workspace my-ws --display-name "CPU" --enabled

# 仅禁用规则
aliyun cms2 alert rule list --workspace my-ws --disabled

# 按规则 ID 查询(单个或逗号分隔多个)
aliyun cms2 alert rule list --alert-rule-id "ar-1,ar-2,ar-3"

# 按标签过滤
aliyun cms2 alert rule list --workspace my-ws \
  --labels '{"allOf":[{"key":"severity","value":"critical"}]}'

# 分页(页码模式 或 token 模式)
aliyun cms2 alert rule list --workspace my-ws --page-number 2 --page-size 20
aliyun cms2 alert rule list --workspace my-ws --max-results 20 --next-token "eyJ..."

# 导出为 Prometheus YAML(--format prometheus 始终输出 YAML)
aliyun cms2 alert rule list --workspace my-ws --format prometheus > rules.yaml

# 文本(CSV)输出
aliyun cms2 alert rule list --workspace my-ws -o text

参数

类型

说明

--workspace

string

按工作空间过滤

--display-name

string

按显示名模糊匹配

--enabled

bool

仅显示启用的规则(开关)

--disabled

bool

仅显示禁用的规则(开关)

--status

string

按告警状态过滤

--alert-rule-id

string

按规则 ID 过滤(单个或逗号分隔多个)

--datasource-type

string

按数据源类型过滤(PROMETHEUS、SLS、APM、CLOUD_MONITORING 等)

--severity

string

按严重级别过滤(逗号分隔,如 CRITICAL,ERROR)

--labels

string

标签过滤(JSON,如 '{"allOf":[{"key":"k","value":"v"}]}')

--page-number / --page-size

int32

页码分页模式

--max-results

int32

token 分页模式,每页最大条数

--next-token

string

token 分页模式,翻页令牌

--format

string

输出格式:json(默认)或 prometheus

--body

string

直接传 QueryAlertRulesInput(与结构化 flag 互斥)

说明:

  • 结构化模式至少提供 --workspace、过滤条件或分页 flag 之一。

  • --format prometheus 仅转换 PromQL 规则,非 PromQL 规则跳过并在 stderr 输出 warning;组名会变为 datasource-{instanceId},不可逆还原。

alert rule update — 全量替换规则

警告

update 为全量替换(PUT 语义),未包含的字段不会保留,缺失必需字段会被后端拒绝。若只想改个别字段,请用 alert rule patch。

推荐工作流(get → 剥离只读字段 → 编辑 → update):

# 1) 拉取当前规则并剥离服务端只读字段
aliyun cms2 alert rule get --alert-rule-id ar-abc123 -o json \
| jq '.data | del(.uuid, .createdAt, .updatedAt, .status, .gmtCreate, .gmtModified)' \
  > rule.body.json

# 2) 编辑 rule.body.json(如把阈值改为 95)
jq '.conditionConfig.threshold = 95' rule.body.json > rule.body.tmp && mv rule.body.tmp rule.body.json

# 3) 提交全量替换
aliyun cms2 alert rule update --alert-rule-id ar-abc123 --body @./rule.body.json

参数

类型

说明

--alert-rule-id

string

要更新的规则 ID

--body

string

完整规则 body,内联 JSON 或 @file.json

--dry-run

bool

仅做本地静态校验

--show-example-body / --show-schema

bool

打印示例 body / schema 后退出

alert rule patch — 增量更新规则

patch 为真正的部分更新(PATCH 语义),未指定字段在服务端保留。两种输入模式互斥:--set key=value(可重复)或 --body(部分 JSON)。action 自动置为 PATCH,uuid 从 --alert-rule-id 注入。

# --set 模式(简单字段编辑,支持点号嵌套路径)
aliyun cms2 alert rule patch --alert-rule-id ar-xxx --set displayName="New Name"
aliyun cms2 alert rule patch --alert-rule-id ar-xxx --set conditionConfig.threshold=95

# --body 模式(部分 JSON,仅含要改的字段)
aliyun cms2 alert rule patch --alert-rule-id ar-xxx \
  --body '{"displayName":"New Name","enabled":false}'

参数

类型

说明

--alert-rule-id

string

要修改的规则 ID

--set

stringArray

字段=值(可重复,点号表示嵌套;与 --body 互斥)

--body

string

部分 PATCH body,内联 JSON 或 @file.json(与 --set 互斥)

--reason

string

本次操作原因(记入审计)

--dry-run

bool

校验输入而不执行

alert rule delete — 删除规则

重要

删除不可逆。--alert-rule-id 支持逗号分隔批量删除。

# 删除单条
aliyun cms2 alert rule delete --alert-rule-id ar-abc123

# 批量删除
aliyun cms2 alert rule delete --alert-rule-id "ar-abc123,ar-def456"

参数

类型

说明

--alert-rule-id

string

规则 ID(逗号分隔可批量删除)

alert rule enable/disable — 批量启停

通过规则 ID 批量启用或禁用,立即生效,无交互确认。可先用 alert rule list 获取 ID。

# 启用(单个或批量)
aliyun cms2 alert rule enable --alert-rule-id ar-abc123
aliyun cms2 alert rule enable --alert-rule-id "ar-abc123,ar-def456"

# 禁用
aliyun cms2 alert rule disable --alert-rule-id ar-abc123

参数

类型

说明

--alert-rule-id

string

规则 ID(逗号分隔可批量操作)

告警模板(alert template)

告警模板可复用规则配置,并通过 apply 批量生成具体告警规则。

# 列出模板(自定义 + 系统)
aliyun cms2 alert template list

# 按类型和来源过滤(is-system: 0=自定义 1=系统 -1=两者)
aliyun cms2 alert template list --alert-type PROMETHEUS_SINGLE_QUERY --is-system 0

# 查询模板详情(按 id 或 uuid)
aliyun cms2 alert template get --template-id 12345

# 创建自定义模板
aliyun cms2 alert template create --body @./template.json

# 更新 / 删除(仅自定义模板可删)
aliyun cms2 alert template update --template-id 12345 --body @./patch.json
aliyun cms2 alert template delete --template-id 12345

# 应用模板批量生成规则(Prometheus 模板需 --prometheus-instance-id)
aliyun cms2 alert template apply --template-id 12345 --workspace ws-xxx \
  --var namespace=prod --prometheus-instance-id prom-xxx \
  --contact-group-id cg-1

# 预览应用结果而不实际创建
aliyun cms2 alert template apply --template-id 12345 --workspace ws-xxx --dry-run

常用参数:

命令

参数

说明

list

--alert-type / --sub-type / --is-system / --template-name / --page-number / --page-size

过滤与分页

get

--template-id(数字,优先)/ --template-uuid

二选一

create

--body

必填;ruleConfigs 为 JSON 编码的字符串

update

--template-id / --body

body 内需含 id

delete

--template-id

仅自定义模板(isSystem=0)可删

apply

--template-id / --template-uuid、--workspace、--var、--prometheus-instance-id、--contact-group-id、--dry-run

由模板生成规则

告警历史(alert history)

# 列出工作空间内的告警历史
aliyun cms2 alert history list --workspace default-cms-12345-cn-hangzhou

# 按级别和时间范围过滤(时间为 epoch 毫秒)
aliyun cms2 alert history list --workspace my-ws \
  --severity CRITICAL --from 1700000000000 --to 1700100000000

# 分页
aliyun cms2 alert history list --workspace my-ws --max-results 50 --next-token xxx

参数

类型

说明

--workspace

string

工作空间标识(除非用 --body,否则必填)

--id

string

按告警规则 ID 过滤

--severity

string

级别:CRITICAL | WARNING | INFO

--status

string

状态:Ok | Alerting

--from / --to

int

时间范围(epoch 毫秒,from <= to)

--body

string

高级过滤,传完整 body(与便捷 flag 互斥)

使用限制

  • list 均为单页返回,不自动翻页。

  • create/update/patch 的 body 不做完整服务端等价校验,最终以 API 返回为准。

  • update 为全量替换,增量修改请用 patch。

  • enable/disable/delete 通过 --alert-rule-id 操作,不支持按名称、标签批量筛选。

  • --format prometheus 导出的 YAML 不保证 100% 还原原始分组名与高级配置。