此处默认已安装Prometheus服务,服务地址:192.168.56.200

一、设置企业微信

1.1、企业微信注册(已有企业微信账号请跳过)

企业微信注册地址:https://work.weixin.qq.com/

3.Prometheus 企业微信报警 - 图1

按照要求填写相应信息,注册企业微信

1.2 、创建自建应用

企业微信注册完成后,登录进去,点击上方导航条中的【应用管理】按钮,切换到应用管理页面。

然后选择【创建应用】,创建用于接收Alertmanager告警信息的自建应用

3.Prometheus 企业微信报警 - 图2

填写应用名称等信息,创建应用。创建应用后,记录应用的 AgentId 和 Secret 以备后用

3.Prometheus 企业微信报警 - 图3

注意注意:上图的可见范围一定要加上你要通知的企业微信成员,不然也收不到通知

然后点击上方导航条中的【我的企业】按钮,在页面最下方查看企业ID 并记录,以备后用。

3.Prometheus 企业微信报警 - 图4

3.Prometheus 企业微信报警 - 图5

二、安装Alertmanager

已安装过的忽略

此处采用源码编译的方式安装。首先下载alertmanager的软件包,下载地址:https://github.com/prometheus/alertmanager/releases/download/v0.19.0/alertmanager-0.19.0.linux-amd64.tar.gz

下载完成后,将下载中软件包上传至Prometheus服务所在的机器(192.168.56.200)的 /usr/local 目录下

3.Prometheus 企业微信报警 - 图6

解压alertmanager软件包:

  1. # tar -zvxf alertmanager-0.19.0.linux-amd64.tar.gz
  2. # mv alertmanager-0.19.0.linux-amd64/ alertmanager

进入解压后的alertmanager文件夹,修改alertmanager.yml文件,配置报警信息,alertmanager.yml 内容如下:

  1. global:
  2. resolve_timeout: 5m
  3. templates: #告警模板
  4. - './template/test.tmpl'
  5. route: # 设置报警分发策略
  6. group_by: ['alertname'] # 分组标签
  7. group_wait: 10s # 告警等待时间。告警产生后等待10s,如果有同组告警一起发出
  8. group_interval: 10s # 两组告警的间隔时间
  9. repeat_interval: 1m # 重复告警的间隔时间,减少相同右键的发送频率 此处为测试设置为1分钟
  10. receiver: 'wechat' # 默认接收者
  11. receivers:
  12. - name: 'wechat'
  13. wechat_configs:
  14. - send_resolved: true
  15. agent_id: '1000002' # 自建应用的agentId
  16. to_user: '*****' # 接收告警消息的人员Id
  17. api_secret: '******' # 自建应用的secret
  18. corp_id: '******' # 企业ID
  19. #inhibit_rules:
  20. # - source_match:
  21. # severity: 'critical'
  22. # target_match:
  23. # severity: 'warning'
  24. # equal: ['alertname', 'dev', 'instance']

创建告警模板

进入Alertmanager安装文件夹,创建告警模板文件

  1. # cd /usr/local/alertmanager
  2. # mkdir template
  3. # cd template/
  4. # vim test.tmpl

将以下内容写入文件当中

3.Prometheus 企业微信报警 - 图7;)

  1. {{ define "wechat.default.message" }}
  2. {{ range .Alerts }}
  3. ========监控报警==========
  4. 告警状态:{{ .Status }}
  5. 告警级别:{{ .Labels.severity }}
  6. 告警类型:{{ .Labels.alertname }}
  7. 告警应用:{{ .Annotations.summary }}
  8. 告警主机:{{ .Labels.instance }}
  9. 告警详情:{{ .Annotations.description }}
  10. 触发阀值:{{ .Annotations.value }}
  11. 告警时间:{{ .StartsAt.Format "2006-01-02 15:04:05" }} ========end============= {{ end }} {{ end }}

3.Prometheus 企业微信报警 - 图8;)

检查alertmanager.yml 配置是否正确

  1. # cd /usr/local/alertmanager
  2. # ./amtool check-config alertmanager.yml

3.Prometheus 企业微信报警 - 图9

配置正确,模板文件也已经识别

启动alertmanager

  1. # ./alertmanager

3.Prometheus 企业微信报警 - 图10

可以看到alertmanager服务已经起来,服务所在的端口为9093

浏览器访问: http://192.168.56.200:9093 (IP:9093)

3.Prometheus 企业微信报警 - 图11

alertmanager成功启动。

三、配置Prometheus

Ctrl+C 结束掉alertmanager服务进程,进入Prometheus的安装目录下修改Prometheus配置。

  1. # cd /usr/local/prometheus
  2. # vim prometheus.yml

修改Prometheus.yml文件中的 alerting 配置项及rule_files配置项

  1. alerting:
  2. alertmanagers:
  3. - static_configs:
  4. - targets: ['localhost:9093']

rule_files: #配置告警规则

  • “rule.yml”

修改完成后保存退出

以下是Prometheus.yml 文件全部内容:

  1. # my global config
  2. global:
  3. scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  4. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  5. # scrape_timeout is set to the global default (10s).
  6. # Alertmanager configuration
  7. alerting:
  8. alertmanagers:
  9. - static_configs:
  10. - targets: ['localhost:9093']
  11. # - alertmanager:9093
  12. # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
  13. rule_files:
  14. - "rule.yml"
  15. # - "first_rules.yml"
  16. # - "second_rules.yml"
  17. # A scrape configuration containing exactly one endpoint to scrape:
  18. # Here it's Prometheus itself.
  19. scrape_configs:
  20. # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  21. - job_name: 'prometheus'
  22. # metrics_path defaults to '/metrics'
  23. # scheme defaults to 'http'.
  24. static_configs:
  25. - targets: ['localhost:9090']
  26. - job_name: 'Linux'
  27. static_configs:
  28. - targets: ['192.168.56.201:9100']

创建并编写告警规则文件rule.yml

  1. # vim rule.yml

将以下内容写入文件当中,(此处用于测试,设置为当内存占用高于10%时,就会告警)

  1. groups:
  2. - name: mem-rule
  3. rules:
  4. - alert: "内存报警"
  5. expr: (node_memory_MemTotal_bytes - (node_memory_MemFree_bytes+node_memory_Buffers_bytes+node_memory_Cached_bytes )) / node_memory_MemTotal_bytes * 100 > 10
  6. for: 30s
  7. labels:
  8. severity: warning
  9. annotations:
  10. summary: "服务名:{{$labels.alertname}} 内存报警"
  11. description: "{{ $labels.alertname }} 内存资源利用率大于 10%"
  12. value: "{{ $value }}"

保存退出

四、告警检测

重启Prometheus服务,使配置的告警规则生效

  1. # systemctl restart prometheus

进入alertmanager的安装文件夹,启动alertmanager

  1. # cd /usr/local/alertmanager
  2. # ./alertmanager

稍等片刻,登录企业微信,可以看到已经接收到告警信息

3.Prometheus 企业微信报警 - 图12

浏览器访问 http://192.168.56.200:9093/#/alerts ,也能看到告警信息

3.Prometheus 企业微信报警 - 图13

五、配置alertmanager服务开机自启

已配置的忽略

Ctrl+C 结束掉 alertmanager 服务进程,创建 alertmanager服务,让 alertmanager 以服务的方式,开机自启。

添加系统服务

  1. # vim /etc/systemd/system/alertmanager.service

将以下内容写入文件中

  1. [Unit]
  2. Description=alertmanager
  3. After=network.target
  4. [Service]
  5. WorkingDirectory=/usr/local/alertmanager
  6. ExecStart=/usr/local/alertmanager/alertmanager --config.file=alertmanager.yml --log.level=debug --log.format=json
  7. Restart=on-failure
  8. [Install]
  9. WantedBy=multi-user.target

保存退出

启动服务,设置开机自启

  1. # systemctl daemon-reload
  2. # systemctl enable alertmanager
  3. # systemctl start alertmanager

至此Prometheus+alertmanage配置企业微信报警完成。