---
title: "Prometheus接入企业微信报警"
author: "lvjun106"
date: "2022-01-18T14:15:38Z"
tags: []
categories: ["开源软件"]
slug: "prometheus"
draft: false
lastmod: "2022-01-18T14:19:42Z"
id: 2524222
url: "https://lvjun106.iteye.com/blog/2524222"
---

## 环境

Prometheus：paas平台搭建；  
grafana： paas平台搭建；  
alertmanager：虚拟机docker环境搭建；  
企业微信管理平台：[https://work.weixin.qq.com/wework\_admin/frame#contacts](https://work.weixin.qq.com/wework_admin/frame#contacts) 企业微信或者微信扫码登陆； [https://work.weixin.qq.com/wework\_admin/loginpage\_wx](https://work.weixin.qq.com/wework_admin/loginpage_wx)  
其他java进程和虚拟机环境：被监控对象。

## []()工具介绍

Prometheus：Prometheus（普罗米修斯）是一套开源的监控&报警&时间序列数据库的组合，现在最常见的Kubernetes容器管理系统中，通常会搭配Prometheus进行监控。Prometheus基本原理是通过HTTP协议周期性抓取被监控组件的状态，这样做的好处是任意组件只要提供HTTP接口就可以接入监控系统，不需要任何SDK或者其他的集成过程。这样做非常适合虚拟化环境比如VM或者Docker，Prometheus应该是为数不多的适合Docker、Mesos、Kubernetes环境的监控系统之一。  
grafana：grafana是一个开源的可视化监控及数据展示分析工具。它有快速灵活的多种图表，支持多达54种数据源如Prometheus，mysql，es等；  
alertmanager：是独立于Prometheus的一个组件，可以支持Prometheus的查询语句，提供十分灵活的报警方式。  
企业微信管理平台：企业微信支持多种接口，其中Prometheus的接口可以直接通过模板对接，非常方便不需要开发进行二次开发；

## []()大致步骤

1.利用JMX exporter和javagent，在Java进程内启动一个小型的Http server  
2.配置Prometheus抓取那个Http server提供的metrics。  
3.配置Prometheus的告警触发规则，例如：  
heap使用超过最大上限的50%、80%、90%  
instance down机时间超过30秒、1分钟、5分钟  
old gc时间在最近5分钟里超过50%、80%  
4.配置Grafana连接Prometheus，配置Dashboard  
5.配置Alertmanager的告警通知规则

## []()配置过程

#### []()一、基础配置

在paas平台上搭建好Prometheus及grafana，直接使用准备好的镜像启动，启动完毕后更改配置文件，数据展示到grafana

#### []()二、注册一个企业微信

企业微信官网注册一个企业微信，注册后打开管理后台，需要其中几个信息；在应用管理中，自建一个应用，此处名叫Prometheus，如图secret和agentid两项需要使用，可见范围可自己定义能接收到消息的群组；  
![](http://showdoc.qingdao.cosmoplat.com/server/index.php?s=%2Fapi%2Fattachment%2FvisitFile%2Fsign%2Fd723153f561ea238467388458da0e694&showdoc=.jpg)  
点击上方通讯录，左侧的部门列表中，可以查看部门id，也是需要的，如图所示：  
![](http://showdoc.qingdao.cosmoplat.com/server/index.php?s=%2Fapi%2Fattachment%2FvisitFile%2Fsign%2F47a0cceca42996f7ae03a6dac7944509&showdoc=.jpg)

#### []()三、配置alertmanager

因为paas资源不够，在虚拟机上搭建此插件，从dockerhub下载alertmanager的官方镜像。 docker pull prom/alertmanager:master ；此镜像需要改动一个配置文件，配置文件需要映射到虚拟机上，设定位置为/etc/alertmanager/alertmanager.yml，配置文件内容为：

01. `global:`
02. `resolve_timeout:2m`
03. `wechat_api_url:'https://qyapi.weixin.qq.com/cgi-bin/'`
04. `wechat_api_secret:'s6GuB1Tu7oMmnhuKULnRb4r6KwbpSOU-PxudV2iQTko'`
05. `wechat_api_corp_id:'ww7792656cb6e551b1'`
06. `route:`
07. `group_by:['alertname']`
08. `group_wait:10s`
09. `group_interval:10s`
10. `repeat_interval:1h`
11. `receiver:'wechat'`
12. `receivers:`
13. `- name:'wechat'`
14. `wechat_configs:`
15. `- send_resolved:true`
16. `to_party:'2'`
17. `agent_id:'1000002'`

其中，wechat\_api\_url固定值，wechat\_api\_secret和agent\_id如图一。wechat\_api\_corp\_id为企业id，位置在我的企业，下拉最下方，若没有出现，等待一段时间刷新或者重新登陆即可，如图：  
![](http://showdoc.qingdao.cosmoplat.com/server/index.php?s=%2Fapi%2Fattachment%2FvisitFile%2Fsign%2F00cbdac100bf7062242e781d9291ff0e&showdoc=.jpg)  
to\_party为部门id，需要发送至哪个部门或者说群组；此处的范围需要小于等于在应用中配置的可见范围。  
配置完毕以上内容后，启动alertmanager： docker run —name alertmanager -d -p 9093:9093 -v /etc/alertmanager/alertmanager.yml:/etc/alertmanager/alertmanager.yml prom/alertmanager:master  
展示端口为9093，启动完毕后可以在页面端查看，页面如图  
![](http://showdoc.qingdao.cosmoplat.com/server/index.php?s=%2Fapi%2Fattachment%2FvisitFile%2Fsign%2F6890d25a7bfe734352ee872f3fd26607&showdoc=.jpg)  
报警内容的发送会在红框范围下展示，处理完毕或恢复的报警，信息会消失，如不处理则一直存在；

#### []()四、配置Prometheus对接alertmanager插件

在paas平台中的配置文件里，找到Prometheus的配置文件。更改配置文件为如下(简略版)：

01. `global:`
02. `scrape_interval:15s`
03. `evaluation_interval:15s`
04. `# Alertmanager configuration`
05. `alerting:`
06. `alertmanagers:`
07. `- static_configs:`
08. `- targets:['10.250.49.43:9093']`
09. `rule_files:`
10. `-"/etc/prometheus/*_rules.yml"`
11. `scrape_configs:`
12. `- job_name:'prometheus'`
13. `static_configs:`
14. `- targets:['localhost:9090']`
15. `- job_name:"qdxw-ops"`
16. `static_configs:`
17. `- targets:`
18. `-"10.250.49.8:9100"`
19. `-"10.250.49.43:9100"`
20. `- job_name:"test-paas-node"`
21. `static_configs:`
22. `- targets:`
23. `-"10.103.127.240:8079"`

其中Alertmanager configuration为alertmanager的地址。  
rule\_files为使用的rules配置文件。

#### []()五、编写rules配置文件

编写报警的rules文件放入Prometheus配置的目录中。如下所示为jvm的简略文件：

01. `groups:`
02. `- name: jvm-alert`
03. `rules:`
04. `# 堆空间使用超过95%`
05. `- alert: heap-usage-too-much`
06. `expr: jvm_memory_bytes_used{area="heap"}/ jvm_memory_bytes_max *100>95`
07. `for:1m`
08. `labels:`
09. `severity: critical`
10. `annotations:`
11. `summary:"JVM Instance {{ $labels.instance }} memory usage > 95%"`
12. `description:"{{ $labels.instance }} of job {{ $labels.job }} has been in status [heap usage > 95%] for more than 1 minutes. current usage ({{ $value }}%)"`
13. `# 在5分钟里，Old GC花费时间超过80%`
14. `- alert: old-gc-time-too-much`
15. `expr: increase(jvm_gc_collection_seconds_sum{gc="PS MarkSweep"}[5m])>5*60*0.8`
16. `for:5m`
17. `labels:`
18. `severity: critical`
19. `annotations:`
20. `summary:"JVM Instance {{ $labels.instance }} Old GC time > 80% running time"`
21. `description:"{{ $labels.instance }} of job {{ $labels.job }} has been in status [Old GC time > 80% running time] for more than 5 minutes. current seconds ({{ $value }}%)"`
22. `- alert: old-gc-time-too-much-compact`
23. `expr: increase(jvm_gc_collection_seconds_sum{gc="MarkSweepCompact"}[5m])>5*60*0.8`
24. `for:5m`
25. `labels:`
26. `severity: critical`
27. `annotations:`
28. `summary:"JVM Instance {{ $labels.instance }} Old GC time > 80% running time"`
29. `description:"{{ $labels.instance }} of job {{ $labels.job }} has been in status [Old GC time > 80% running time] for more than 5 minutes. current seconds ({{ $value }}%)"`

其中alert为报警名或者说项目名；  
expr为语法，此语法可以在Prometheus的页面中进行尝试，如图所示  
![](http://showdoc.qingdao.cosmoplat.com/server/index.php?s=%2Fapi%2Fattachment%2FvisitFile%2Fsign%2Fa6da7c8b6aeb1dc05100b1c26d9140e5&showdoc=.jpg)  
Prometheus有大量的报警规则可以自定义，其中需要内容可以在每个项目启动的javagent和虚拟机的node\_exporter的页面中查看，取其中的数值进行自定义即可。如图为一台虚拟机的node\_exporter的内容：  
![](http://showdoc.qingdao.cosmoplat.com/server/index.php?s=%2Fapi%2Fattachment%2FvisitFile%2Fsign%2F9f86f2d12aef4deeceeb8ef9555adb99&showdoc=.jpg)  
继续，在rules文件中：for为持续时间，上面显示的就为连续的5分钟内堆内存使用超过百分之95；  
labels包含报警严重性等的定义或分级；  
annotations包含描述等；

#### []()六、报警测试

以上步骤都完成后，可以进行报警测试。  
基本每次报警都有两条，其中一条为FIRING，是故障提醒；一条为RESOLVED，为故障恢复提醒。  
下图为报警内容，需要关注的点有三个，首先为故障原因，下图为heap-usage-too-much；第二个是报警的项目，surveytool-admin为调查问卷后台管理；第三点为ip和具体内容，ip就是，具体内容为memory usage &gt; 95%。  
根据这些可以完全了解报警内容，其他内容可以忽略。  
![](http://showdoc.qingdao.cosmoplat.com/server/index.php?s=%2Fapi%2Fattachment%2FvisitFile%2Fsign%2F367fdf3afbe64705fd0e3b41808c01ff&showdoc=.jpg)