Prometheus Alertmanager告警模板

1、告警模板

关于Alertmanager的告警模板,我们以上篇《Prometheus配置和使用Alertmanager发送告警至企业微信》的模板为例,对其做个说明,

[root@centos74 home]# cat /usr/local/prometheus/alertmanager/wechat.tmpl
{{ define "wechat.default.message" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts -}}
======== 异常告警 ========
告警名称:{{ $alert.Labels.alertname }}
告警级别:{{ $alert.Labels.severity }}
告警机器:{{ $alert.Labels.instance }} {{ $alert.Labels.device }}
告警详情:{{ $alert.Annotations.summary }}
告警时间:{{ $alert.StartsAt.Format "2006-01-02 15:04:05" }}
========== END ==========
{{- end }}
{{- end }}
{{- if gt (len .Alerts.Resolved) 0 -}}
{{- range $index, $alert := .Alerts -}}
======== 告警恢复 ========
告警名称:{{ $alert.Labels.alertname }}
告警级别:{{ $alert.Labels.severity }}
告警机器:{{ $alert.Labels.instance }}
告警详情:{{ $alert.Annotations.summary }}
告警时间:{{ $alert.StartsAt.Format "2006-01-02 15:04:05" }}
恢复时间:{{ $alert.EndsAt.Format "2006-01-02 15:04:05" }}
========== END ==========
{{- end }}
{{- end }}
{{- end }}

2、主要语法

模板是基于go语言的template——https://golang.org/pkg/text/template/

2.1 Text and spaces

By default, all text between actions is copied verbatim when the template 
is executed. For example, the string " items are made of " in the example
above appears on standard output when the program is run.

However, to aid in formatting template source code, if an action's left 
delimiter (by default "{{") is followed immediately by a minus sign and 
ASCII space character ("{{- "), all trailing white space is trimmed from 
the immediately preceding text. Similarly, if the right delimiter ("}}") 
is preceded by a space and minus sign (" -}}"), all leading white space 
is trimmed from the immediately following text. In these trim markers, 
the ASCII space must be present; "{{-3}}" parses as an action containing 
the number -3.

For instance, when executing the template whose source is

"{{23 -}} < {{- 45}}"

the generated output would be

"23<45"

For this trimming, the definition of white space characters is the same 
as in Go: space, horizontal tab, carriage return, and newline.

2.2 Actions

Here is the list of actions. "Arguments" and "pipelines" are evaluations 
of data, defined in detail in the corresponding sections that follow.

{{/* a comment */}}
{{- /* a comment with white space trimmed from preceding and following text */ -}}
	A comment; discarded. May contain newlines.
	Comments do not nest and must start and end at the
	delimiters, as shown here.

{{pipeline}}
	The default textual representation (the same as would be
	printed by fmt.Print) of the value of the pipeline is copied
	to the output.

{{if pipeline}} T1 {{end}}
	If the value of the pipeline is empty, no output is generated;
	otherwise, T1 is executed. The empty values are false, 0, any
	nil pointer or interface value, and any array, slice, map, or
	string of length zero.
	Dot is unaffected.

{{if pipeline}} T1 {{else}} T0 {{end}}
	If the value of the pipeline is empty, T0 is executed;
	otherwise, T1 is executed. Dot is unaffected.

{{if pipeline}} T1 {{else if pipeline}} T0 {{end}}
	To simplify the appearance of if-else chains, the else action
	of an if may include another if directly; the effect is exactly
	the same as writing
		{{if pipeline}} T1 {{else}}{{if pipeline}} T0 {{end}}{{end}}

{{range pipeline}} T1 {{end}}
	The value of the pipeline must be an array, slice, map, or channel.
	If the value of the pipeline has length zero, nothing is output;
	otherwise, dot is set to the successive elements of the array,
	slice, or map and T1 is executed. If the value is a map and the
	keys are of basic type with a defined order, the elements will be
	visited in sorted key order.

