博客 > 术业专攻> 服务类相关> Prometheus学习笔记–Prometheus邮件报警配置 2019年08月29日 11:24:08
$ tar xf alertmanager-0.15.2.linux-amd64.tar.gz -C /usr/local/
$ mv alertmanager-0.15.2.linux-amd64/ alertmanager
$ vim /usr/lib/systemd/system/alertmanager.service
添加如下内容:
[Unit]
Description=alertmanager
Documentation=https://github.com/prometheus/alertmanager
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/alertmanager/alertmanager --config.file=/usr/local/alertmanager/alert-test.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
Alertmanager 安装目录下默认有 alertmanager.yml 配置文件,可以创建新的配置文件,在启动时指定即可。
$ cd /usr/local/alertmanager
$ vim alert-test.yml
global:
smtp_smarthost: 'smtp.163.com:25'
smtp_from: 'Linuxlql@163.com'
smtp_auth_username: 'Linuxlql@163.com'
smtp_auth_password: '123546' # 这里是邮箱的授权密码,不是登录密码
smtp_require_tls: false
templates:
- '/alertmanager/template/*.tmpl'
route:
group_by: ['alertname', 'cluster', 'service']
group_wait: 30s
group_interval: 5m
repeat_interval: 10m
receiver: default-receiver
receivers:
- name: 'default-receiver'
email_configs:
- to: 'liqilong@edspay.com'
html: ''
headers: { Subject: "[WARN] 报警邮件 test" }
邮箱一开始使用的是公司的邮箱,结果在后边验证的时候,总是会报错level=error ts=2019-01-26T06:21:59.062483579Z caller=notify.go:332 component=dispatcher msg="Error on notify" err="*smtp.plainAuth failed: unencrypted connection"
,也在这里看了一些人踩坑的报告,试验了25、465、587端口,发现均无效果,最后改成163邮箱,直接就生效了。
配置 rule.yml。
$ cd /usr/local/prometheus
$ vim rule.yml
groups:
- name: alert-rules.yml
rules:
- alert: InstanceStatus # alert 名字
expr: up{job="linux-node01"} == 0 # 判断条件
for: 10s # 条件保持 10s 才会发出 alter
labels: # 设置 alert 的标签
severity: "critical"
annotations: # alert 的其他标签,但不用于标识 alert
description: 服务器 已当机超过 20s
summary: 服务器 运行状态
在 prometheus.yml 中指定 rule.yml 的路径
$ cat prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- localhost:9093 # 这里修改为 localhost
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
- "/usr/local/prometheus/rule.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090','localhost:9100']
- job_name: '111.4'
scrape_interval: 5s
static_configs:
- targets: ['192.168.111.4:9100']
重启 Prometheus 服务:
$ chown -R prometheus.prometheus /usr/local/prometheus/rule.yml
$ systemctl restart prometheus
注意:文件后缀为 tmpl
$ mkdir -pv /alertmanager/template/
$ vim /alertmanager/template/alert.tmpl
报警名 开始时间
$ chown -R prometheus.prometheus /usr/local/alertmanager
$ systemctl daemon-reload
$ systemctl start alertmanager.service
$ systemctl status alertmanager.service
$ ss -tnl|grep 9093
此时到管理界面可以看到如下信息:
然后停止111.4节点上的 node_exporter 服务,然后再看效果。
$ systemctl stop node_exporter.service
接着邮箱应该会收到邮件:
© 2018 www.qingketang.net 鄂ICP备18027844号-1
武汉快勤科技有限公司 13554402156 武汉市东湖新技术开发区关山二路特一号国际企业中心6幢4层7号
扫码关注,全站教程免费播放
订单金额:
支付金额:
支付方式: