すべてのプロダクト
Search
ドキュメントセンター

Application Real-Time Monitoring Service:通知テンプレートと Webhook テンプレートの設定

最終更新日:Sep 05, 2026

通知ポリシー内のアラートルールがトリガーされると、ARMS は DingTalk、Lark、WeCom、SMS、電話、E メール、Webhook などのチャネルを通じてアラート通知を送信します。ARMS は、各通知方法に対して事前設定済みの通知テンプレートを提供します。これらのデフォルトテンプレートが要件を満たさない場合は、カスタマイズできます。このトピックでは、カスタマイズに用いる Go テンプレート構文、事前設定済みのテンプレート、および利用可能なテンプレート変数について説明します。

Go テンプレート構文

ARMS のアラート通知テンプレートでは、Go テンプレート 構文を使い、提供されたコンテキストのデータでテンプレートに値を設定します。

説明

通知テンプレートのアラートパラメーターは、複数のラベルで構成されます。ラベルを使用すると、特定のアラートパラメーターを通知に含めることができます。たとえば、この例の alertname ラベルはアラート名を示します。

このトピックでは、次のサンプルコードを使用して、一般的な Go テンプレート構文について説明します。

{
  "alerts": [
    {
      "annotations": {
        "message": "Namespace: arms-prom / Pod: kube-state-metrics-ccb59dbff-jljg4 / Container: kube-state-metrics memory usage has exceeded 80%, current value 15.52%",
        "value": "15.521240234375"
      },
      "endsAt": "2023-02-22T07:27:15.404000000Z",
      "fingerprint": "bec72890cc2c7b4a027e008df0cd1013",
      "labels": {
        "container": "kube-state-metrics",
        "severity": "warning",
        "instance": "10.0.80.186:10255",
        "clustername": "klyz1688-kubernetes-1",
        "alertname": "Container memory usage exceeds 80%",
        "_aliyun_arms_involvedObject_name": "klyz1688-kubernetes-1",
        "pod_name": "kube-state-metrics-ccb59dbff-jljg4",
        "_aliyun_arms_involvedObject_kind": "cluster",
        "name": "k8s_kube-state-metrics_kube-state-metrics-ccb59dbff-jljg4_arms-prom_359508f3-7e76-4740-b915-41ea48849641_0",
        "namespace": "arms-prom"
      },
      "startsAt": "2023-02-22T07:18:15.578000000Z",
      "status": "firing"
    }
  ],
  "commonAnnotations": {
    "message": "Namespace: arms-prom / Pod: kube-state-metrics-ccb59dbff-jljg4 / Container: kube-state-metrics memory usage has exceeded 80%, current value 15.52%",
    "value": "15.521240234375"
  },
  "commonLabels": {
    "container": "kube-state-metrics",
    "severity": "warning",
    "instance": "10.0.80.186:10255",
    "clustername": "klyz1688-kubernetes-1",
    "alertname": "Container memory usage exceeds 80%",
    "_aliyun_arms_involvedObject_name": "klyz1688-kubernetes-1",
    "pod_name": "kube-state-metrics-ccb59dbff-jljg4",
    "_aliyun_arms_involvedObject_kind": "cluster",
    "name": "k8s_kube-state-metrics_kube-state-metrics-ccb59dbff-jljg4_arms-prom_359508f3-7e76-4740-b915-41ea48849641_0",
    "namespace": "arms-prom"
  },
  "groupLabels": {
    "alertname": "Container memory usage exceeds 80%"
  },
  "status": "firing",
  "startTime":"2023-02-22 07:18:15",
  "endTime":"Alert resolution time",
  "level":"error",
  "dispatchRuleName":"notification policy name",
  "alarmId":"123456"
},

.の文字

現在のスコープ内のオブジェクトを表し、そのデータにアクセスできるようにします。

例 1:最上位スコープからすべての内容を取得します。最上位スコープは、サンプルコードのコンテキストデータ全体です。

{{ . }}

サンプルコードの内容全体を返します。

