2、Alertmanager生成告警

Prometheus入门 / 2022-05-05

本文为朱政科《Prometheus云原生监控:运维与开发实战》学习笔记

一、Alertmanager部署

Prometheus高度模块化,它的指标采集、数据存储、告警生成是由不同组件完成,告警则是由官方制作的Alertmanager组件提供,其默认端口为9093。

1.1、下载

官网下载地址

wget https://github.com/prometheus/alertmanager/releases/download/v0.24.0/alertmanager-0.24.0.linux-amd64.tar.gz

1.2、解压安装

# 解压安装
sudo tar -zxf alertmanager-0.24.0.linux-amd64.tar.gz -C /usr/local/prometheus/
# 更改属主属组
sudo chown -R sanxi:sanxi /usr/local/prometheus/alertmanager-0.24.0.linux-amd64/
# 创建软链接
sudo ln -s /usr/local/prometheus/alertmanager-0.24.0.linux-amd64 /usr/local/alertmanager

1.3、修改配置文件

在Alertmanager安装目录下有一个alertmanager.yml文件,我们需要在里面添加一些配置来将告警信息推送到邮件。

参考如下配置:

global:
  # 邮箱解析超时时间
  resolve_timeout: 10s
  # smtp域名
  smtp_smarthost: 'smtp.qq.com:465'
  # 邮件发送服务器
  smtp_from: 'smtp.qq.com'
  # 邮箱账号
  smtp_auth_username: '88888888'
  # 邮箱密码
  smtp_auth_password: 'hhhhhhhhhhhh'
route:
 group_by: ['alertname']
 group_wait: 30s
 group_interval: 5m
 repeat_interval: 1h
 receiver: 'mail-receiver'
receivers:
 - name: 'mail-receiver'
   email_configs:
     # 邮件接收服务器
     - to: 'imap.qq.com'
inhibit_rules:
 - source_match:
     severity: 'critical'
   target_match:
     severity: 'warning'
   equal: ['alertname', 'dev', 'instance']

1.4、service文件

可参照prometheus,新增/usr/lib/systemd/system/alertmanager.service,如下所示:

[Unit]
Description=Alertmanager 0.24
Documentation=https://github.com/prometheus
After=network.target

[Service]
Type=simple
User=root
NotifyAccess=all
KillMode=process
WorkingDirectory=/usr/local/alertmanager
ExecStop=/bin/kill -TERM $MAINPID
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/alertmanager/alertmanager

[Install]
WantedBy=multi-user.target

二、Alertmanager规则

学习阶段先以手动编写规则为主,后续会有web图形配置界面。

2.1、配置alertmanager

在Alertmanager安装目录下新增alertmanager.yml,该配置将会通过邮箱发送告警信息。

下面以QQ邮箱为例,参考配置如下:

global:
  resolve_timeout: 5m
  smtp_smarthost: 'smtp.qq.com:465'
  smtp_from: 'smtp.qq.com'
  smtp_auth_username: '88888888'
  smtp_auth_password: 'hahahihi'
route:
 group_by: ['alertname']
 group_wait: 30s
 group_interval: 5m
 repeat_interval: 1h
 receiver: 'mail-receiver'
receivers:
 - name: 'mail-receiver'
   email_configs:
     - to: '666666@qq.com'
inhibit_rules:
 - source_match:
     severity: 'critical'
   target_match:
     severity: 'warning'
   equal: ['alertname', 'dev', 'instance']

2.2、启动Alertmanager

参照上述service写法后,操作如下可启动alertmanager,默认配置文件为alertmanager当前目录

sudo systemctl daemon-reload
sudo systemctl start alertmanager

2.3、alert_rules

部署并启动Alertmanager后,需要在prometheus添加alert_rules.yml编写相应的规则。

以下规则仅为示例,因该指标为CPU核心数

groups:
  - name: test-alert-rule
    rules:
      - alert: test alert message
        expr: count(count(node_cpu_seconds_total{instance="localhost:9100",job="prometheus"}) by (cpu)) == 12
        for: 1m
        labels:
          serveriry: critical

2.4、prometheus配置

接着需要在prometheus配置文件中添加alerting(前面启动的alertmanager)与rule_file(前面新增的alert_rules告警规则文件)模块。

配置参考如下:

# 全局配置
global:
  scrape_interval: 15s # 采集间隔,默认1分钟。
  evaluation_interval: 15s # 告警检测时间,即多久检查一次是否达到告警的条件,默认1分钟。
  # scrape_timeout: # 采集超时时间的间隔为10秒。

# 告警配置
alerting:
  # prometheus server发送请求给alertmanager之前也会触发一次打标签操作,子模块也可以单独配置alert_relabel_configs
  alertmanagers:
    # 静态配置alertmanager地址,也可以使用服务动态识别。
    - static_configs:
        - targets:  # 可以有多个地址
          - localhost:9093

