本文介绍旧版内容模板所支持的变量以及引用方式。

引用方式

重要 引用变量时,变量名称必须完全匹配。对于不存在的变量或者不合法的引用默认替换为空字符串。如果引用的值为对象类型,则会转换为JSON字符串。

配置行动策略时必须选择内容模板,内容模板定义具体的发送内容和主题。您可以在发送内容主题中,通过${fieldName}方式引用模板变量。日志服务发送告警通知时,会将发送内容主题中的模板变量替换为真实值,例如${project}替换为告警规则所属的Project名称。

可用变量及其引用

目前支持的所有可用变量及引用方式如下表所示。
变量 说明 数据类型 取值示例 引用示例
aliuid Project所属的阿里云账号ID。 string 117918664953**** ${aliuid}用户的告警规则已触发。
alert_instance_id 告警触发的实例的ID。 string ee16a8f435485f3f-5be6b81edc520-3d6**** 实例ID为${alert_instance_id}
project 告警规则所属Project。 string my-project ${project}项目中的告警规则已触发。
alert_id 告警规则ID,Project内唯一。 string 0fdd88063a611aa114938f9371daeeb6-1671a52**** 告警规则ID是${alert_id}
alert_type 告警类型。
  • sls_alert:由告警监控规则触发的告警。
  • sls_pub:来自于开放告警中的告警。
string sls_alert 告警类型是${alert_type}
alert_name 告警规则名称。 string 告警规则new2 告警规则${alert_name}已经触发。
next_eval_interval 下一次评估间隔。 int 900 下一次评估时间为${next_eval_interval}秒后。
alert_time 本次评估时间。 int 1616744734 本次评估告警的时间为${alert_time}
fire_time 首次触发时间。 int 1616059834 告警首次触发时间为${fire_time}
status 告警状态。
  • firing:触发告警。
  • resolved:恢复通知。
string firing 告警状态为${status}
resolve_time 告警恢复时间。
  • 如果告警状态是firing,取值为0。
  • 如果告警状态是resolved,取值为具体恢复时间。
int 0 告警恢复的时间为${resolve_time}
results 查询参数和中间结果,数组类型。变量取值说明,请参见QueryData结构 array
[
{
"store_type": "log",
"region": "cn-hangzhou",
"project": "sls-alert-test",
"store": "test",
"query": "* | select count(1) as cnt",
"start_time": 1616741485,
"end_time": 1616745085,
"dashboard_id": "mydashboard",
"raw_results": [{"cnt": "4"}],
"raw_result_count": 1,
"truncated": false,
"role_arn": ""
}
]
第一个查询的开始时间为${results[0].start_time};结束时间为${results[0].end_time}
说明 其中0为图表编号。更多信息,请参见如何查看图表编号
labels 标签列表。 map {"env":"test"} 告警标签为${labels}
annotations 标注列表。 map { "title": "告警标题","desc": "告警描述" } 告警标注为${annotations}
severity 告警严重度。
  • 10:严重
  • 8:高
  • 6:中
  • 4:低
  • 2:仅报告
