本文介绍告警通知模板支持的变量参数和语法。

变量参数

参数 说明
alerts 原始告警事件,类型List<Alert>。
alerts.annotations 原始告警的注释,类型Map<String, String>。
alert.endsAt 原始告警的结束时间。

结束时间=告警发生时间+恢复时长

alert.startsAt 原始告警的发生时间。
alert.fingerprint 原始告警指纹标识,同一组标签的告警其指纹相同。
alert.labels 原始告警的标签。
alert.status 原始告警状态:
  • firing:正触发
  • resolved:已恢复
commonAnnotations 一组原始告警中相同的注释。
commonLabels 一组原始告警中相同的标签。
groupLabels 分组标签,根据通知策略中的分组条件设置的标签进行分组。
status 告警组状态:
  • firing:正触发
  • resolved:已恢复
startTime 告警组创建的时间。
endTime 告警组恢复的时间,最后一个原始告警的恢复时间。
level 告警等级:
  • critical:P1
  • error:P2
  • warn:P3
  • page:P4
dispatchRuleName 告警组匹配的通知策略名称。

返回示例

{
  "alerts": [
    {
      "annotations": {
        "_aliyun_arms_alert_value": "15.521240234375",
        "_aliyun_arms_alert_message": "null\n消息ID: ac10c42a16124966960554200d2450-7996494\n",
        "_aliyun_arms_alert_now_value": "15.521240234375",
        "message": "报警:命名空间: {{$labels.namespace}} / Pod: {{$labels.pod_name}} / 容器: {{$labels.container}} 内存使用率超>过80%, 当前值{{ printf \"%.2f\" $value }}%",
        "value": "15.521240234375",
        "_aliyun_arms_alert_past_value": "15.521240234375"
      },
      "endsAt": "2021-02-22T07:27:15.404000000Z",
      "fingerprint": "bec72890cc2c7b4a027e008df0cd1013",
      "labels": {
        "container": "kube-state-metrics",
        "severity": "warning",
        "_aliyun_arms_alert_level": "ERROR",
        "instance": "10.0.80.186:10255",
        "clustername": "klyz1688-kubernetes-1",
        "_aliyun_arms_alert_type": "101",
        "_aliyun_arms_integration_name": "测试集成-prometheus",
        "alertname": "容器内存使用率大于80%",
        "_aliyun_arms_userid": "1131971649496228",
        "_aliyun_arms_involvedObject_name": "klyz1688-kubernetes-1",
        "pod_name": "kube-state-metrics-ccb59dbff-jljg4",
        "_aliyun_arms_involvedObject_id": "cb36dcafb9b9340498fad2e1f40b9a254",
        "_aliyun_arms_region_id": "cn-hangzhou",
        "_aliyun_arms_involvedObject_kind": "cluster",
        "_aliyun_arms_product_type": "PROMETHEUS",
        "name": "k8s_kube-state-metrics_kube-state-metrics-ccb59dbff-jljg4_arms-prom_359508f3-7e76-4740-b915-41ea48849641_0",
        "namespace": "arms-prom",
        "_aliyun_arms_integration_id": "80",
        "_aliyun_arms_involvedObject_type": "ManagedKubernetes",
        "_aliyun_arms_alert_rule_id": "3927051"
      },
      "startsAt": "2021-02-22T07:18:15.578000000Z",
      "status": "firing"
    }
  ],
  "commonAnnotations": {
    "_aliyun_arms_alert_value": "15.521240234375",
    "_aliyun_arms_alert_message": "null\n消息ID: ac10c42a16124966960554200d2450-7996494\n",
    "_aliyun_arms_alert_now_value": "15.521240234375",
    "message": "报警:命名空间: {{$labels.namespace}} / Pod: {{$labels.pod_name}} / 容器: {{$labels.container}} 内存使用率超过80%, 当前值{{ printf \"%.2f\" $value }}%",
    "value": "15.521240234375",
    "_aliyun_arms_alert_past_value": "15.521240234375"
  },
  "commonLabels": {
    "container": "kube-state-metrics",
    "severity": "warning",
    "_aliyun_arms_alert_level": "ERROR",
    "instance": "10.0.80.186:10255",
    "clustername": "klyz1688-kubernetes-1",
    "_aliyun_arms_alert_type": "101",
    "_aliyun_arms_integration_name": "测试集成-prometheus",
    "alertname": "容器内存使用率大于80%",
    "_aliyun_arms_userid": "1131971649496228",
    "_aliyun_arms_involvedObject_name": "klyz1688-kubernetes-1",
    "pod_name": "kube-state-metrics-ccb59dbff-jljg4",
    "_aliyun_arms_involvedObject_id": "cb36dcafb9b9340498fad2e1f40b9a254",
    "_aliyun_arms_region_id": "cn-hangzhou",
    "_aliyun_arms_involvedObject_kind": "cluster",
    "_aliyun_arms_product_type": "PROMETHEUS",
    "name": "k8s_kube-state-metrics_kube-state-metrics-ccb59dbff-jljg4_arms-prom_359508f3-7e76-4740-b915-41ea48849641_0",
    "namespace": "arms-prom",
    "_aliyun_arms_integration_id": "80",
    "_aliyun_arms_involvedObject_type": "ManagedKubernetes",
    "_aliyun_arms_alert_rule_id": "3927051"
  },
  "groupLabels": {
    "alertname": "容器内存使用率大于80%"
  },
  "status": "firing",
  "startTime":"告警发生时间",
  "endTime":"告警恢复时间",
  "level":"error",
  "dispatchRuleName":"通知策略名称"
}

语法示例

获取字段内容。

示例:获取告警等级。

告警等级:{{ .level }}

For循环。

示例:获取原始告警事件列表的message注释。

{{ for .alerts }} {{ .annotations.message }} \n {{ end }}

数组Index。

示例:获取第一个告警事件的message注释。

{{$alert := index .alerts 0 }} {{ $alert.annotations.message }}