{{range pipeline}} T1 {{else}} T0 {{end}}
	The value of the pipeline must be an array, slice, map, or channel.
	If the value of the pipeline has length zero, dot is unaffected and
	T0 is executed; otherwise, dot is set to the successive elements
	of the array, slice, or map and T1 is executed.

{{template "name"}}
	The template with the specified name is executed with nil data.

{{template "name" pipeline}}
	The template with the specified name is executed with dot set
	to the value of the pipeline.

{{block "name" pipeline}} T1 {{end}}
	A block is shorthand for defining a template
		{{define "name"}} T1 {{end}}
	and then executing it in place
		{{template "name" pipeline}}
	The typical use is to define a set of root templates that are
	then customized by redefining the block templates within.

{{with pipeline}} T1 {{end}}
	If the value of the pipeline is empty, no output is generated;
	otherwise, dot is set to the value of the pipeline and T1 is
	executed.

{{with pipeline}} T1 {{else}} T0 {{end}}
	If the value of the pipeline is empty, dot is unaffected and T0
	is executed; otherwise, dot is set to the value of the pipeline
	and T1 is executed.

3、Alert数据结构

告警的数据结构主要如下,

NameTypeNotes
StatusstringDefines whether or not the alert is resolved or currently firing.
LabelsKVA set of labels to be attached to the alert.
AnnotationsKVA set of annotations for the alert.
StartsAttime.TimeThe time the alert started firing. If omitted, the current time is assigned by the Alertmanager.
EndsAttime.TimeOnly set if the end time of an alert is known. Otherwise set to a configurable timeout period from the time since the last alert was received.
GeneratorURLstringA backlink which identifies the causing entity of this alert.

3.1 Labels

Labels为prometheus web上告警时的label,如下
在这里插入图片描述

3.2 Annotations

Annotations为用户在告警规则里定义的annotations字段,

groups:
- name: node_health
  rules:
  - alert: HighMemoryUsage
    expr: node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes < 0.9
    for: 1m
    labels:
      severity: warning
    annotations:
      summary: High memory usage

3.3 StartsAt和EndsAt

StartsAt用于告警触发的时间,EndsAt则用于告警恢复的时间。

如果我们在告警模板中直接使用$alert.StartsAt,得到的时间格式如下,

告警时间:2020-12-16 22:35:33.676515606 +0800 CST

这个时间也就是和我们机器上的时间一致,因此如果需要保证机器上的时间是我们需要的时区。

可以通过tzselect命令设置时区,完成后最好重启下系统,通过/var/log/messages的时间戳来确定时间是否满足我们的需求,这样我们收到的告警时间才是符合我们所在时区。

不过对于我们的告警不需要精确到纳秒级别,也不需要显示时区,那就需要对这个时间进行格式化,这也是我们模板中使用$alert.StartsAt.Format的原因,至于其中的"2006-01-02 15:04:05",可以理解为时间格式,而且还必须就是这个时间,不可以修改,就当做是魔术字吧,go的开发者就是这么个性。

在使用邮件告警时,一般使用如下格式,

告警时间: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}

