0
点赞
收藏
分享

微信扫一扫

Prometheus配置Mysql邮件报警

Brose 2022-12-30 阅读 89

安装Alertmanager

prometheus报警配置需要用到alertmanager组件,这个组件可以到prometheus官网上进行下载。

官网:https://prometheus.io/download/

1、下载Alertmanager

[root@localhost ~]# wget https://github.com/prometheus/alertmanager/releases/download/v0.20.0/alertmanager-0.20.0.linux-amd64.tar.gz

[root@localhost ~]# tar xf alertmanager-0.20.0.linux-amd64.tar.gz

[root@localhost ~]# mv alertmanager-0.20.0.linux-amd64 /usr/local/alertmanager

2、创建启动文件

[root@localhost ~]# vim /usr/lib/systemd/system/alertmanager.service

  1. [Unit]
  2. Description=alertmanager
  3. Documentation=https://github.com/prometheus/alertmanager
  4. After=network.target
  5. [Service]
  6. Type=simple
  7. User=root
  8. ExecStart=/usr/local/alertmanager/alertmanager --config.file=/usr/local/alertmanager/alertmanager.yml
  9. Restart=on-failure
  10. [Install]
  11. WantedBy=multi-user.target

3、配置alertmanager.yml文件

Alertmanager 安装目录下默认有 alertmanager.yml 配置文件,可以创建新的配置文件,在启动时指定即可。

[root@localhost ~]# cd /usr/local/alertmanager

[root@localhost alertmanager]# vim alertmanager.yml

  1. global:
  2. resolve_timeout: 5m
  3. # 邮件配置
  4. smtp_smarthost: 'smtp.exmail.qq.com:25'
  5. smtp_from: 'service@yangxingzhen.com'
  6. smtp_auth_username: 'service@yangxingzhen.com'
  7. smtp_auth_password: '123456'
  8. smtp_require_tls: false
  9. # route用来设置报警的分发策略
  10. route:
  11. # 采用哪个标签来作为分组依据
  12. group_by: ['alertname']
  13. # 组告警等待时间。也就是告警产生后等待10s,如果有同组告警一起发出
  14. group_wait: 10s
  15. # 两组告警的间隔时间
  16. group_interval: 10s
  17. # 重复告警的间隔时间,减少相同邮件的发送频率
  18. repeat_interval: 5m
  19. # 设置默认接收人
  20. receiver: 'default-receiver'
  21. routes: # 可以指定哪些组接手哪些消息
  22. - receiver: 'default-receiver'
  23. continue: true
  24. group_wait: 10s
  25. receivers:
  26. - name: 'default-receiver'
  27. email_configs:
  28. - to: 'xingzhen.yang@yangxingzhen.com'
  29. headers: { Subject: "[WARN] 报警邮件" }
  • smtp_smarthost:是用于发送邮件的邮箱的 SMTP 服务器地址+端口;
  • smtp_auth_password:是发送邮箱的授权码而不是登录密码;
  • smtp_require_tls:不设置的话默认为 true,当为 true 时会有 starttls 错误,为了简单这里设置为 false;
  • headers:为邮件标题;

4、配置Alertmanager报警规则

[root@localhost alertmanager]# mkdir -p /usr/local/prometheus/rules

[root@localhost alertmanager]# cd /usr/local/prometheus/rules