例 2: コンテキストデータから level フィールドを取得します。

{{ .level }}

戻り値:

error

例 3: コンテキストデータから commonLabels.alertname フィールドを取得します。

{{ .commonLabels.alertname }}

戻り値:

Container memory usage exceeds 80%

変数

テンプレートに渡されたデータを、テンプレート全体でアクセス可能な変数に保存できます。例えば、{{$context := .}} では、$context 変数に渡されたデータが保存され、{{$context}} を使用してアクセスできます。

例 1: アラート名を $alertname に代入し、次に $alertname を出力します。

{{ $alertname := .commonLabels.alertname }} {{$alertname}}

戻り値:

Container memory usage exceeds 80%

例 2: alerts リストの最初の アラートを $alert0 に代入し、その alertname を出力します。

{{ $alert0 := index .alerts 0 }} {{$alert0.labels.alertname}}

戻り値:

Container memory usage exceeds 80%

if/else アクション

if アクションは条件ロジックを提供し、条件が false の場合はオプションの else ブロックが実行されます。 0 や nil、空文字列などの値は、false として評価されます。

例 1: アラートイベントの severity ラベルの値が warning の場合、P3 alert を出力します。

{{if eq "warning" .commonLabels.severity }} P3 alert {{ end }}

戻り値:

P3 alert

例 2: アラートイベントの severity ラベルの値が critical の場合、P1 alert を出力します。それ以外の場合、P2 alert を出力します。

{{if eq "critical" .commonLabels.severity }} P1 alert {{ else }} P2 alert {{ end }}

戻り値:

P2 alert

for アクション

for アクションは、コードブロックを繰り返し実行します。

例 1: alerts リストを反復処理して、各アラートの alertname を取得します。

{{ for .alerts}} {{.labels.alertname}} \n {{end}}

戻り値:

Container memory usage exceeds 80%
 

range アクション

range アクションは、スライスやマップなどのデータ構造を反復処理します。

例 1:リストを反復処理して、各要素のインデックスと値を取得します。

{{ range $index, $alert := .alerts }}Index {{ $index }}: {{ $alert.labels.alertname }}\n{{ end }}

戻り値:

Index 0: Container memory usage exceeds 80% 

index 関数

index 関数は、配列、スライス、またはマップの要素にアクセスします。

例 1: alerts リストから最初の要素を取得し、$alert0 に割り当てます。

{{ $alert0 := index .alerts 0 }} {{$alert0.labels.alertname}}

戻り値:

Container memory usage exceeds 80%

比較関数

次の比較関数がよく使用されます:

  • eq: 等しい

  • ne: 等しくない

  • lt:未満

  • le: 以下

  • gt: より大きい

  • ge:以上

例:commonLabels 内の severity ラベルの値が warning と等しい場合、P3 alert を出力します。

{{if eq "warning" .commonLabels.severity }} P3 alert {{ end }}

戻り値:

P3 alert

論理関数:and、or、not

例:commonLabels 内の severity ラベルの値が warning または warnning と等しい場合、P3 alert を出力します。

{{if or (eq "warning" .commonLabels.severity) (eq "warnning" .commonLabels.severity) }} P3 alert {{ end }}

戻り値:

P3 alert

一般的な組み込み関数

printf :指定したフォーマット指定子に従って文字列をフォーマットします。

例: alerts リストの最初のアラートの value を、小数点以下 2 桁にフォーマットして出力します。

{{ $alert0 := index .alerts 0 }} {{ printf "%.2f" $alert0.annotations.value }}

戻り値:

15.52

humanizePercentage :値をパーセンテージとしてフォーマットします。

例:文字列 "0.95332123123124" を、より読みやすいパーセンテージに変換します。

{{ "0.95332123123124" | humanizePercentage }}

戻り値:

95.33%

humanizeDate: ミリ秒単位のタイムスタンプを日付にフォーマットします。日付形式は yyyy-MM-dd HH:mm:ss です。

