

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

# 在 Amazon Managed Service for Prometheus 中建立警示管理員組態，以管理和路由警示
<a name="AMP-alertmanager-config"></a>

若要在 Amazon Managed Service for Prometheus 中使用警示管理員和範本，請建立警示管理員組態 YAML 檔案。Amazon Managed Service for Prometheus 警示管理員檔案分成兩個主要部份：
+ `template_files:` 包含用於接收者傳送訊息的範本。如需詳細資訊，請參閱 Prometheus 說明文件中的[範本參考](https://prometheus.io/docs/prometheus/latest/configuration/template_reference/)和[範本範例](https://prometheus.io/docs/prometheus/latest/configuration/template_examples/)。
+ `alertmanager_config:` 包含警示管理員組態。這使用與獨立 Prometheus 中的警示管理員組態檔案相同的結構。如需詳細資訊，請參閱警示管理員說明文件中的[組態](https://prometheus.io/docs/alerting/latest/configuration/)。
**注意**  
上述 Prometheus 說明文件中描述的 `repeat_interval` 有額外的 Amazon Managed Service for Prometheus 限制。允許的值上限為五天。若您將其設為大於五天，這將視為五天且經過五天期間後將再次傳送通知。

**注意**  
您也可以直接在 Amazon Managed Service for Prometheus 主控台中編輯組態檔案，但仍必須遵循此處指定的格式。如需上傳或編輯組態檔案的詳細資訊，請參閱 [將警示管理員組態檔案上傳至 Amazon Managed Service for Prometheus](AMP-alertmanager-upload.md)。

在 Amazon Managed Service for Prometheus 中，您的警示管理員組態檔案必須在 YAML 檔案根目錄的 `alertmanager_config` 金鑰內包含所有警示管理員組態內容。

以下是警示管理員設定檔的基本範例：

```
alertmanager_config: |
  route:
    receiver: 'default'
  receivers:
    - name: 'default'
      sns_configs:
      - topic_arn: arn:aws:sns:us-east-2:123456789012:My-Topic
        sigv4:
          region: us-east-2
        attributes:
          key: key1
          value: value1
```

目前唯一支援的接收器即 Amazon Simple Notification Service (Amazon SNS)。若您在組態中列出的其他接收者類型，則將會予以拒絕。

以下是其他同時使用 `template_files` 區塊和 `alertmanager_config` 區塊的警示管理員設定檔範例。

```
template_files:
  default_template: |
    {{ define "sns.default.subject" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}]{{ end }}
    {{ define "__alertmanager" }}AlertManager{{ end }}
    {{ define "__alertmanagerURL" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver | urlquery }}{{ end }}
alertmanager_config: |
  global:
  templates:
    - 'default_template'
  route:
    receiver: default
  receivers:
    - name: 'default'
      sns_configs:
      - topic_arn: arn:aws:sns:us-east-2:accountid:My-Topic
        sigv4:
          region: us-east-2
        attributes:
          key: severity
          value: SEV2
```

**預設 Amazon SNS 範本區塊**

除非您明確覆寫，否則預設 Amazon SNS 組態會使用下列範本。

```
{{ define "sns.default.message" }}{{ .CommonAnnotations.SortedPairs.Values | join " " }}
  {{ if gt (len .Alerts.Firing) 0 -}}
  Alerts Firing:
    {{ template "__text_alert_list" .Alerts.Firing }}
  {{- end }} 
  {{ if gt (len .Alerts.Resolved) 0 -}} 
  Alerts Resolved:
    {{ template "__text_alert_list" .Alerts.Resolved }}
  {{- end }}
{{- end }}
```