[root@localhost rules]# vim mysql.yml

  1. groups:
  2. - name: MySQL
  3. rules:
  4. - alert: MySQL Status # 告警名称
  5. expr: mysql_up == 0
  6. for: 5s # 满足告警条件持续时间多久后,才会发送告警
  7. annotations: # 解析项,详细解释告警信息
  8. summary: "{{$labels.instance}}: MySQL has stop !!!"
  9. value: "{{$value}}"
  10. alertname: "MySQL数据库停止运行"
  11. description: "检测MySQL数据库运行状态"
  12. message: 当前数据库实例{{$labels.instance}}已经停止运行,请及时处理
  13. - alert: MySQL Slave IO Thread Status # 告警名称
  14. expr: mysql_slave_status_slave_io_running == 0
  15. for: 5s # 满足告警条件持续时间多久后,才会发送告警
  16. annotations: # 解析项,详细解释告警信息
  17. summary: "{{$labels.instance}}: MySQL Slave IO Thread has stop !!!"
  18. value: "{{$value}}"
  19. alertname: "MySQL主从IO线程停止运行"
  20. description: "检测MySQL主从IO线程运行状态"
  21. message: 当前数据库实例{{$labels.instance}} IO线程已经停止运行,请及时处理
  22. - alert: MySQL Slave SQL Thread Status # 告警名称
  23. expr: mysql_slave_status_slave_sql_running == 0
  24. for: 5s # 满足告警条件持续时间多久后,才会发送告警
  25. annotations: # 解析项,详细解释告警信息
  26. summary: "{{$labels.instance}}: MySQL Slave SQL Thread has stop !!!"
  27. value: "{{$value}}"
  28. alertname: "MySQL主从SQL线程停止运行"
  29. description: "检测MySQL主从SQL线程运行状态"
  30. message: 当前数据库实例{{$labels.instance}} SQL线程已经停止运行,请及时处理
  31. - alert: MySQL Slave Delay Status # 告警名称
  32. expr: mysql_slave_status_sql_delay == 30
  33. for: 5s # 满足告警条件持续时间多久后,才会发送告警
  34. annotations: # 解析项,详细解释告警信息
  35. summary: "{{$labels.instance}}: MySQL Slave Delay has more than 30s !!!"
  36. value: "{{$value}}"
  37. alertname: "MySQL主从延时过大"
  38. description: "检测MySQL主从延时状态"
  39. message: 当前数据库实例{{$labels.instance}} 主从延时状态已经超过30s,请及时处理

在Prometheus.yml 中指定 mysql.yml 的路径

[root@localhost rules]# vim /usr/local/prometheus/prometheus.yml

  1. global:
  2. scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  3. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  4. # scrape_timeout is set to the global default (10s).

  5. # Alertmanager configuration
  6. alerting:
  7. alertmanagers:
  8. - static_configs:
  9. - targets: ['localhost:9093']
  10. # - localhost:9093

  11. # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
  12. rule_files:
  13. - 'rules/*.yml'
  14. # - "first_rules.yml"
  15. # - "second_rules.yml"

  16. # A scrape configuration containing exactly one endpoint to scrape:
  17. # Here it's Prometheus itself.
  18. scrape_configs:
  19. # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  20. - job_name: 'prometheus'
  21. # metrics_path defaults to '/metrics'
  22. # scheme defaults to 'http'.

  23. static_configs:
  24. - targets: ['localhost:9100']

5、重启 Prometheus 服务

[root@localhost rules]# systemctl restart prometheus

6、启动 Alertmanager

[root@localhost rules]# systemctl daemon-reload

[root@localhost rules]# systemctl start alertmanager

7、验证邮件报警

登陆prometheus的web页面,查看报警信息。

浏览器输入Prometheus_IP:9090,可以看到各个报警项的状态

Prometheus配置Mysql邮件报警_数据库

然后停止Mysql服务,然后再看效果。

[root@localhost rules]# systemctl stop mysqld

prometheus界面的alert可以看到告警状态。

  • 绿色表示正常。
  • 红色状态为PENDING表示alerts还没有发送至Alertmanager,因为rules里面配置了for: 5s。

5秒后状态由PENDING变为FIRING,此时Prometheus才将告警发给alertmanager,在Alertmanager中可以看到有一个alert。

Prometheus配置Mysql邮件报警_MySQL_02

Prometheus配置Mysql邮件报警_MySQL_03

接着邮箱会收到邮件:

Prometheus配置Mysql邮件报警_数据库_04

至此Mysql邮件告警完毕。

可参考示例:

​​https://github.com/prometheus/mysqld_exporter/blob/master/example.rules​​

举报

相关推荐

0 条评论