例:タイムスタンプ 1671074058234 を日付に変換します。

{{ "1671074058234" | humanizeDate }}

戻り値:

2022-12-15 11:14:18

len :配列、スライス、文字列、またはマップの長さを返します。

例:alerts リストの長さを取得します。

{{ len .alerts }}

戻り値:

1

事前設定済みの通知テンプレート

E メール通知テンプレート

Alert name: {{ .commonLabels.alertname }}
{{if .commonLabels.clustername }}
Cluster name: {{ .commonLabels.clustername }}
{{ end }}
{{if eq "app" .commonLabels._aliyun_arms_involvedObject_kind }}
Application name: {{ .commonLabels._aliyun_arms_involvedObject_name }}
{{ end }}
Notification policy: {{ .dispatchRuleName }}
Alert time: {{ .startTime }}
Alert content: {{ if .newIncidentIn }} (New event) {{end}} {{ for .alerts }} {{ .annotations.message }} {{if .generatorURL }}  <a href="{{.generatorURL}}" >Details URL</a> {{ end }} {{ end }}

テンプレート内容

説明

Alert name: {{ .commonLabels.alertname }}

アラート名。デフォルトでは、通知ポリシーは alertname ラベルによってアラートをグループ化するため、通知内のすべてのイベントのアラート名は同じになります。

{{if .commonLabels.clustername }} Cluster name: {{ .commonLabels.clustername }}
                {{ end }}

イベントに clustername ラベルが含まれている場合、clustername ラベルの値はクラスター名フィールドとしてレンダリングされます。

{{if eq "app" .commonLabels._aliyun_arms_involvedObject_kind }} Application name: {{ .commonLabels._aliyun_arms_involvedObject_name }}
                {{ end }}

関連オブジェクトの種類が app の場合、アプリケーション名が含まれます。

Notification policy: {{ .dispatchRuleName }}

一致した通知ポリシーの名前です。

Alert time: {{ .startTime }}

アラートが最初にトリガーされた時刻です。

Alert content: {{ if .newIncidentIn }} (New event) {{end}} 
{{ for .alerts }} {{ .annotations.message }} {{if .generatorURL }}  <a href="{{.generatorURL}}" > Details URL</a> {{ end }} {{ end }}

アラートイベントのリストを反復処理して、message の値をレンダリングします。イベントに generatorUrl フィールドが含まれている場合は、generatorUrl フィールドの値をイベント詳細 URL としてレンダリングします。

説明

事前設定されたテンプレートは、HTML の <a></a> 構文を使用してリンクをレンダリングします。

<a href="{{.generatorURL}}">Details URL</a>

{{ if .newIncidentIn }} (New event) {{end}} ブロックは、新しいイベントがアラートグループに追加されると、New event を出力して通知をマークします。

表示結果の例:

Alert name: Container memory usage exceeds 80%
Cluster name: klyz1688-kubernetes-1
Notification policy: notification policy name
Alert time: 2023-02-22 07:18:15
Alert content: Namespace: arms-prom / Pod: kube-state-metrics-ccb59dbff-jljg4 / Container: kube-state-metrics memory usage has exceeded 80%, current value 15.52%

SMS および電話の通知テンプレート

これらのテンプレートは E メールテンプレートと同様ですが、詳細 URL を省略します。

{{ .level }} alert occurred
Alert name: {{ .commonLabels.alertname }}
{{if .commonLabels.clustername }}
Cluster name: {{ .commonLabels.clustername }}
{{ end }}
{{if eq "app" .commonLabels._aliyun_arms_involvedObject_kind }}
Application name: {{ .commonLabels._aliyun_arms_involvedObject_name }}
{{ end }}
Notification policy: {{ .dispatchRuleName }}
Alert time: {{ .startTime }}
Alert content: {{ if .newIncidentIn }} (New event)
 {{end}} {{ for .alerts }} {{ .annotations.message }} {{ end }}
            

チャットボット通知テンプレート

