Manage alert rules

Updated at:

Use the aliyun cms2 CLI to manage alerting rules, templates, and history in Cloud Monitor 2.0. The CLI lets you create, query, modify, and delete rules, enable and disable rules in batches, export rules to a Prometheus YAML file, and generate rules from templates in batches.

Sub-command overview

Sub-command

Description

alert rule create

Creates an alert rule.

alert rule get

Queries a single rule by ID.

alert rule list

Queries rules with filtering. Supports JSON and Prometheus YAML output.

alert rule update

Replaces a rule entirely (PUT semantics).

alert rule patch

Partially updates rule fields (PATCH semantics).

alert rule delete

Deletes one or more rules by ID.

alert rule enable/disable

Batch enables or disables rules.

alert template

Manages alert templates (create, get, list, update, delete, and apply).

alert history list

Queries alert history records.

Prerequisites

  • The CLI tool is installed and configured.

  • A Managed Service for Prometheus instance created with its workspace identifier available.

alert rule create — Create an alert rule

Creates an alert rule. Provide the rule content through the --body parameter as inline JSON or a file reference (@file.json). The CLI automatically sets the action field to CREATE.

# Create from inline JSON (use canonical type values)
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>"]}]}
}'

# Create from a JSON file
aliyun cms2 alert rule create --body @./rule.json

# View the example body and request schema
aliyun cms2 alert rule create --show-example-body
aliyun cms2 alert rule create --show-schema

# Validate locally without creating the rule
aliyun cms2 alert rule create --body @./rule.json --dry-run

Parameter

Type

Description

--body

string

The rule body as inline JSON or @file.json. Required.

--dry-run

bool

Performs local static validation only without calling the server.

--show-example-body

bool

Prints the example request body and exits.

--show-schema

bool

Prints the JSON schema of the request body and exits.

Required body fields

Field

Description

workspace

The workspace identifier.

displayName

The display name of the rule.

queryConfig

The query configuration. For Prometheus rules, set type to PROMETHEUS_SINGLE_QUERY and specify promQl.

conditionConfig

The trigger condition. Includes operator, threshold, and severity.

datasourceConfig

The data source configuration. For Prometheus rules, set type to PROMETHEUS and specify instanceId.

notifyConfig.channels

The notification channels. Required. The server rejects requests with empty channels.

Canonical type values

  • datasourceConfig.type: PROMETHEUS | SLS (Simple Log Service) | 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

Note

The --dry-run option performs local static validation only (JSON structure, enums, and PromQL syntax). It is not equivalent to server-side validation. Legacy type aliases that pass local validation may still be rejected by the server.

alert rule get — Query a rule

Queries a single alert rule by ID.

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

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

Parameter

Type

Description

--alert-rule-id

string

The alert rule ID. Required.

alert rule list — Query a rule list

Queries alert rules with filtering. Supports two mutually exclusive input modes: structured flags or --body with a complete QueryAlertRulesInput.

# Query by workspace
aliyun cms2 alert rule list --workspace default-cms-12345-cn-hangzhou

# Search by name and filter enabled rules only
aliyun cms2 alert rule list --workspace my-ws --display-name "CPU" --enabled

# Filter disabled rules only
aliyun cms2 alert rule list --workspace my-ws --disabled

# Query by rule ID (single or comma-separated)
aliyun cms2 alert rule list --alert-rule-id "ar-1,ar-2,ar-3"

# Filter by labels
aliyun cms2 alert rule list --workspace my-ws \
  --labels '{"allOf":[{"key":"severity","value":"critical"}]}'

# Pagination (page number mode or token mode)
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..."

# Export as Prometheus YAML (--format prometheus always outputs YAML)
aliyun cms2 alert rule list --workspace my-ws --format prometheus > rules.yaml

# Text (CSV) output
aliyun cms2 alert rule list --workspace my-ws -o text

Parameter

Type

Description

--workspace

string

Filters results by workspace.

--display-name

string

Filters results by display name using fuzzy match.

--enabled

bool

Shows only enabled rules.

--disabled

bool

Shows only disabled rules.

--status

string

Filters results by alert status.

--alert-rule-id

string

Filters by rule ID (single or comma-separated).

--datasource-type

string

Filters by data source type (PROMETHEUS, SLS, APM, CLOUD_MONITORING, etc.).

--severity

string

Filters by severity level (comma-separated, such as CRITICAL,ERROR).

--labels

string

Filters by labels as a JSON string (such as '{"allOf":[{"key":"k","value":"v"}]}').

--page-number / --page-size

int32

Page-number pagination. Use for known-size result sets where random page access is needed.

--max-results

int32

Maximum number of entries per page in token-based pagination.

--next-token

string

Pagination token for token-based pagination.

--format

string

The output format: json (default) or prometheus.

--body

string

A complete QueryAlertRulesInput body (mutually exclusive with structured flags).

Note
  • In structured mode, specify at least one of --workspace, a filter flag, or a pagination flag.

  • The --format prometheus option converts only PromQL rules. Non-PromQL rules are skipped and a warning is written to stderr. Group names become datasource-{instanceId} and cannot be reversed.

alert rule update — Replace a rule in its entirety

Replaces a rule in its entirety (PUT semantics).

Warning

The update command performs a full replacement. Fields not included in the body are not preserved. If a required field is missing, the server rejects the request. To update individual fields, use alert rule patch.

The following workflow demonstrates the recommended get → edit → update sequence:

