AlertManager告警

Prometheus Server配合Alert Manager能够实现满足一定PromQL条件时告警的功能。Prometheus Server负责配置和定时计算PromQL,如果满足条件则发送信息给Alert Manager,而Alert Manager负责具体实现发送邮件、Slack消息等。

Alert Manager是需要单独部署的服务,我们可以在Prometheus官网找到其安装包。服务启动后,我们可以访问http://localhost:9093,查看其管理界面。

Prometheus告警规则配置

我们这里单独创建一个yml配置文件,并在prometheus.yml中引用。例子如下:

my_rules.yml

groups:
- name: cpuAlertRules
  rules:
  - alert: cpuUsageAlert
    expr: sum(avg without (cpu)(irate(node_cpu_seconds_total{mode!='idle'}[5m]))) by (instance) > 0.85
    for: 1m
    labels:
      severity: page
    annotations:
      summary: "Instance {{ $labels.instance }} CPU usgae high"
      description: "{{ $labels.instance }} CPU usage above 85% (current value: {{ $value }})"
  - alert: memUsageAlert
    expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes > 0.85
    for: 1m
    labels:
      severity: page
    annotations:
      summary: "Instance {{ $labels.instance }} MEM usgae high"
      description: "{{ $labels.instance }} MEM usage above 85% (current value: {{ $value }})"

我们创建了两条规则,第一条检测CPU占用率是否超过85%,第二条检测内存占用率是否超过85%。其中:

  • expr:字段是具体检测的PromQL表达式
  • labels:用户自定义标签,可以附加自定义数据
  • for:评估等待时间,告警持续指定时间后发送
  • summarydescription:两个模板字符串,其中包含具体的告警文本,相当于标题和内容

当告警规则被触发时,我们可以在Prometheus的页面上查看到:

配置AlertManager

AlertManager通过route路由和receiver接收器处理告警信息。路由定义了一系列规则,配置告警信息发送给哪一个接收器;而接收器具体处理告警信息,如邮件接收器需要配置邮件服务器的SMTP用户名、密码,告警信息发送的邮箱等。

首先我们需要配置Prometheus Server,在配置文件prometheus.yml中配置Alert Manager的服务地址:

prometheus.yml

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

Alert Manager中,我们配置了一个邮件Receiver。例子如下:

alertmanager.yml

global:
  smtp_smarthost: <SMTP服务器地址和端口>
  smtp_from: <发件人邮箱>
  smtp_auth_username: <用户名>
  smtp_auth_identity: <用户名>
  smtp_auth_password: <密码>

route:
  group_by: ['alertname']
  receiver: 'default-receiver'

receivers:
  - name: default-receiver
    email_configs:
      - to: <接收人邮箱>
        send_resolved: true

当告警规则触发后,我们就会收到告警邮件:

作者:Gacfox
版权声明:本网站为非盈利性质,文章如非特殊说明均为原创,版权遵循知识共享协议CC BY-NC-ND 4.0进行授权,转载必须署名,禁止用于商业目的或演绎修改后转载。