linux(centos8):prometheus使用alertmanager发送报警邮件(prometheus 2.18.1/alertmanager 0.20.0)

一,alertmanager的用途

1,Alertmanager的作用:
Alertmanager是一个独立的报警模块,
它接收Prometheus等客户端发来的警报,并通过分组、删除重复等处理,
通过路由把警报发送给正确的接收器;
报警方式可以按照不同的规则发送给不同的模块负责人,
Alertmanager支持Email, Slack,等报警方式, 
也支持通过webhook接入钉钉等IM工具
 
2,我们这里配置的例子:
当一台运行node_exporter的服务器发生故障后,
alertmanager通过prometheus得到监控信息,
会发送邮件给指定的邮箱实现报警
 

说明:刘宏缔的架构森林是一个专注架构的博客,

网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/22/centos8linuxprometheus-shi-yong-alertmanager-fa-song-bao-jing-you-jian/

         对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,下载alertmanager

1,官网下载地址
https://prometheus.io/download/

 

2,下载安装包
[root@blog prometheus]# pwd
/usr/local/source/prometheus
[root@blog prometheus]# wget https://github.com/prometheus/alertmanager/releases/download/v0.20.0/alertmanager-0.20.0.linux-amd64.tar.gz

 

三,安装alertmanager

1,解压
[root@blog prometheus]# tar -zxvf alertmanager-0.20.0.linux-amd64.tar.gz

 

2,移动到安装目录
[root@blog prometheus]# mv alertmanager-0.20.0.linux-amd64 /usr/local/soft/

 

3,查看安装效果:
   查看alertmanager的版本
[root@blog prometheus]# /usr/local/soft/alertmanager-0.20.0.linux-amd64/alertmanager --version
alertmanager, version 0.20.0 (branch: HEAD, revision: f74be0400a6243d10bb53812d6fa408ad71ff32d)
  build user:       root@00c3106655f8
  build date:       20191211-14:13:14
  go version:       go1.13.5

 

四,配置alertmanager

1,编辑配置文件
[root@blog alertmanager-0.20.0.linux-amd64]# pwd
/usr/local/soft/alertmanager-0.20.0.linux-amd64
[root@blog alertmanager-0.20.0.linux-amd64]# vi alertmanager.yml
内容:
global:
  resolve_timeout: 5m
  smtp_smarthost: 'smtp.163.com:465'
  smtp_from: 'chinalinuxzend@163.com'
  smtp_auth_username: 'chinalinuxzend@163.com'
  smtp_auth_password: 'yourauthcode'
  smtp_require_tls: false
route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  receiver: 'mail'
receivers:
- name: 'mail'
  email_configs:
    - to: '371125307@qq.com'

说明:

如果是在阿里云上,需要配置465端口发送邮件,因为25端口被封禁了,参考这一篇:

https://blog.imgtouch.com/index.php/2023/05/22/centos8linux-a-li-yun-ecs-pei-zhi-smtps-fa-you-jian-jie-jue-bu-neng-tong-guo-25-duan-kou-fa-you-jian/

smtp_auth_password:它不是邮箱的登录密码,而是邮件服务商提供的一个授权码

 

2,检查配置是否正确?
[root@blog alertmanager-0.20.0.linux-amd64]# ./amtool check-config alertmanager.yml
如果看到Checking 'alertmanager.yml’  SUCCESS
表示配置正确
 

五,使systemd支持alertmanager服务

 
1,编辑service文件
[root@blog alertmanager-0.20.0.linux-amd64]# vi /usr/lib/systemd/system/alertmanager.service 
内容:
[Unit]
Description=Alertmanager
After=network.target
 
[Service]
Type=simple
ExecStart=/usr/local/soft/alertmanager-0.20.0.linux-amd64/alertmanager --config.file=/usr/local/soft/alertmanager-0.20.0.linux-amd64/alertmanager.yml
Restart=on-failure
 
[Install]
WantedBy=multi-user.target

 

2,重新加载service文件
[root@blog alertmanager-0.20.0.linux-amd64]# systemctl daemon-reload

 

3,启动alertmanager服务
[root@blog alertmanager-0.20.0.linux-amd64]# systemctl start alertmanager.service

 

4,检查服务启动是否成功
[root@blog alertmanager-0.20.0.linux-amd64]# systemctl status alertmanager.service

 

5,检查alertmanager是否已在守护端口?
[root@blog alertmanager-0.20.0.linux-amd64]# ss -lntp | grep alertmanager
LISTEN   0         1024                      *:9093                   *:*        users:(("alertmanager",pid=29807,fd=6))
LISTEN   0         1024                      *:9094                   *:*        users:(("alertmanager",pid=29807,fd=3))

 

6,通过url访问9093端口:
http://121.122.123.47:9093/

 

六,配置prometheus使用alertmanager报警

1,生成报警的规则文件?
[root@blog ~]# mkdir -p /data/prometheus/rules
[root@blog ~]# cd /data/prometheus/rules/
[root@blog rules]# vi rule.yml
内容:
groups:
- name: alert-rules.yml
  rules:
  - alert: InstanceStatus # alert 名字
    expr: up{job="118node"} == 0 # 判断条件
    for: 10s # 条件保持 10s 才会发出 alter
    labels: # 设置 alert 的标签
      severity: "critical"
    annotations:  # alert 的其他标签,但不用于标识 alert
      description: 服务器  已当机超过 20s
      summary: 服务器  运行状态  
 
2,在prometheus的配置文件中,指定rule和alert
[root@blog ~]# vi /usr/local/soft/prometheus-2.18.1.linux-amd64/prometheus.yml
内容:
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - localhost:9093
rule_files:
  - "/data/prometheus/rules/rule.yml"

说明:指定了报警服务的地址/规则文件的地址

 

七,重启prometheus和alertmanager,使配置生效

重启prometheus
[root@blog ~]# systemctl restart prometheus.service

 

重启alertmanager
[root@blog ~]# systemctl restart alertmanager.service

 

八,测试发送邮件效果:

1,从promethus的管理界面查看已添加的alert
http://121.122.123.47:9090/alerts
如图:
 
2,在118这个node上,关掉node_exporter
[root@cacheServer ~]# ps auxfww | grep node
root      6440  0.3  0.0 718788 16520 ?        Sl   Jun05  14:13 /usr/local/soft/node_exporter-1.0.0.linux-amd64/node_exporter
[root@cacheServer ~]# kill 6440
[root@cacheServer ~]# ps auxfww | grep node

 

3,返回到管理界面,查看:
状态变成了firing,同时也已经收到了邮件
如图:
 
邮件内容:
 
 

九,查看prometheus的版本

[root@blog ~]# /usr/local/soft/prometheus-2.18.1.linux-amd64/prometheus --version
prometheus, version 2.18.1 (branch: HEAD, revision: ecee9c8abfd118f139014cb1b174b08db3f342cf)
  build user:       root@2117a9e64a7e
  build date:       20200507-16:51:47
  go version:       go1.14.2 

 

十,查看linux的版本

[root@blog ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core) 

 

posted @ 2020-06-08 13:53  刘宏缔的架构森林  阅读(799)  评论(0编辑  收藏  举报