Prometheus Alertmanager告警模板

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数据结构


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




| Name | Type | Notes |
| --- | --- | --- |
| Status | string | Defines whether or not the alert is resolved or currently firing. |
| Labels | KV | A set of labels to be attached to the alert. |
| Annotations | KV | A set of annotations for the alert. |
| StartsAt | time.Time | The time the alert started firing. If omitted, the current time is assigned by the Alertmanager. |
| EndsAt | time.Time | Only 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. |
| GeneratorURL | string | A backlink which identifies the causing entity of this alert. |


### 3.1 Labels


**Labels**为prometheus web上告警时的label,如下  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/2020121910333177.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTAwMzk0MTg=,size_16,color_FFFFFF,t_70)


### 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的时间戳来确定时间是否满足我们的需求,这样我们收到的告警时间才是符合我们所在时区。




**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数Linux运维工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/f80cee525d65af96a331899026e6a86e.png)
![img](https://img-blog.csdnimg.cn/img_convert/b610dcffcff4f157b688ea73bb80800c.png)
![img](https://img-blog.csdnimg.cn/img_convert/c1b986b1944d6b3783a4ebc693a16608.png)
![img](https://img-blog.csdnimg.cn/img_convert/44babb80a8a909be73792d00e10e5998.png)
![img](https://img-blog.csdnimg.cn/img_convert/0942bde0b589fe43a23b23d982697118.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Linux运维知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以添加VX:vip1024b (备注Linux运维获取)**
![img](https://img-blog.csdnimg.cn/img_convert/590bc4477a5d7587c7567009d17b808c.jpeg)

![](https://img-blog.csdnimg.cn/img_convert/9a8cb5f8c0ec69e6499adead0da6e95b.png)



最全的Linux教程,Linux从入门到精通

======================

1.  **linux从入门到精通(第2版)**

2.  **Linux系统移植**

3.  **Linux驱动开发入门与实战**

4.  **LINUX 系统移植 第2版**

5.  **Linux开源网络全栈详解 从DPDK到OpenFlow**



![华为18级工程师呕心沥血撰写3000页Linux学习笔记教程](https://img-blog.csdnimg.cn/img_convert/59742364bb1338737fe2d315a9e2ec54.png)



第一份《Linux从入门到精通》466页

====================

内容简介

====

本书是获得了很多读者好评的Linux经典畅销书**《Linux从入门到精通》的第2版**。本书第1版出版后曾经多次印刷,并被51CTO读书频道评为“最受读者喜爱的原创IT技术图书奖”。本书第﹖版以最新的Ubuntu 12.04为版本,循序渐进地向读者介绍了Linux 的基础应用、系统管理、网络应用、娱乐和办公、程序开发、服务器配置、系统安全等。本书附带1张光盘,内容为本书配套多媒体教学视频。另外,本书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。



![华为18级工程师呕心沥血撰写3000页Linux学习笔记教程](https://img-blog.csdnimg.cn/img_convert/9d4aefb6a92edea27b825e59aa1f2c54.png)



**本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。**

> 需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论




**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
![img](https://img-blog.csdnimg.cn/img_convert/0f46371363bb2221082096b62d64b782.jpeg)


> 需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论




**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
[外链图片转存中...(img-fA8tBsie-1712856195983)]

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值