Prometheus配合Alertmanager报警系统

Promethous+Alertmanager+Grafana

监控技术栈如下:
Prometheus(最新版):基于TSDB的微服务指标采集&报警;
Alertmanager:报警服务;
Grafana(>=5.x):监控报表展示。

一、软件部署

1.1 Prometheus安装
# wget https://github.com/prometheus/prometheus/releases/download/v2.5.0/prometheus-2.5.0.linux-amd64.tar.gz
# tar zxvf prometheus-2.5.0.linux-amd64.tar.gz -C /usr/local/
# cd /usr/local/
# ln -s prometheus-2.5.0.linux-amd64 prometheus
# chown work:work prometheus* -R
# cd prometheus
# ls
# console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool
1.2 Alertmanager安装
# wget https://github.com/prometheus/alertmanager/releases/download/v0.15.3/alertmanager-0.15.3.linux-amd64.tar.gz
# tar zxvf alertmanager-0.15.3.linux-amd64.tar.gz 
# ln -s alertmanager-0.15.3.linux-amd64 alertmanager
# chown work:work alertmanager* -R
# cd alertmanager
# ls
# alertmanager alertmanager.yml amtool LICENSE NOTICE
1.3 Grafana安装
# wget wget https://dl.grafana.com/oss/release/grafana-5.3.4-1.x86_64.rpm 
# rpm -Uvh grafana-5.3.4-1.x86_64.rpm 
# systemctl restart grafana.service

二、服务配置

2.1 Prometheus配置

指定服务监听alertmanager端口及报警规则目录

vim /usr/local/prometheus/prometheus.yml

#配置alertmanager信息
alerting:
  alertmanagers:
  - static_configs:
    - targets: ['localhost:9093']
#配置告警规则目录
rule_files:
  - /usr/local/prometheus/rules/*.rules
2.2 Rules策略配置

创建一个服务down的报警规则

vim /usr/local/prometheus/rules/service_down.rules

groups:
- name: ServiceStatus #规则组名称   
  rules:
  - alert: ServiceStatusAlert  #单个规则的名称
    expr: up == 0   #匹配规则, up==0
    for: 10s        #持续时间
    labels:         #标签
      project: zhidaoAPP    #自定义lables
    annotations:            #告警正文
      summary: "Instance {{ $labels.instance }} down"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."
2.3 Alertmanager配置
vim /usr/local/alertmanager/alertmanager.yml

#全局配置,比如配置发件人
global:
  resolve_timeout: 5m    #处理超时时间,默认为5min
  smtp_smarthost: 'smtp.163.com:25'  # 邮箱smtp服务器代理
  smtp_from: '[email protected]' # 发送邮箱名称
  smtp_auth_username: '[email protected]' # 邮箱名称
  smtp_auth_password: '12345678xxOO'              # 邮箱密码或授权码

# 定义模板信息,可以自定义html模板,发邮件的时候用自己定义的模板内容发
templates:
  - 'template/*.tmpl'

# 定义路由树信息,这个路由可以接收到所有的告警,还可以继续配置路由,比如project: zhidaoAPP(prometheus 告警规则中自定义的lable)发给谁,project: baoxian的发给谁
route:
  group_by: ['alertname'] # 报警分组依据
  group_wait: 10s         # 最初即第一次等待多久时间发送一组警报的通知
  group_interval: 60s     # 在发送新警报前的等待时间
  repeat_interval: 1h     # 发送重复警报的周期 对于email配置中,此项不可以设置过低,否则将会由于邮件发送太多频繁,被smtp服务器拒绝
  receiver: 'email'       # 发送警报的接收者的名称,以下receivers name的名称

# 定义警报接收者信息
receivers:
  - name: 'email'  # 路由中对应的receiver名称
    email_configs: # 邮箱配置
    - to: '[email protected]'   # 接收警报的email配置
      #html: '{{ template "test.html" . }}'  # 设定邮箱的内容模板

三、服务启动

3.1 Prometheus启动

/usr/local/prometheus/prometheus --config.file=prometheus.yml --web.enable-lifecycle --web.external-url=http://127.0.0.1:9090 --storage.tsdb.path=/data1/prometheus/data &

3.2 Alertmanager启动

/usr/local/alermanager/alertmanager &

四、报警验证

4.1 Prometheus

Prometheus配合Alertmanager报警系统

4.2 Rules

Prometheus配合Alertmanager报警系统

4.3 Alerts

Prometheus配合Alertmanager报警系统

4.4 Mails

Prometheus配合Alertmanager报警系统

五、参考

https://yunlzheng.gitbook.io/prometheus-book/parti-prometheus-ji-chu/alert
https://prometheus.io/docs/prometheus/latest/getting_started/

猜你喜欢

转载自blog.51cto.com/jerrymin/2333824