其中Add 28800e9表示在基准时间上添加8小时,28800e9是8小时的纳秒数。这就是从UTC时间转换到北京东八区时间。

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 好的,关于 Prometheus Alertmanager 的邮件告警配置,你可以按照以下步骤进行: 1. 在 Alertmanager 的配置文件 alertmanager.yml 中添加以下内容: ``` route: receiver: 'email' group_by: ['alertname'] group_wait: 30s group_interval: 5m repeat_interval: 4h receivers: - name: 'email' email_configs: - to: 'xxx@xxx.com' from: 'xxx@xxx.com' smarthost: 'smtp.xxx.com:25' auth_username: 'xxx' auth_password: 'xxx' starttls_policy: 'mayskip' send_resolved: true ``` 这里的配置中,receiver 指定了告警通知的接收方式,这里是通过邮件发送,group_by 指定了按照 alertname 进行分组,group_wait 和 group_interval 分别指定了等待时间和分组时间间隔,repeat_interval 指定了重复发送告警的时间间隔。 receivers 中的 name 指定了接收器的名称,email_configs 中指定了邮件发送的相关参数,包括收件人、发件人、SMTP 服务器地址、SMTP 认证用户名和密码等。 2. 配置 SMTP 服务器的认证方式。Alertmanager 支持多种认证方式,包括 PLAIN、LOGIN、CRAM-MD5 等。根据 SMTP 服务器的要求进行配置即可。 3. 重启 Alertmanager 服务,使配置生效。 这样就完成了 Prometheus Alertmanager 的邮件告警配置。当出现告警时,Alertmanager 会根据配置发送邮件给指定的收件人。 ### 回答2: Prometheus是一个非常流行的开源监控工具,它提供了很多功能来监控各种系统和服务。AlertmanagerPrometheus的一部分,用于处理和发送告警消息。 要配置Alertmanager发送邮件告警,首先需要编辑Alertmanager的配置文件alertmanager.yml。在配置文件中,需要定义邮件接收者和SMTP服务器的详细信息。以下是一个简单的示例配置: ``` global: smtp_smarthost: 'smtp.example.com:587' smtp_from: 'alertmanager@example.com' smtp_auth_username: 'username' smtp_auth_password: 'password' route: group_by: ['job'] group_wait: 30s group_interval: 5m repeat_interval: 1h receiver: 'email' receivers: - name: 'email' email_configs: - to: 'recipient@example.com' ``` 在上面的配置中,我们定义了全局的SMTP服务器和发件人信息。其中'smtp_smarthost'是SMTP服务器的地址和端口,'smtp_from'是发件人的邮件地址,'smtp_auth_username'和'smtp_auth_password'是SMTP服务器的认证信息。 然后,在'route'部分中定义了告警的路由规则。此示例中,我们将告警按照'job'进行分组,每个组的告警等待时间为30秒,组间间隔为5分钟,重复发送间隔为1小时。最后,如果满足路由规则,将发送到名为'email'的接收者。 在'receivers'部分,我们定义了接收者'email'的详细信息,包括收件人的邮件地址。 保存并关闭配置文件后,重新启动Alertmanager服务,新的配置将生效。 这样,当Prometheus监测到有告警触发时,Alertmanager将根据配置发送邮件给指定的接收者。注意确保SMTP服务器配置正确,并且能够成功发送邮件。 ### 回答3: 为了配置Prometheus Alertmanager发送邮件告警,我们需要修改alertmanager.yml文件。首先,确保已经安装了PrometheusAlertmanager,并且已经使用了正确的配置文件路径。以下是一个示例的alertmanager.yml配置文件: ``` global: smtp_smarthost: 'smtp.example.com:587' smtp_from: 'alertmanager@example.com' smtp_auth_username: 'username' smtp_auth_password: 'password' smtp_auth_identity: '' smtp_require_tls: true route: group_by: ['alertname', 'job'] group_wait: 10s group_interval: 5m repeat_interval: 3h receiver: 'email-alert' receivers: - name: 'email-alert' email_configs: - to: 'your-email@example.com' ``` 在上述配置文件中,我们进行了以下配置: 1. 全局设置: - `smtp_smarthost`:SMTP服务器的地址和端口。 - `smtp_from`:发送邮件的地址。 - `smtp_auth_username`:SMTP服务器的用户名。 - `smtp_auth_password`:SMTP服务器的密码。 - `smtp_auth_identity`:SMTP服务器的身份验证标识。 - `smtp_require_tls`:是否需要启用TLS加密。 2. 路由设置: - `group_by`:用于分组告警的标签。 - `group_wait`:相同分组告警之间的等待时间。 - `group_interval`:发送相同分组告警的时间间隔。 - `repeat_interval`:重复发送告警的时间间隔。 - `receiver`:指定接收告警的接收器。 3. 接收器设置: - `name`:接收器的名称。 - `email_configs`:邮件相关的配置。 - `to`:接收告警的邮箱地址。 请根据实际情况修改这些配置项,确保将邮件服务器的地址、端口、邮箱地址及密码等信息替换为实际的内容。修改完成后,保存文件并重新启动Alertmanager服务,配置的邮件告警将生效。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值