# prometheus自定义的规则主要有recording rule和alerting rule两类
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
  - "alert_rules.yml"  # 此前添加的yml配置文件

scrape_configs:
  # prometheus会将该名称作为标签名追加到采集到每条时序数据中
  - job_name: "prometheus"

    # 指标默认路径为 '/metrics'
    # 默认采集数据所使用的网络协议为 'http'
    # 静态配置
    static_configs:
            - targets: ["localhost:9090", "localhost:9100"]

三、邮件告警扩展

上一小节中,介绍了邮件发送的关键配置。电子邮件有三种类型的收件方式:to(收件人)、cc(抄送)、bcc(密送),怎么搞?

其实alertmanager提供的不仅仅是普通邮件功能,还有SMTP相关功能。 下面例子中小写的to包含了3个邮箱地址,其中alertmanager会将大写的To默认为to配置,大写的CC会将其对应的值默认为抄送人,而没有指定第三个邮箱则默认为密送。

prometheus官方文档也是使用小写的to,请大家慎用大写的To和CC,不推荐使用BCC。

receivers:
 - name: 'mail-receiver'
   email_configs:
     - to: '666@qq.com','777@qq.com','555@qq.com'
     headers:
       To: '666@qq.com'
       CC: '777@qq.com'

四、构建钉钉告警

4.1、部署钉钉插件

需要先装好docker

在这个年头,告警通过邮件的方式估计没人看,所以通过社交APP方式推送消息可能更为适合。

以下为GitHub上开源的钉钉插件:地址

webhook获取方式:打开钉钉群–>群设置–>智能群助手–>添加机器人–>自定义(通过webhook接入自定义服务),然后跟着提示操作即可。

# 下载二进制文件
wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v2.0.0/prometheus-webhook-dingtalk-2.0.0.linux-amd64.tar.gz
# 解压
sudo tar -zxf prometheus-webhook-dingtalk-2.0.0.linux-amd64.tar.gz -C /usr/local/prometheus/alertmanager
# 更改属主属组
sudo chown -R sanxi:sanxi /usr/local/prometheus/alertmanager/prometheus-webhook-dingtalk-2.0.0.linux-amd64/
# 创建软链接
ln -s /usr/local/alertmanager/prometheus-webhook-dingtalk-2.0.0.linux-amd64 /usr/local/alertmanager/prometheus-webhook-dingtalk
# 编辑配置文件
cd /usr/local/prometheus/alertmanager/prometheus-webhook-dingtalk
cp config.example.yml config.yml
vim config.yml

配置参考如下

targets:
  webhook1:
    url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx
    # secret for signature
    secret: xxxxxxxxxx

service文件

[Unit]
Description=prometheus-webhook-dingtalk
Documentation=https://github.com/timonwong/prometheus-webhook-dingtalk
After=network-online.target

[Service]
Restart=on-failure
ExecStart=/usr/local/prometheus/alertmanager/prometheus-webhook-dingtalk/prometheus-webhook-dingtalk --config.file=/usr/local/prometheus/alertmanager/prometheus-webhook-dingtalk/config.yml

[Install]
WantedBy=multi-user.target

4.2、修改配置文件

一定要注意,yml格式是强要求,对齐极其严格,稍有错误会导致alertmanager无法运行。

需要修改前面添加的alertmanager.yml文件,改动内容是将邮件模块改为webhook,如下所示:

global:
  resolve_timeout: 5m  #
  smtp_smarthost: 'smtp.qq.com:465'
  smtp_from: 'smtp.qq.com'
  smtp_auth_username: '88888888'
  smtp_auth_password: 'hhhhhhhh'
route:
  group_by: [ 'alertname' ]
  group_wait: 30s
  group_interval: 1m
  repeat_interval: 1h
  receiver: 'dingtalk'
receivers:
#  - name: 'mail-receiver'
#    email_configs:
#      - to: 'imap.qq.com'
- name: 'dingtalk'
  webhook_configs:
  - url: 'http://localhost:8060/dingtalk/webhook1/send'
    send_resolved: true

inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: [ 'alertname', 'dev', 'instance' ]

4.3、钉钉告警验证

上面工作准备好后,先访问告警模块的web页面,即lhttp://ocalhost:9090/alerts,先确认模块是否正常。

  • INACTIVE:活跃中,即表示正常无告警产生。
  • PENDING:待触发,表示已经达到预设的阈值,但没达到预设的时间。
  • FIRING:表示达到预设的阈值并超过预设的时间触发告警

因为设置了监测到CPU核心数为12,本人电脑核心数也是12,所以一定会触发告警。

image-20220420144622204

来看看钉钉的告警消息

image-20220420164917425

世间微尘里 独爱茶酒中