Prometheus 和 Alertmanager实战配置

AlertManager下载

https://prometheus.io/download/

也可通过github下载

https://github.com/prometheus/alertmanager

解压

添加配置文件test.yml,配置收发邮件邮箱

参考配置:

global:
  smtp_smarthost: 'smtp.163.com:25'  #163服务器
  smtp_from: 'XXX@163.com'        #发邮件的邮箱
  smtp_auth_username: 'XXX@163.com'  #发邮件的邮箱用户名,也就是你的邮箱
  smtp_auth_password: 'XXX'        #发邮件的邮箱密码

route:
  group_by: ['alertname']

  repeat_interval: 1h

  receiver: live-monitoring

receivers:
- name: 'live-monitoring'
  email_configs:
  - to: 'czh1226@qq.com'        #收邮件的邮箱

更多配置参考alertmanager包中的simple.yml

添加报警规则

prometheus targets 监控报警参考配置(node_down.yml):

groups:
- name: example
  rules:
  - alert: InstanceDown
    expr: up == 0
    for: 1m
    labels:
      user: caizh
    annotations:
      summary: "Instance {{ $labels.instance }} down"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."

节点内存使用率监控报警参考配置(memory_over.yml)

groups:
- name: example
  rules:
  - alert: NodeMemoryUsage
    expr: (node_memory_MemTotal_bytes - (node_memory_MemFree_bytes+node_memory_Buffers_bytes+node_memory_Cached_bytes )) / node_memory_MemTotal_bytes * 100 > 80
    for: 1m
    labels:
      user: caizh
    annotations:
      summary: "{{$labels.instance}}: High Memory usage detected"
      description: "{{$labels.instance}}: Memory usage is above 80% (current value is:{{ $value }})"

当然,想要监控节点内存需要提前配置好node_exporter

修改prometheus配置文件prometheus.yml,开启报警功能,添加报警规则配置文件

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets: ["localhost:9093"]
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  - "node_down.yml"
  - "memory_over.yml"

配置完成!

启动alertmanager

./alertmanager --config.file test.yml

启动prometheus(默认会调用prometheus.yml)

./prometheus

http://localhost:9090/alerts

看配置与报警规则是否添加成功

成功则如下图:

image

告警信息生命周期的3中状态
1)inactive:表示当前报警信息即不是firing状态也不是pending状态
2)pending:表示在设置的阈值时间范围内被激活的
3)firing:表示超过设置的阈值时间被激活的

我的Prometheus Targets如下:

image

尝试kill一个测试是否可以用邮件报警

例如在slave1节点上:

InstanceDown会变成(1 active),并处在PENDING状态

image

1min后变FIRING状态

image

耐心等待几分钟,会收到报警邮件:

image

邮件可能会有延时,耐心等一会~

想测试内存使用率可以多开点占内存的服务,或者把报警规则中内存占用超过80%报警调小一些

Over~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Prometheus TSDB是什么? (Time Series Database) 简单的理解为.一个优化后用来...
    ilkkzm阅读 19,339评论 0 4
  • Prometheus是什么? Prometheus 是由 SoundCloud 开源监控告警解决方案,从 2012...
    tonyyan阅读 12,040评论 0 5
  • 主机数据收集 主机数据的采集是集群监控的基础;外部模块收集各个主机采集到的数据分析就能对整个集群完成监控和告警等功...
    梅_梅阅读 11,685评论 0 7
  • Prometheus官方文档 入门指导 Instructions and example code for a P...
    xufeibuaa阅读 9,989评论 1 6
  • 近来苦不堪言的阶段刚刚过去,闺中密友乖乖和晓梅约我去看电影。遂兴致勃勃的前往。乖乖开车带我们到万达看了一部电影。看...
    左岸渡己阅读 3,887评论 2 10

友情链接更多精彩内容