关于prometheus:prometheus实战告警模板编写四

6次阅读

共计 4201 个字符,预计需要花费 11 分钟才能阅读完成。

本篇文章次要介绍如何编写 alertmanager 的告警模板,应用这个告警模板,能够格式化咱们的告警信息,让告警内容更加易读和好看

prometheus 告警规定详解: https://www.dogfei.cn/archives/alertmanager-rule
prometheus 配置文件参考与介绍: https://www.dogfei.cn/archives/prometheus-config

告警模板

上篇文章介绍了对于告警规定的一些编写,在应用企业微信告警的时候,提到了告警模板,这里具体说下告警模板如何写。

首先,告警模板是基于 go 语言的模板来写的,具体可参考官网文档:https://golang.org/pkg/text/template/, 这里列举一些罕用的写法,而后再去看上面的示例,就晓得什么意思了。

"."的用法

写过 helm 的肯定晓得这个'.', 就是你定义了一个模板,当给这个模板传数据的时候,能够应用 ’.’ 来拜访获取对应的变量

{{.Country.Name.Age}}

这种就是通过链式拜访这个简单类型的数据。

模板变量

在模板中定义了变量后,在整个模板中都能应用,例如:

{{$Name := "fei"}}
hello {{$Name}}

define

{{define "this.is.template"}}

定义了一个名为 ”this.is.template” 的模板

if/else 语句

满足条件继续执行,不满足则去执行 else,不满足条件能够是一些空值,或者 false, 例如false,0,nill, 空字符串

{{if .Name}} 
hello {{.Name}}
{{else}}
no one!!!
{{end}}

这里就是判断 Name 是否有值,有就会输入 hello {{.Name}},没有输入 no one,完结用 {{end}}
多条件能够应用

{{if .Name1}}
hello {{.Name1}}
{{else if .Name2}}
hello {{.Name2}}
{{else}}
no one
{{end}}

Range 语法

模板里应用 range 来进行遍历数据,相似于 jinja2 模板语言中的 for,例如:
假如数据结构为:

type Info struct {
    Name string
    Age int
}

而后模板写法如下:

{{range .Info}}
name: {{.Name}}
age: {{.Age}}
{{end}}

比拟

  • eq 等于
  • ne 不等于
  • lt 小于
  • le 小于等于
  • gt 大于
  • ge 大于等于

能够做判断比拟之类的

{{if gt .Number1 .Number2}}
{{.Number1}}大于{{.Number2}}
{{end}}

逻辑运算

  • and 全都满足,返回true
  • not 取反
  • or 有一个为true, 即可返回true

做一些逻辑运算,比如说

{{if and .Username .Passwd}}
begin login
{{if gt (len .Passwd) 16 }}
passwd valid
{{end}}
{{else}}
login faild
{{end}}
{{if not .Authenticated}}
access deny
{{end}}

内置函数

  • title: 将字符串转换为首字母大写
  • toUpper: 所有字母转换成大写
  • toLower: 所有字母转换成小写
  • join: 拼接字符串
  • safeHtml: 将字符串标记为不须要主动本义的 html
  • len: 获取长度

简略举例:

{{"abcd" | toUpper}}
{{"ABCD" | toLower}}
{{.Values | join ","}}

移除空格

写过 ansible-playbook 的肯定晓得,在应用 jinja2 写模板的时候,缩进、空格会让人很头疼,go 语言的模板同样也是如此,也是应用 - 减号来做解决

{{-}} #去掉右边的空格
{{-}} #去掉左边的空格
{{- -}} #去掉两边所有的空格

数据结构介绍

.Receiver: 接收器的名称
.Status: 如果正在告警,值为 firing,复原为 resolved
.Alerts: 所有告警对象的列表,是一个列表,.Alerts.Firing: 告警列表
.Alerts.Resolved: 复原列表
.GroupLabels: 告警的分组标签
.CommonLabels: 所有告警共有的标签
.CommonAnnotations: 所有告警共有的注解
.ExternalURL: 告警对应的 alertmanager 连贯地址