# 1) Retrieve the current rule and strip server-managed read-only fields
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) Edit rule.body.json (for example, change the threshold to 95)
jq '.conditionConfig.threshold = 95' rule.body.json > rule.body.tmp && mv rule.body.tmp rule.body.json

# 3) Submit the full replacement
aliyun cms2 alert rule update --alert-rule-id ar-abc123 --body @./rule.body.json

Parameter

Type

Description

--alert-rule-id

string

The ID of the rule to update. Required.

--body

string

The complete rule body as inline JSON or @file.json. Required.

--dry-run

bool

Performs local static validation only without calling the server.

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

bool

Prints the example body or JSON schema and exits.

alert rule patch — Partially update rules

Partially updates rule fields (PATCH semantics). Fields not specified in the request are preserved on the server. Supports two mutually exclusive input modes: --set key=value (repeatable) or --body (partial JSON). The CLI automatically sets action to PATCH and injects uuid from --alert-rule-id.

# --set mode (simple field edits, supports dot-notation for nested paths)
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 mode (partial JSON, only the fields to change)
aliyun cms2 alert rule patch --alert-rule-id ar-xxx \
  --body '{"displayName":"New Name","enabled":false}'

Parameter

Type

Description

--alert-rule-id

string

The ID of the rule to modify. Required.

--set

stringArray

Sets field=value pairs. Repeatable. Supports dot-notation for nested paths. Mutually exclusive with --body.

--body

string

The partial PATCH body as inline JSON or @file.json. Mutually exclusive with --set.

--reason

string

The reason for this operation (recorded in audit logs).

--dry-run

bool

Validates input locally without executing the operation.

alert rule delete — Delete a rule

Deletes an alert rule. The --alert-rule-id parameter accepts comma-separated values for batch deletion.

Important

Deletion is irreversible.

# Delete a single rule
aliyun cms2 alert rule delete --alert-rule-id ar-abc123

# Delete multiple rules
aliyun cms2 alert rule delete --alert-rule-id "ar-abc123,ar-def456"

Parameter

Type

Description

--alert-rule-id

string

The rule ID to delete. Accepts comma-separated IDs for batch deletion. Required.

alert rule enable/disable — Batch enable/disable rules

Batch enables or disables rules by ID. Changes take effect immediately without interactive confirmation. Run alert rule list to obtain the rule IDs.

# Enable (single or batch)
aliyun cms2 alert rule enable --alert-rule-id ar-abc123
aliyun cms2 alert rule enable --alert-rule-id "ar-abc123,ar-def456"

# Disable
aliyun cms2 alert rule disable --alert-rule-id ar-abc123

Parameter

Type

Description

--alert-rule-id

string

The rule ID to enable or disable. Accepts comma-separated IDs for batch operations. Required.

Alert templates

Alert templates provide reusable rule configurations. Use the apply sub-command to generate alert rules from a template.

# List templates (custom and system)
aliyun cms2 alert template list

# Filter by type and source (is-system: 0=custom, 1=system, -1=both)
aliyun cms2 alert template list --alert-type PROMETHEUS_SINGLE_QUERY --is-system 0

# Query template details (by id or uuid)
aliyun cms2 alert template get --template-id 12345

# Create a custom template
aliyun cms2 alert template create --body @./template.json

# Update or delete (only custom templates can be deleted)
aliyun cms2 alert template update --template-id 12345 --body @./patch.json
aliyun cms2 alert template delete --template-id 12345

# Apply a template to generate rules (Prometheus templates require --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

# Preview the apply result without creating rules
aliyun cms2 alert template apply --template-id 12345 --workspace ws-xxx --dry-run

Sub-command

Parameter

Description

list

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

Filters and pagination options.

get

--template-id (numeric, preferred) / --template-uuid

Specifies the template to retrieve. Provide one of the two identifiers.

create

--body

The template body as a JSON file reference. The ruleConfigs value is a JSON-encoded string. Required. Run aliyun cms2 alert template get on an existing template to see the expected structure.

update

--template-id / --body

Updates a template. The body must contain the id field.

delete

--template-id

Deletes a custom template. Only custom templates (isSystem=0) can be deleted.

apply

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

Generates rules from a template. For Prometheus templates, --prometheus-instance-id is required.

Alert history

Queries alert history records within a workspace.

# List alert history in a workspace
aliyun cms2 alert history list --workspace default-cms-12345-cn-hangzhou

# Filter by severity and time range (timestamps in epoch milliseconds)
aliyun cms2 alert history list --workspace my-ws \
  --severity CRITICAL --from 1700000000000 --to 1700100000000

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

Parameter

Type

Description

--workspace

string

The workspace identifier. Required unless --body is used.

--id

string

Filters by alert rule ID.

--severity

string

Filters by severity level: CRITICAL | WARNING | INFO.

--status

string

Filters by alert status: Ok | Alerting.

--from / --to

int

The time range in epoch milliseconds (from <= to).

--body

string

A complete filter body for advanced queries. Mutually exclusive with convenience flags.

Limits

  • All list commands return a single page per request. Automatic pagination is not supported.

  • The body in create, update, and patch commands is not subject to full server-equivalent validation. The final result is determined by the API response.

  • The update command performs a full replacement. For partial modifications, use patch.

  • The enable, disable, and delete commands operate on --alert-rule-id only. Filtering by name or labels is not supported.

  • The --format prometheus export does not guarantee 100% restoration of original group names and advanced configurations.