

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 設定警示管理員以 JSON 形式傳送訊息至 Amazon SNS
<a name="AMP-alertmanager-receiver-JSON"></a>

根據預設，Amazon Managed Service for Prometheus 警示管理員會以純文字清單格式輸出訊息。對於其他 服務來說，這可能更難剖析。您可以設定警示管理員改為以 JSON 格式傳送警示。JSON 可以更輕鬆地處理 Webhook 接收端點中 AWS Lambda Amazon SNS 下游的訊息。您可以定義自訂範本而非使用預設範本，以 JSON 格式輸出訊息內容，以便在下游函數中更容易剖析。

若要以 JSON 格式將訊息從警示管理員輸出至 Amazon SNS，請更新警示管理員組態，以在 `template_files` 根區段中包含下列代碼：

```
default_template: |
   {{ define "sns.default.message" }}{{ "{" }}"receiver": "{{ .Receiver }}","status": "{{ .Status }}","alerts": [{{ range $alertIndex, $alerts := .Alerts }}{{ if $alertIndex }}, {{ end }}{{ "{" }}"status": "{{ $alerts.Status }}"{{ if gt (len $alerts.Labels.SortedPairs) 0 -}},"labels": {{ "{" }}{{ range $index, $label := $alerts.Labels.SortedPairs }}{{ if $index }}, {{ end }}"{{ $label.Name }}": "{{ $label.Value }}"{{ end }}{{ "}" }}{{- end }}{{ if gt (len $alerts.Annotations.SortedPairs ) 0 -}},"annotations": {{ "{" }}{{ range $index, $annotations := $alerts.Annotations.SortedPairs }}{{ if $index }}, {{ end }}"{{ $annotations.Name }}": "{{ $annotations.Value }}"{{ end }}{{ "}" }}{{- end }},"startsAt": "{{ $alerts.StartsAt }}","endsAt": "{{ $alerts.EndsAt }}","generatorURL": "{{ $alerts.GeneratorURL }}","fingerprint": "{{ $alerts.Fingerprint }}"{{ "}" }}{{ end }}]{{ if gt (len .GroupLabels) 0 -}},"groupLabels": {{ "{" }}{{ range $index, $groupLabels := .GroupLabels.SortedPairs }}{{ if $index }}, {{ end }}"{{ $groupLabels.Name }}": "{{ $groupLabels.Value }}"{{ end }}{{ "}" }}{{- end }}{{ if gt (len .CommonLabels) 0 -}},"commonLabels": {{ "{" }}{{ range $index, $commonLabels := .CommonLabels.SortedPairs }}{{ if $index }}, {{ end }}"{{ $commonLabels.Name }}": "{{ $commonLabels.Value }}"{{ end }}{{ "}" }}{{- end }}{{ if gt (len .CommonAnnotations) 0 -}},"commonAnnotations": {{ "{" }}{{ range $index, $commonAnnotations := .CommonAnnotations.SortedPairs }}{{ if $index }}, {{ end }}"{{ $commonAnnotations.Name }}": "{{ $commonAnnotations.Value }}"{{ end }}{{ "}" }}{{- end }}{{ "}" }}{{ end }}
   {{ define "sns.default.subject" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}]{{ end }}
```

**注意**  
此版本會從英數字元資料建立 JSON。如果您的資料具有特殊字元，請在使用此範本之前對其進行編碼。

若要確認已在送出通知中使用此範本，則請在 `alertmanager_config` 區塊中依照下列方式參考：

```
alertmanager_config: |
  global:
  templates:
    - 'default_template'
```

**注意**  
此範本適用於整個郵件內文的 JSON 格式。此範本會覆寫整個訊息內文。如果您想要使用此特定範本，則無法覆寫訊息內文。任何手動完成的覆寫都會優先於範本。

如需更多相關資訊：
+ 警示管理員組態檔案，請參閱 [在 Amazon Managed Service for Prometheus 中建立警示管理員組態，以管理和路由警示](AMP-alertmanager-config.md)。
+ 上傳您的組態檔案時，請參閱 [將警示管理員組態檔案上傳至 Amazon Managed Service for Prometheus](AMP-alertmanager-upload.md)。