int 10 告警严重度为${severity}
policy 告警策略或者行动策略。变量取值说明,请参见Policy结构 map
{
    "alert_policy_id": "sls.test-alert",
    "action_policy_id": "sls.test-action",
    "use_default": false,
    "repeat_interval": "6m0s"
}
告警策略ID为${policy.alert_policy_id}
region 地域。 string cn-hangzhou 告警触发的地域为${region}
drill_down_query 用于下钻分析的查询语句。在自定义告警中值为空,目前适用于日志审计服务、成本管家和SLB日志中心的告警内容模板。 string * | select count(1) as cnt
alert_url 告警的详细URL地址。 string https://sls.console.aliyun.com/lognext/project/test-xxxx/alert/alert-1617164106-940166 告警URL为${alert_url}
query_url 查询统计中第一个查询页面的URL地址。 string https://sls-stg.console.aliyun.com/lognext/project/test-xxx/logsearch/test-alert-access?encode=base64&endTime=1617175989&queryString=KiB8IHNlbGVjdCBjb3VudCgxKSBhcyBjbnQ%3D&queryTimeType=99&startTime=1617175089 查询统计中第一个查询页面的URL地址为${query_url}
alert_history_dashboard_url 告警历史统计报表的URL地址。 string https://sls.console.aliyun.com/lognext/project/test-xx/dashboard/internal-alert-analysis 告警历史统计报表的URL地址为${alert_history_dashboard_url}
condition 触发告警的评估表达式。其中,以触发告警的值替换您所配置的变量,并使用中括号([ ])包裹。格式为Count:数量表达式; Condition:匹配表达式 string Count:[5] > 3;Condition:[example.com]=='example.com' 告警评估表达式为${condition}
raw_condition 原始的评估表达式,即变量未被替换为真实值的原始表达式。格式为Count:数量表达式; Condition:匹配表达式 string Count:__count__ > 3;Condition:host=='example.com' 原始评估表达式为${raw_condition}
dashboard 告警关联的仪表盘名称。 string mydashboard 告警关联的仪表盘名称为${dashboard}
dashboard_url 告警关联的仪表盘地址。 string https://sls.console.aliyun.com/next/project/myproject/dashboard/mydashboard 告警关联的仪表盘地址为${dashboard_url}
fire_results 触发告警的数据,即集合操作后的数据,最多100条。 array
[{
    "host":example.com",
    "host__1":"example.com",
    "pv":"836",
    "slbid":"slb-02",
    "status":"200"},{
    "host":"example.com",
    "host__1":"example.org",
    "pv":"836",
    "slbid":"slb-02",
    "status":"200"
},{
    "host":"example.com",
    "host__1":"example.com",
    "pv":"836",
    "slbid":"slb-02",
    "status":"200"
},{
    "host":"example.com",
    "host__1":"example.com",
    "pv":"836",
    "slbid":"slb-02",
    "status":"200"
},{
    "host":"example.com",
    "host__1":"example.com",
    "pv":"780",
    "slbid":"slb-01",
    "status":"200"
}]
告警触发时产生的数据为${fire_results}
fire_results_count 触发告警的数据的总条数,可能多于100,比如笛卡尔积操作后的总条数。 int 3 告警触发时产生的总数据条数为${fire_results_count}
fire_results_as_kv 触发告警的数据,即集合操作后的数据,最多100条。以[key1:value1,key2:value2]形式展示。 array [host:example.com,pv:836,status:200][host:example.com,pv:780,status:200] 告警触发时产生的数据详情为${fire_results_as_kv}

Policy结构

policy变量中可引用的变量说明如下表所示。

字段 说明 数据类型 举例
alert_policy_id 告警策略ID。 string sls.test-alert
action_policy_id 告警监控规则指定的行动策略ID,仅在告警策略使用动态行动策略时有用。 string sls.test-action
repeat_interval 重复等待时间,仅在告警策略使用行动策略时有用。 string 4h

QueryData结构

results变量中可引用的变量说明如下表所示。

变量 说明 数据类型 举例
role_arn 使用服务角色。 string acs:ram::117918664953****:role/aliyunslsalertmonitorrole
store_type 存储类型。
  • log:日志。
  • metric:时序数据。
  • meta:资源数据。
string log
region 查询统计目标库所在地域。

存储类型为资源数据时,该变量值为空。

string cn-hangzhou
project 查询统计目标库所在Project。

存储类型为资源数据时,该变量值为空。

string sls-test-alert
store 查询统计目标库名称。 string test-logstore
query 查询语句。 string error | select count(1) as cnt
start_time 查询开始时间。

存储类型为资源数据时,该变量值为空。

int 2006-01-02 15:04:05
start_time_ts 查询开始时间,Unix格式。

存储类型为资源数据时,该变量值为空。

int 1616741485
end_time 查询结束时间。

存储类型为资源数据时,该变量值为空。

int 2006-01-02 15:04:05
end_time_ts 查询结束时间,Unix格式。

存储类型为资源数据时,该变量值为空。

int 1616745085
dashboard_id 查询时关联的仪表盘ID。 string mydashboard
raw_results 实际查询内容,数组格式,最多100行。 array
[{
    "host":"example.com",
    "slbid":"slb-02",
    "status":"200"
},{
    "host":"example.com",
    "slbid":"slb-01",
    "status":"200"
},{
    "host":"example.com",
    "slbid":"slb-02",
    "status":"306"
},{
    "host":"example.com",
    "slbid":"slb-02",
    "status":"200"
},{
    "host":"example.com",
    "slbid":"slb-01",
    "status":"200"
},{
    "host":"example.com",
    "slbid":"slb-02",
    "status":"200"
}]