{{ if .newIncidentIn }} (New event) {{end}}
        {{if .commonLabels.clustername }} > Cluster name: {{ .commonLabels.clustername }}
        {{ end }}
        {{if eq "app" .commonLabels._aliyun_arms_involvedObject_kind }} > Application name: {{ .commonLabels._aliyun_arms_involvedObject_name }}
        {{ end }}
        {{ for .alerts }} > {{ .annotations.message }} {{if .generatorURL }} [details URL]({{.generatorURL}}) {{end}}{{if .annotations._aliyun_arms_insights_analyze_link }}[<font color='#ff0000'>root cause</font>]({{ .annotations._aliyun_arms_insights_analyze_link}}){{ end }}{{if eq "1" .labels._aliyun_arms_denoise_code }} (Important:{{.labels._aliyun_arms_important_reason }}) {{end}}
        {{end}}

出力は Markdown で表示されます。機能のサポート状況はチャットボットによって異なります。

  • ブロッククォート: >

  • フォント色:

    <font color='#ff0000'>root cause</font>
    説明

    フォント色の変更をサポートしているのは DingTalk と WeCom のみです。Lark はサポートしていません。

  • 改行:

    • DingTalk:テンプレートに改行を 2 つ追加します。

    • Lark:テンプレートに改行を 1 つ追加します。

    • WeCom:テンプレートに改行を 1 つ追加します。

Webhook テンプレート

{
  "alert_name":"{{ .commonLabels.alertname }}",
  {{if .commonLabels.clustername }}"cluster_name":"{{ .commonLabels.clustername }}",{{ end }}
  {{if eq "app" .commonLabels._aliyun_arms_involvedObject_kind }}"application_name":"{{ .commonLabels._aliyun_arms_involvedObject_name }}",{{ end }}
  "notification_policy":"{{ .dispatchRuleName }}",
  "alert_time":"{{ .startTime }}",
  "alert_content":"{{ range $index, $alert := .alerts }}{{if $index}}, {{end}}{{.annotations.message}}{{ end }}"
}

表示結果の例:

{
  "alert_name": "Container memory usage exceeds 80%",
  "cluster_name": "klyz1688-kubernetes-1",
  "notification_policy": "notification policy name",
  "alert_time": "2023-02-22 07:18:15",
  "alert_content": "Namespace: arms-prom / Pod: kube-state-metrics-ccb59dbff-jljg4 / Container: kube-state-metrics memory usage has exceeded 80%, current value 15.52%"
}

変数リファレンス

次の表では、通知テンプレートで使用できる変数について説明します。

パラメータ

説明

alarmId

元のアラートの ID です。

alerts

元のアラートイベントのリストです。型: List<Alert>

alerts.annotations

元のアラートのアノテーションです。型: 文字列のマップ

alerts.endsAt

元のアラートの解決時刻です。

解決時刻 = トリガー時刻 + 解決までの期間

alerts.startsAt

元のアラートのトリガー時刻です。

alerts.fingerprint

元のアラートのフィンガープリントです。同じラベルセットを持つアラートは同じフィンガープリントになります。

alerts.labels

元のアラートのラベルです。

alerts.status

元のアラートのステータス:

  • firing:アラートはアクティブです。

  • resolved:アラートは解決済みです。

commonAnnotations

グループ内のすべてのアラートに共通するアノテーションのセットです。

commonLabels

グループ内のすべてのアラートに共通するラベルのセットです。

groupLabels

通知ポリシーで定義された、アラートをグループ化するためのラベルです。

status

アラートグループのステータス:

  • firing:アラートはアクティブです。

  • resolved:アラートは解決済みです。

startTime

アラートグループが作成された時刻です。

endTime

アラートグループが解決された時刻です。これは、グループ内の最後の元のアラートの解決時刻です。

level

通知の重要度レベルです。値は優先度レベルにマッピングされます:

  • critical:P1

  • error:P2

  • warning:P3

  • page:P4

dispatchRuleName

一致した通知ポリシーの名前です。