⭐⭐⭐ Spring Boot 项目实战 ⭐⭐⭐ Spring Cloud 项目实战
《Dubbo 实现原理与源码解析 —— 精品合集》 《Netty 实现原理与源码解析 —— 精品合集》
《Spring 实现原理与源码解析 —— 精品合集》 《MyBatis 实现原理与源码解析 —— 精品合集》
《Spring MVC 实现原理与源码解析 —— 精品合集》 《数据库实体设计合集》
《Spring Boot 实现原理与源码解析 —— 精品合集》 《Java 面试题 + Java 学习指南》

摘要: 原创出处 jianshu.com/p/128e4751404e 「pigness」欢迎转载,保留摘要,谢谢!


🙂🙂🙂关注**微信公众号:【芋道源码】**有福利:

  1. RocketMQ / MyCAT / Sharding-JDBC 所有源码分析文章列表
  2. RocketMQ / MyCAT / Sharding-JDBC 中文注释源码 GitHub 地址
  3. 您对于源码的疑问每条留言将得到认真回复。甚至不知道如何读源码也可以请教噢
  4. 新的源码解析文章实时收到通知。每周更新一篇左右
  5. 认真的源码交流微信群。

AlertManager用于接收Prometheus发送的告警并对于告警进行一系列的处理后发送给指定的用户。系统的整体设计图如下面所示,并且支持HA高可用部署。

alertmanager.png

1. 下载可编译版本的 AlertManager

git clone https://github.com/prometheus/alertmanager.git

2. 在 notify.go 中添加 sms 新标签

numNotifications.WithLabelValues("sms") //新增标签

3. 在 config.go 中添加新配置项

SMSConfigs       []*SMSConfig       `yaml:"sms_configs,omitempty" json:"sms_configs,omitempty"`

4. 在 notifiers.go 中添加 config 的详细定义

// SMSConfig configures notifications via sms.
type SMSConfig struct {
NotifierConfig `yaml:",inline" json:",inline"`

// SMS info to notify.
To string `yaml:"to,omitempty" json:"to,omitempty"`
From string `yaml:"from,omitempty" json:"from,omitempty"`
Hello string `yaml:"hello,omitempty" json:"hello,omitempty"`
}

具体字段可根据实际情况修改

5. 参照 webhook 进行自定义的 notify 逻辑编写 自定义的notify路由需要满足该Notifier接口,实现Notify方法。 比如下面是webhook的实现,首先定义一个管理webhook的结构体Webhook,包含基本的配置和模板信息,WebhookMessage定义了发送webhook的信息

// Webhook implements a Notifier for generic webhooks.
type Webhook struct {
conf *config.WebhookConfig
tmpl *template.Template
logger log.Logger
}

// NewWebhook returns a new Webhook.
func NewWebhook(conf *config.WebhookConfig, t *template.Template, l log.Logger) *Webhook {
return &Webhook{conf: conf, tmpl: t, logger: l}
}

// WebhookMessage defines the JSON object send to webhook endpoints.
type WebhookMessage struct {
*template.Data

// The protocol version.
Version string `json:"version"`
GroupKey string `json:"groupKey"`
}

6. 修改 alertmanager.yml 相关配置进行告警调用

# 定义路由树信息
route:
group_by: ['alertname'] # 报警分组依据
group_wait: 10s # 最初即第一次等待多久时间发送一组警报的通知
group_interval: 10s # 在发送新警报前的等待时间
repeat_interval: 1m # 发送重复警报的周期 对于email配置中,此项不可以设置过低,否则将会由于邮件发送太多频繁,被smtp服务器拒绝
receiver: 'sms' # 发送警报的接收者的名称,以下receivers name的名称

# 定义警报接收者信息
receivers:
- name: 'sms'
sms_configs:
- to: '12345678901' # 接收警报的手机号
from: '12345678901' #发送警报的手机号

文章目录