Manage alert rules
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 |
| Creates an alert rule. |
| Queries a single rule by ID. |
| Queries rules with filtering. Supports JSON and Prometheus YAML output. |
| Replaces a rule entirely (PUT semantics). |
| Partially updates rule fields (PATCH semantics). |
| Deletes one or more rules by ID. |
| Batch enables or disables rules. |
| Manages alert templates (create, get, list, update, delete, and apply). |
| 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-runParameter | Type | Description |
| string | The rule body as inline JSON or |
| bool | Performs local static validation only without calling the server. |
| bool | Prints the example request body and exits. |
| bool | Prints the JSON schema of the request body and exits. |
Required body fields
Field | Description |
| The workspace identifier. |
| The display name of the rule. |
| The query configuration. For Prometheus rules, set |
| The trigger condition. Includes |
| The data source configuration. For Prometheus rules, set |
| The notification channels. Required. The server rejects requests with empty channels. |
Canonical type values
datasourceConfig.type:PROMETHEUS|SLS(Simple Log Service) |APM|UMODELqueryConfig.type:PROMETHEUS_SINGLE_QUERY|PROMETHEUS_MULTI_QUERY|SLS_MULTI_QUERY|APM_MULTI_QUERY|UMODEL_METRICSET_QUERYchannel.type:GROUP|CONTACT|DINGTALK|FEISHU|WEIXIN|SLACK|WEBHOOK
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 jsonParameter | Type | Description |
| 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 textParameter | Type | Description |
| string | Filters results by workspace. |
| string | Filters results by display name using fuzzy match. |
| bool | Shows only enabled rules. |
| bool | Shows only disabled rules. |
| string | Filters results by alert status. |
| string | Filters by rule ID (single or comma-separated). |
| string | Filters by data source type ( |
| string | Filters by severity level (comma-separated, such as |
| string | Filters by labels as a JSON string (such as |
| int32 | Page-number pagination. Use for known-size result sets where random page access is needed. |
| int32 | Maximum number of entries per page in token-based pagination. |
| string | Pagination token for token-based pagination. |
| string | The output format: |
| string | A complete |
In structured mode, specify at least one of
--workspace, a filter flag, or a pagination flag.The
--format prometheusoption converts only PromQL rules. Non-PromQL rules are skipped and a warning is written to stderr. Group names becomedatasource-{instanceId}and cannot be reversed.
alert rule update — Replace a rule in its entirety
Replaces a rule in its entirety (PUT semantics).
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.jsonParameter | Type | Description |
| string | The ID of the rule to update. Required. |
| string | The complete rule body as inline JSON or |
| bool | Performs local static validation only without calling the server. |
| 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 |
| string | The ID of the rule to modify. Required. |
| stringArray | Sets |
| string | The partial PATCH body as inline JSON or |
| string | The reason for this operation (recorded in audit logs). |
| 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.
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 |
| 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-abc123Parameter | Type | Description |
| 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-runSub-command | Parameter | Description |
|
| Filters and pagination options. |
|
| Specifies the template to retrieve. Provide one of the two identifiers. |
|
| The template body as a JSON file reference. The |
|
| Updates a template. The body must contain the |
|
| Deletes a custom template. Only custom templates ( |
|
| Generates rules from a template. For Prometheus templates, |
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 xxxParameter | Type | Description |
| string | The workspace identifier. Required unless |
| string | Filters by alert rule ID. |
| string | Filters by severity level: |
| string | Filters by alert status: |
| int | The time range in epoch milliseconds ( |
| string | A complete filter body for advanced queries. Mutually exclusive with convenience flags. |
Limits
All
listcommands return a single page per request. Automatic pagination is not supported.The body in
create,update, andpatchcommands is not subject to full server-equivalent validation. The final result is determined by the API response.The
updatecommand performs a full replacement. For partial modifications, usepatch.The
enable,disable, anddeletecommands operate on--alert-rule-idonly. Filtering by name or labels is not supported.The
--format prometheusexport does not guarantee 100% restoration of original group names and advanced configurations.