Alert rule expressions allow you to configure complex alert conditions when you create an alert rule. An expression consists of basic elements such as metric names, operators, and functions, and supports historical data comparison.
Elements
Cloud Monitor evaluates alert rule expressions against the aggregate data of metrics or individual instances. If the monitoring data meets the expression, an alert is triggered. An expression consists of metric names, metric members, operators, functions, and monitoring data. Example: @cpu_total[60].$instanceId != 'i-2ze3jvsy7620giru****' && @cpu_total[60].$Average > 60.
-
Metric names
For more information about the metrics of different cloud services, see Appendix 1: Metrics for Alibaba Cloud products.
A metric expression uses the
@Metric name[Aggregation period]format. For example, to monitor an Elastic Compute Service (ECS) instance based on the CPUUtilization metric, use@CPUUtilization[60].Note-
The aggregation period of metrics for ECS is displayed in the Min Periods column of the table on the Metric List page for ECS. To view the aggregation period of metrics for another cloud service, select the cloud service from the drop-down list.
-
A metric name must conform to the regular expression
[a-zA-Z][_a-zA-Z0-9]*. If a metric name does not conform to the regular expression, use the@(Metric name)[Aggregation period]format, for example,@('vm.DiskIORead')[60].
Metric names must be combined with metric members to specify the scope of monitoring data.
-
-
Metric members
Metric members specify the scope of monitoring data for a metric. A metric member must start with a dollar sign ($). Use the
@Metric name[Aggregation period].$Metric memberformat. For example, the statistical methods for the CPUUtilization metric of ECS are Average, Minimum, and Maximum. The expression for the average value is@CPUUtilization[60].$Average. The dimensions for the CPUUtilization metric of ECS areuserIdandinstanceId. The expression for a specific instance is@CPUUtilization[60].$instanceId.NoteThe members of metrics for ECS are displayed in the Dimensions and Statistics columns of the table on the Metric List page for ECS. To view the members of metrics for another cloud service, select the cloud service from the drop-down list.
-
Operators
Operators evaluate whether monitoring data meets the alert condition.
-
Basic operators
Category
Operator and description
Mathematical operators
-
+: addition -
-: subtraction -
*: multiplication -
/: division -
%: modulo -
**: exponentiation
Comparison operators
-
==: equal to -
! =: not equal to -
>: greater than -
>=: greater than or equal to -
<: less than -
<=: less than or equal to
Logical operators
-
&&orand: AND -
||oror: OR -
!ornot: NOT
Example:
$Average > 50 && $instanceId != 'i-not-exist'.NoteIf the values on the two sides of a mathematical operator or a comparison operator are not of the same type, a string is converted to a number, for example,
'123' + 321 == 123 + '321' == 444. -
-
String operators
String operator
Description
Example
matches
Matches a regular expression.
Use the logical operator NOT with
matchesto negate a regular expression match.!("hello" matches "^fo.+")NoteThe
!operator has higher priority than the matches operator. Therefore, you must enclose the regular expression in parentheses ().contains
Contains a string.
'abcdef' contains 'cde'startsWith
Checks whether a string starts with a specified prefix.
'abcdef' startsWith 'abc'endsWith
Checks whether a string ends with a specified suffix.
'abcdef' endsWith 'def' -
Membership operators
Membership operator
Description
Example
in
Contains
-
userld in [120886317861****, 425876]The value of
userldis120886317861****or20654616023382****. -
"foo" in {'foo':1, 'bar':2}The value of foo is
fooorbar.
not in
Does not contain
-
userld not in [120886317861****, 425876]The value of
userldis not120886317861****or20654616023382****. -
"foo" not in {'foo1':1, 'bar':2}The value of foo is not
foo1orbar.
-
-
Ternary operators
?:: Works the same as the ternary operator in C and Java, for example, @CPUUtilization[60].$Average > 30? "ok": "lower".
-
-
Data operations
-
Data types
Data type
Description
Example
string
A string enclosed in single quotation marks or double quotation marks.
"hello", 'hello'number
An integer or floating-point number.
NoteFor integers, you can use underscores (_) as separators to improve readability.
-
103, 2.5, .5, 2e+6 -
1_000_000
array
An array.
[1, 2, 3]map or dict
A dictionary.
{"foo":"bar"}bool
A Boolean value.
trueorfalsenil
An empty value.
nil -
-
Built-in functions
Built-in function
Description
len(array|map|string)Returns the length of an array, map, or string.
now()Returns a UTC timestamp in milliseconds.
abs(number)Returns the absolute value (a floating-point number).
rand()Returns a floating-point number between
[0, 1).rand(N)Returns a floating-point number between
[0, N).toLower(string)Converts a string to lowercase.
toUpper(string)Converts a string to uppercase.
-
Comparison
Note-
Example of a metric:
@cpu_total[60]. -
An aggregation method is a string. Example:
'$Average'. -
The comparison result is a Boolean value. If the metric value of the current period is greater than the metric value of the previous period, the comparison result is
true. If the metric value of the current period is less than the metric value of the previous period, the comparison result isfalse.
Function
Description
CompareLastPeriod(metric, aggregation method, comparison result)Returns the percentage change compared with the previous period. Example:
CompareLastPeriod(@cpu_total[60], '$Average', true).For example, if the average value of cpu_total in the current period is 20% and in the previous period is 15%, the return value depends on the comparison result.
-
If the comparison result is
true, this function returns (20 - 15) × 100/20. -
If the comparison result is
false, this function returns (15 - 20) × 100/20.
CompareLastHour(metric, aggregation method, comparison result)Returns the percentage change compared with the previous hour.
CompareLastHour(@cpu_total[60], '$Average', true) > 10: If the average value of cpu_total increases by 10% compared with yesterday, an alert is triggered.CompareLastWeek(metric, aggregation method, comparison result)Obtains the data compared with yesterday.
CompareYesterday(@cpu_total[60], '$Average', true) > 10: If the average value of cpu_total increases by 10% compared with the previous week, an alert is triggered.CompareLastWeek(metric, aggregation method, comparison result)Obtains the data compared with the previous week.
CompareLastWeek(@cpu_total[60], '$Average', true) > 10: If the average value of cpu_total increases by 10% compared with the previous week, an alert is triggered.ComparePast(metric, aggregation method, comparison result, seconds)Returns the percentage change compared with the specified number of
secondsprior to the current time.The preceding four functions are simplified forms of this function.
-
CompareLastHour(@cpu_total[60], '$Average', true)is equivalent toComparePast(@cpu_total[60], '$Average', true, 3600). -
CompareYesterday(@cpu_total[60], '$Average', true)is equivalent toComparePast(@cpu_total[60], '$Average', true, 24*60*60). -
CompareLastWeek(@cpu_total[60], '$Average', true)is equivalent toComparePast(@cpu_total[60], '$Average', true, 7*24*60*60).
-
-
Historical functions
Function
Description
LastPeriod(metric)Returns the data of the previous period. Example:
LastPeriod(@cpu_total[60]).$Average > 75. If the average value ofcpu_totalin the previous period exceeds 75%, an alert is triggered.LastHour(metric)Returns the data of the previous hour. Example:
LastHour(@cpu_total[60]).$Average > 75. If the average value ofcpu_totalin the previous hour exceeds 75%, an alert is triggered.Yesterday(metric)Returns the data of the previous day. Example:
Yesterday(@cpu_total[60]).$Average > 75. If the average value ofcpu_totalon the previous day exceeds 75%, an alert is triggered.LastWeek(metric)Returns the data of the previous week. Example:
LastWeek(@cpu_total[60]).$Average > 75. If the average value ofcpu_totalin the previous week exceeds 75%, an alert is triggered.Past(metric, seconds)Returns the data from the specified number of seconds prior to the current time. The number must be an integral multiple of the metric aggregation period. The preceding functions are shorthand forms of this function. For example,
Past(@cpu_total[60], 3600) > 75is equivalent toLastHour(@cpu_total[60]).$Average > 75.
-
Scenarios
The following table lists common scenarios for alert rule expressions.
|
Scenario |
Alert rule expression |
Description |
|
Set a dedicated threshold for a specific instance and a different threshold for other instances |
|
For the instance |
|
Exclude a specific instance from alerting |
|
For the instance |
|
Set an alert condition based on multiple metrics |
|
If the average value of cpu_total for an instance exceeds 50% and the average value of memory_usage for the instance exceeds 80%, an alert is triggered. |
|
Monitor the metric reporting latency |
|
If the heartbeat timeout exceeds one minute, an alert is triggered. Note
The reportTime parameter indicates when a metric was reported. It is a built-in parameter of each metric. The |