具体能够看下报警进去的信息:

{
    'receiver': 'webhook',
    'status': 'firing',
    'alerts': [{
        'status': 'firing',
        'labels': {
            'alertname': '内存使用率',
            'instance': '10.127.92.100',
            'job': 'sentry',
            'severity': 'warning',
            'team': 'ops'
        },
        'annotations': {
            'description': '内存使用率已超过 55%,内存使用率:58%',
            'summary': '内存使用率'
        },
        'startsAt': '2020-12-30T07:20:08.775177336Z',
        'endsAt': '0001-01-01T00:00:00Z',
        'generatorURL': 'http://prometheus-server:9090/graph?g0.expr=round%28%281+-+%28node_memory_MemAvailable_bytes%7Bjob%3D%22sentry%22%7D+%2F+%28node_memory_MemTotal_bytes%7Bjob%3D%22sentry%22%7D%29%29%29+%2A+100%29+%3E+55&g0.tab=1',
        'fingerprint': '09f94bd1aa7da54f'
    }],
    'groupLabels': {'alertname': '内存使用率'},
    'commonLabels': {
        'alertname': '内存使用率',
        'job': 'sentry',
        'severity': 'warning',
        'team': 'ops'
    },
    'commonAnnotations': {'summary': '内存使用率'},
    'externalURL': 'http://alertmanager-server:9093',
    'version': '4',
    'groupKey': '{}:{alertname=" 内存使用率 "}',
    'truncatedAlerts': 0
}

这么一看是不是就清晰了。

模板参考示例

上面看一下示例

官网模板参考:https://raw.githubusercontent.com/prometheus/alertmanager/master/template/default.tmpl

{{define "wechat.default.message"}}
{{- if gt (len .Alerts.Firing) 0 -}} // 判断报警列表的长度是否大于 0,大于 0 阐明有报警,否则没有
{{- range $index, $alert := .Alerts -}} // 遍历所有的告警列表,$index 是索引,$alert 是每一个报警元素
========== 异样告警 ==========
告警类型: {{$alert.Labels.alertname}}
告警级别: {{$alert.Labels.severity}}
告警详情: {{$alert.Annotations.description}};{{$alert.Annotations.summary}}
故障工夫: {{($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }} // 调整为北京工夫
{{- if gt (len $alert.Labels.instance) 0 }} // 判断下是否存在 instance
实例信息: {{$alert.Labels.instance}}
{{- end}}
============END============
{{- end}}
{{- end}}
{{- if gt (len .Alerts.Resolved) 0 -}} // 判断复原列表长度是否大于 0,大于 0 阐明有复原信息,否则没有
{{- range $index, $alert := .Alerts -}} // 遍历复原列表
========== 异样复原 ==========
告警类型: {{$alert.Labels.alertname}}
告警级别: {{$alert.Labels.severity}}
告警详情: {{$alert.Annotations.description}};{{$alert.Annotations.summary}}
故障工夫: {{($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }} #调整为北京工夫
复原工夫: {{($alert.EndsAt.Add 28800e9).Format "2006-01-02 15:04:05" }} #调整为北京工夫
{{- if gt (len $alert.Labels.instance) 0 }}
实例信息: {{$alert.Labels.instance}}
{{- end}}
============END============
{{- end}}
{{- end}}
{{- end}}

须要特地阐明下告警信息里的工夫是 utc 工夫,须要转换为北京工夫,也就是在 utc 工夫的根底上加 8 小时,也就是 28800 秒,也就是这里写的28800e9

基本上到这儿,咱们对如何编写告警信息模板有了一个很清晰的意识,上面看下如何配置到 alertmanager 里,其实之前的文章也说过了,上面再说下:

receivers:
- name: 'wechat'
  wechat_configs:
  - send_resolved: true
    message: '{{template"wechat.default.message".}}' 
    to_party: '接管告警的部门 ID'
    agent_id: '利用 ID'
    to_user: '用户 ID'
    api_secret: '部门 secret'

wechat.default.message就是下面 define 的模板名称

正文完
 0