通知ポリシーのアラートルールがトリガーされると、ARMS は DingTalk、Lark、WeCom、SMS、電話、メール、または Webhook を介して通知を送信します。各チャネルにはプリセットテンプレートがあり、Go テンプレート 構文を使用してカスタマイズできます。
このページでは、各チャネルのプリセットテンプレート、利用可能なテンプレート変数、およびカスタムテンプレートを作成するための Go テンプレート構文について説明します。
プリセット通知テンプレート
ARMS は、各通知チャネルにプリセットテンプレートを提供します。各テンプレートは、Go テンプレート構文を使用してアラートデータを読みやすいメッセージにレンダリングします。
メール
{
"アラート名":"{{ .commonLabels.alertname }}
{{if .commonLabels.clustername }}", "クラスター名":"{{ .commonLabels.clustername }} {{ end }}
{{if eq "app" .commonLabels._aliyun_arms_involvedObject_kind }}", "アプリケーション名":"{{ .commonLabels._aliyun_arms_involvedObject_name }} {{ end }}",
"通知ポリシー":"{{ .dispatchRuleName }}",
"アラート発生時刻":"{{ .startTime }}",
"アラート内容":"{{ for .alerts }} {{ .annotations.message }} {{ end }}"
}次の表は、テンプレートの各セクションについて説明しています。
| テンプレートセクション | 説明 |
|---|---|
{{ .commonLabels.alertname }} | アラート名。デフォルトでは、通知ポリシーが alertname を基準にアラートをグループ化するため、同一グループ内のすべてのイベントは同じ名前になります。 |
{{if .commonLabels.clustername }} | clustername ラベルがイベントに存在する場合のみ、クラスター名をレンダリングします。 |
{{if eq "app" .commonLabels._aliyun_arms_involvedObject_kind }} | アラート対象オブジェクトのタイプが app の場合のみ、アプリケーション名をレンダリングします。 |
{{ .dispatchRuleName }} | アラートにマッチした通知ポリシーの名称。 |
{{ .startTime }} | アラートがトリガーされた時刻。 |
{{ if .newIncidentIn }} (新規イベント) {{end}} | アラートグループ内で新規イベントが発生した場合、通知に (新規イベント) のマークを付与します。 |
{{ for .alerts }} {{.annotations.message}} ... {{end}} | すべてのアラートイベントを反復処理し、各 message アノテーションをレンダリングします。また、generatorURL が存在する場合は、<a href="..."> を使用してクリック可能な詳細リンクをレンダリングします。 |
出力例:
アラート名: コンテナのメモリ使用量が 80% を超えました。
クラスター名: klyz1688-kubernetes-1
通知ポリシー: Notification policy name
アラートトリガー時刻: 2023-02-22 07:18:15
アラート内容: 名前空間: arms-prom / Pod: kube-state-metrics-ccb59dbff-jljg4 / コンテナ: kube-state-metrics。コンテナのメモリ使用量が 80% を超えました。現在の値: 15.52%。SMS と電話
SMS および電話のテンプレートは、メールテンプレートと同じ構造を共有しますが、詳細 URL は省略されます。
{{ .level }}チャットボット (DingTalk、Lark、WeCom)
チャットボットテンプレートは Markdown 出力をレンダリングします。書式設定のサポートはプラットフォームによって異なります。
{{ 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'>根本原因</font>]({{ .annotations._aliyun_arms_insights_analyze_link}}){{ end }}{{if eq "1" .labels._aliyun_arms_denoise_code }} (重要: {{.labels._aliyun_arms_important_reason }}) {{end}}
{{end}}チャットボットの Markdown サポート:
| 機能 | 構文 | 対応プラットフォーム |
|---|---|---|
| ブロック引用 | > | DingTalk、Lark、WeCom |
| フォントカラー | <font color='#ff0000'>text</font> | DingTalk と WeCom のみ。Lark はフォントカラーをサポートしていません。 |
| 改行 | テンプレートで [Enter] を 2 回押します | DingTalk のみ |
| 改行 | テンプレートで [Enter] を 1 回押します | Lark、WeCom |
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 triggered at":"{{ .startTime }}",
"Alert content":"{{ for .alerts }} {{ .annotations.message }} {{ end }}"
}出力例:
{
"Alert name": "コンテナのメモリ使用量が 80% を超えました。",
"Cluster name": "klyz1688-kubernetes-1",
"Notification policy": "通知ポリシー名",
"Alert triggered at": "2023-02-22 07:18:15",
"Alert content": "名前空間: arms-prom / Pod: kube-state-metrics-ccb59dbff-jljg4 / コンテナ: kube-state-metrics。コンテナのメモリ使用量が 80% を超えました。現在の値: 15.52%。"
}テンプレート変数
これらの変数は、アラートデータのレンダリングのために通知テンプレートで利用できます。
| 変数 | 型 | 説明 |
|---|---|---|
alarmId | String | 元のアラートの ID。 |
alerts | List<Alert> | このグループに含まれる元のアラートイベントの一覧。 |
alerts.annotations | Map<String, String> | 元のアラートのアノテーション。 |
alerts.endsAt | Timestamp | アラートがクリアされた時刻。トリガー時刻にクリア期間を加算した値と等しい。 |
alerts.startsAt | Timestamp | アラートがトリガーされた時刻。 |
alerts.fingerprint | String | アラートの指紋。同じラベルセットを持つアラートは、同一の指紋を共有します。 |
alerts.labels | Map<String, String> | 元のアラートのラベル。 |
alerts.status | String | アラートのステータス。有効な値: firing(アラートがアクティブ)、resolved(アラートがクリア済み)。 |
commonAnnotations | Map<String, String> | このグループ内のすべてのアラートで共有されるアノテーション。 |
commonLabels | Map<String, String> | このグループ内のすべてのアラートで共有されるラベル。 |
groupLabels | Map<String, String> | 通知ポリシーグループの条件で定義された、アラートをグループ化するために使用されるラベル。 |
status | String | アラートグループのステータス。有効な値: firing(1 つ以上のアラートがアクティブ)、resolved(すべてのアラートがクリア済み)。 |
startTime | String | アラートグループが作成された時刻。 |
endTime | String | アラートグループがクリアされた時刻。これは、グループ内の最後のアラートが解決された時刻です。 |
level | String | 重要度レベル。有効な値: critical(P1)、error(P2)、warning(P3)、page(P4)。 |
dispatchRuleName | String | このアラートグループにマッチした通知ポリシーの名称。 |
アラートパラメーターは複数のラベルで構成されます。テンプレート内で特定のアラートデータを参照するには、ラベルを使用します。たとえば、.commonLabels.alertname はアラート名を返します。
アラートコンテキストの例
次の JSON は、Go テンプレート構文 セクションのすべての例が参照する完全なアラートコンテキストを示しています。
{
"alerts": [
{
"annotations": {
"message": "Namespace: arms-prom / Pod: kube-state-metrics-ccb59dbff-jljg4 / Container: kube-state-metrics. The memory usage of the container 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": "The memory usage of the container has exceeded 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. The memory usage of the container 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": "The memory usage of the container has exceeded 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": "The memory usage of the container has exceeded 80%."
},
"status": "firing",
"startTime": "2023-02-22 07:18:15",
"endTime": "The time when the alert was cleared",
"level": "error",
"dispatchRuleName": "Notification policy name",
"alarmId": "123456"
}Go テンプレート構文
ARMS は、通知テンプレートをレンダリングするために Go テンプレート 構文を使用します。レンダリング時、テンプレートにはアラートコンテキストからのデータが入力されます。
ドット表記
ドット (.) は、現在のコンテキストスコープを表します。
コンテキスト全体にアクセス:
{{ . }}出力:アラートコンテキスト内のすべてのデータを返します。
トップレベルフィールドにアクセス:
{{ .level }}出力:
errorネストされたフィールドにアクセス:
{{ .commonLabels.alertname }}出力:
The memory usage of the container has exceeded 80%.変数
再利用のためにコンテキストデータを変数に格納します。
フィールドを変数に割り当て:
{{ $alertname := .commonLabels.alertname }} {{$alertname}}出力:
The memory usage of the container has exceeded 80%.変数を介して配列要素にアクセス:
{{ $alert0 := index .alerts 0 }} {{$alert0.labels.alertname}}出力:
The memory usage of the container has exceeded 80%.条件文: if/else
if 条件は、null 値、0、nil ポインター、空の文字列、および長さが 0 の文字列の場合に false と評価されます。
単純な条件:
{{if eq "warning" .commonLabels.severity }} P3 alert {{ end }}出力:
P3 alertif/else:
P2 アラート出力:
P2 alertループ: for
リストを反復処理します。
アラートをトラバースし、各アラート名を取得:
{{ for .alerts}} {{.labels.alertname}} \n {{end}}出力:
The memory usage of the container has exceeded 80%.ループ: range
値のシーケンスをトラバースします。
0 から 9 までの整数を反復処理:
{{ $ran := range 0 10 }} {{ for $ran }} {{.}} {{end}}出力:
0 1 2 3 4 5 6 7 8 9index 関数
インデックスによって配列から特定の要素を取得します。
最初のアラートイベントを取得:
{{ $alert0 := index .alerts 0 }} {{$alert0.labels.alertname}}出力:
The memory usage of the container has exceeded 80%.比較関数
| 関数 | 説明 |
|---|---|
eq | 等しい |
ne | 等しくない |
lt | より小さい |
le | 以下 |
gt | より大きい |
ge | 以上 |
例:
{{if eq "warning" .commonLabels.severity }} P3 alert {{ end }}出力:
P3 alert論理演算子: and、or、not
論理演算子で条件を結合します。
例:
{{if eq "warning" .commonLabels.severity or eq "warning" .commonLabels.severity }} P3 alert {{ end }}出力:
P3 alert組み込み関数
printf
出力データをフォーマットします。
小数点以下 2 桁に丸める:
{{ $alert0 := index .alerts 0 }} {{ printf "%.2f" $alert0.annotations.value }}出力:
15.52humanizePercentage
10 進数文字列を読みやすいパーセンテージに変換します。
例:
{{ "0.95332123123124" | humanizePercentage }}出力:
95.33%humanizeDate
ミリ秒単位のタイムスタンプをフォーマット済みの日付に変換します。デフォルトのフォーマット:yyyy-MM-dd HH:mm:ss。
例:
{{ "1671074058234" | humanizeDate }}出力:
2022-12-15 11:14:18len
配列の長さを返します。
例:
{{ len .alerts }}出力:
1