Prometheus配置企业微信报警

更多技术博客,请关注微信公众号:运维之美

Prometheus被号称是下一代的监控,可以解决云上K8S集群的监控问题,搭配部署alertmanager,可以实现告警发送,本篇我们就通过企业微信实现告警发送,运维小哥可以躺平了。

环境:prometheus服务端和alertmanager部署在同一台机器上,实验前提是prometheus服务端已经安装好

操作系统:Centos7.4

prometheus的告警管理分为两部分。通过在prometheus服务端设置告警规则, Prometheus服务器端产生告警向Alertmanager发送告警。然后,Alertmanager管理这些告警,包括静默,抑制,聚合以及通过电子邮件,企业微信,钉钉等方法发送告警通知。

设置警报和通知的主要步骤如下:

安装启动Alertmanager;

配置Prometheus对Alertmanager访问,配置告警规则;

配置企微后台,alertmanager配置对接企微并配置告警模板;

修改阈值触发告警

01 安装AlertManager

以官网最新版本为例,可以从官网地址https://prometheus.io/download/下载alertmanager安装包

[root@prometheus ~]# mkdir -p /usr/local/alertmanager
[root@prometheus~]# tar -xvf alertmanager-0.22.2.linux-amd64.tar.gz  -C /usr/local/alertmanager
[root@prometheus~]# cd /usr/local/alertmanager/
[root@prometheus alertmanager]# nohup ./alertmanager &

将包上传到服务器上,按照上面步骤安装和启动alertmanager服务

02 配置prometheus

prometheus中添加配置监控alertmanager服务器

prometheus.yml添加如下配置

alerting:alertmanagers:- static_configs:- targets:- 192.168.61.123:9093
rule_files:- "rules/*_rules.yml"- "rules/*_alerts.yml"scrape_configs:- job_name: 'alertmanager'static_configs:- targets: ['localhost:9093']

rule_files为告警触发的规则文件

prometheus当前路径下新建rules目录,创建如下配置文件

[root@prometheus prometheus]# cd rules/
[root@prometheus rules]# ls
node_alerts.yml  pod_rules.yml

Node节点告警配置

node_alerts.yml #监控主机级别告警

[root@prometheus rules]# cat node_alerts.yml
groups:
- name: 主机状态-监控告警rules:- alert: 主机状态expr: up {job="kubernetes-nodes"} == 0for: 15slabels:status: 非常严重annotations:summary: "{{.instance}}:服务器宕机"description: "{{.instance}}:服务器延时超过15s"- alert: CPU使用情况expr: 100-(avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) by(instance)* 100) > 60for: 1mlabels:status: warningannotations:summary: "{{$labels.instance}}: High CPU Usage Detected"description: "{{$labels.instance}}: CPU usage is {{$value}}, above 60%"- alert: NodeFilesystemUsageexpr: 100 - (node_filesystem_free_bytes{fstype=~"ext4|xfs"} / node_filesystem_size_bytes{fstype=~"ext4|xfs"} * 100) > 80for: 1mlabels:severity: warningannotations:summary: "Instance {{ $labels.instance }} : {{ $labels.mountpoint }} 分区使用率过高"description: "{{ $labels.instance }}: {{ $labels.mountpoint }} 分区使用大于80% (当前值: {{ $value }})"- alert: 内存使用expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100 > 80for: 1mlabels:status: 严重告警annotations:summary: "{{ $labels.instance}} 内存使用率过高!"description: "{{ $labels.instance }} 内存使用大于80%(目前使用:{{ $value}}%)"- alert: IO性能expr: (avg(irate(node_disk_io_time_seconds_total[1m])) by(instance)* 100) > 60for: 1mlabels:status: 严重告警annotations:summary: "{{$labels.instance}} 流入磁盘IO使用率过高!"description: "{{ $labels.instance }} 流入磁盘IO大于60%(目前使用:{{ $value }})"- alert: 网络expr: ((sum(rate (node_network_receive_bytes_total{device!~'tap.*|veth.*|br.*|docker.*|virbr*|lo*'}[5m])) by (instance)) / 100) > 102400for: 1mlabels:status: 严重告警annotations:summary: "{{ $labels.instance}} 流入网络带宽过高!"description: "{{ $labels.instance }}流入网络带宽持续2分钟高于100M. RX带宽使用率{{ $value }}"- alert: TCP会话expr: node_netstat_Tcp_CurrEstab > 1000for: 1mlabels:status: 严重告警annotations:summary: "{{ $labels.instance }} TCP_ESTABLISHED过高!"description: "{{ $labels.instance }} TCP_ESTABLISHED大于1000%(目前使用:{{ $value }}%)"

pod告警配置

pod_rules.yml文件配置 #pod级别告警

[root@prometheus rules]# cat pod_rules.yml
groups:
- name: k8s_pod.rulesrules:- alert: pod-statusexpr: kube_pod_container_status_running != 1for: 5slabels:severity: warningannotations:description : pod-{{ $labels.pod }}故障summary: pod重启告警- alert: Pod_all_cpu_usageexpr: (sum by(name)(rate(container_cpu_usage_seconds_total{image!=""}[5m]))*100) > 10for: 5mlabels:severity: criticalservice: podsannotations:description: 容器 {{ $labels.name }} CPU 资源利用率大于 75% , (current value is {{ $value }})summary: Dev CPU 负载告警- alert: Pod_all_memory_usageexpr: sort_desc(avg by(name)(irate(container_memory_usage_bytes{name!=""}[5m]))*100) > 1024*10^3*2for: 10mlabels:severity: criticalannotations:description: 容器 {{ $labels.name }} Memory 资源利用率大于 2G , (current value is {{ $value }})summary: Dev Memory 负载告警- alert: Pod_all_network_receive_usageexpr: sum by (name)(irate(container_network_receive_bytes_total{container_name="POD"}[1m])) > 1024*1024*50for: 10mlabels:severity: criticalannotations:description: 容器 {{ $labels.name }} network_receive 资源利用率大于 50M , (current value is {{ $value }})summary: network_receive 负载告警

for子句:Prometheus将expr中的规则作为触发条件, 在这种情况下,Prometheus将在每次检查警报是否继续处于活动状态,然后再触发警报。处于活动状态但尚未触发的元素处于pending状态,for中定义时间即为达到活动状态持续时间才触发告警

配置加之后热重启prometheus服务

curl -XPOST http://192.168.61.123:9090/-/reload
注:prometheus启动命令添加参数–web.enable-lifecycle可实现支持热重启

$ ./promtool check config prometheus.yml
Checking prometheus.ymlSUCCESS: 0 rule files found

上面命令可以检查配置文件修改是否正确

登录prometheus targets界面已经出现alertmanager指标

检查prometheus告警规则配置是否生效

可以看到node和pod的监控指标都已经加载,Perfect,离成功更近一步

03 配置AlertManager

实现企业微信告警通知,需要首先在企业后台创建应用,起名叫prometheus

记录企业ID,secret,agentid信息,后边配置文件中需要。

[root@prometheus-bpmdev alertmanager]# cat alertmanager.yml
global:resolve_timeout: 1m   # 每1分钟检测一次是否恢复wechat_api_url: 'https://qyapi.weixin.qq.com/cgi-bin/'wechat_api_corp_id: 'xxxxxxxxx'      # 企业微信中企业IDwechat_api_secret: 'xxxxxxxx'templates:- '/usr/local/alertmanager/template/*.tmpl'
route:receiver: 'wechat'group_by: ['env','instance','type','group','job','alertname']group_wait: 10sgroup_interval: 5srepeat_interval: 1hreceivers:
- name: 'wechat'wechat_configs:- send_resolved: truemessage: '{{ template "wechat.default.message" . }}'to_party: '57'agent_id: 'xxxx'   #企微后台查询的agentidto_user : "@all"api_secret: 'xxxxxxx'  #后台查询的secret

注意

wechat_api_url配置为企业微信的接口地址,因此需要alertmanager所在服务器能够连接公网

to_user需要配置,all是发送所有可见范围用户,无此标签告警无法发出,本人亲测,企微后台可见范围可以添加接收告警的用户

字段解释

global:全局配置

resolve_timeout:告警恢复超时时间,当接收的告警没有EndsAt字段时,经过该时间就将该告警标志为已解决,prometheus上用不上,告警都会带EndsAt字段

route:告警分配配置

group_by:设置分组标签,告警时出现的labels都可用于分组,如果需要对所有不同label都分组,可以使用’…’

group_wait:告警发送等待时间,时间拉长便于告警聚合

group_interval:前后两组告警发送间隔时间

repeat_interval:重复告警发送间隔时间

receiver:定义接收告警的对象

receivers:告警接收对象,这部分信息参考步骤1获取

name:告警接收名称,与route中的receiver一一对应,这里我们配置的是企业微信

corp_id: 企业微信唯一ID,我的企业 -> 企业信息

to_party: 告警需要发送的组

agent_id: 自己创建应用的ID,自己创建的应用详情页面查看

api_secret: 自己创建应用的密钥,自己创建的应用详情页面查看

send_resolved: 告警解决是否发送通知

inhibit_rules:告警抑制规则

当新的告警匹配到target_match规则,而已发送告警满足source_match规则,并且新告警与已发送告警中equal定义的标签完全相同,则抑制这个新的告警。

上述配置的结果就是同个instance的同个alertname告警,major会抑制warning告警,这很好理解,比如阈值告警,达到critical肯定也达到了warning,没必要发送两个告警。

不过,从实际测试结果看,这个抑制规则只能在触发告警时使用,对于告警恢复没有,应该是个bug,也有可能我用的版本过低,有时间再去看下源码,查一查

templates:告警消息模板

企业微信告警发送模板,当前路径新建template目录

[root@prometheus-bpmdev alertmanager]# cat template/wechat.tmpl
{{ define "wechat.default.message" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 }}
=========xxx环境监控报警 =========
告警状态:{{   .Status }}
告警级别:{{ .Labels.severity }}
告警类型:{{ $alert.Labels.alertname }}
故障主机: {{ $alert.Labels.instance }} {{ $alert.Labels.pod }}
告警主题: {{ $alert.Annotations.summary }}
告警详情: {{ $alert.Annotations.message }}{{ $alert.Annotations.description}};
触发阀值:{{ .Annotations.value }}
故障时间: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
========= = end =  =========
{{- end }}
{{- end }}
{{- end }}
{{- if gt (len .Alerts.Resolved) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 }}
=========xxx环境异常恢复 =========
告警类型:{{ .Labels.alertname }}
告警状态:{{   .Status }}
告警主题: {{ $alert.Annotations.summary }}
告警详情: {{ $alert.Annotations.message }}{{ $alert.Annotations.description}};
故障时间: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
恢复时间: {{ ($alert.EndsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
{{- if gt (len $alert.Labels.instance) 0 }}
实例信息: {{ $alert.Labels.instance }}
{{- end }}
========= = end =  =========
{{- end }}
{{- end }}
{{- end }}
{{- end }}

配置修改后,执行命令curl -XPOST http://192.168.61.123:9093/-/reload进行热重启

配置完成,我们可以调整告警阈值进行测试

修改/usr/local/prometheus/rules/node_alerts.yml中磁盘告警阈值

expr: 100 - (node_filesystem_free_bytes{fstype=~“ext4|xfs”} / node_filesystem_size_bytes{fstype=~“ext4|xfs”} * 100) > 80
修改为>10就告警,登录管理界面发现马上就收到告警了

这里说明一下 Prometheus Alert 告警状态有三种状态:Inactive、Pending、Firing。

Inactive:非活动状态,表示正在监控,但是还未有任何警报触发。

Pending:表示这个警报必须被触发。由于警报可以被分组、压抑/抑制或静默/静音,所以等待验证,一旦所有的验证都通过,则将转到 Firing 状态。

Firing:将警报发送到 AlertManager,它将按照配置将警报的发送给所有接收者。一旦警报解除,则将状态转到 Inactive,如此循环。

大功告成,此处该有掌声!

公众号:运维之美 不定期更新领取IT学习资料 关注一波!

Prometheus配置企业微信报警相关推荐

  1. Zabbix 3.0 配置企业微信报警(注册---测试)

    一.申请企业微信 1.登录企业微信官网,点击企业注册 二.配置企业微信 1.邀请管理员使用企业微信,如果有多个人直接添加新成员 2.管理员收到邀请,下载手机版企业微信,使用微信号登陆即可 3.创建应用 ...

  2. 搭建Prometheus配置企业微信告警

    ** 一.各种配置文件和容器准备 ** 1.数据监测端口 docker run -d -p 9100:9100 -v /proc:/bridge/proc -v /sys:/bridge/sys -v ...

  3. Zabbix 3.0 配置企业微信报警(配置zabbix-web)

    一.添加报警媒体类型 Name:自定义 Type:选择script Scripts name:填写脚本名称 Script parameters:脚本参数--corpid=XXX--corpsecret ...

  4. Prometheus+Grafana+企业微信机器人告警

    Prometheus+Grafana+企业微信机器人告警 开源监控和报警系统 Prometheus+Grafana+企业微信机器人告警 Prometheus+Grafana+企业微信机器人告警 1.P ...

  5. zabbix4.0添加mysql报警_Zabbix 3.4.3实现企业微信报警

    Zabbix 3.4.3实现企业微信报警 [日期:2017-11-08] 来源:Linux社区  作者:wzlinux [字体:大 中 小] 一.企业微信注册 微信的报警方式可以让我们随时随地接收到信 ...

  6. AlertManager实现企业微信报警(十三)

    AlertManager实现企业微信报警 1.AlertManager对接企业微信 ALertManager支持的报警方式有邮件报警.钉钉报警.微信报警,本次将实现企业微信报警 2.企业微信配置 2. ...

  7. Zabbix配置企业微信群聊机器人告警

    转载来源 : Zabbix配置企业微信群聊机器人告警 : https://www.jianshu.com/p/b5b1f92b1f15 最近在给内部使用的zabbix配置告警发送,要求是使用企业微信群 ...

  8. zabbix 配置企业微信告警群机器人告警

    一.企业微信端配置 1.创建微信群机器人 在需要接收告警信息的企业微信群上右键(注意群里成员至少要3人以上),选择"添加群机器人",设置机器人名称,系统自动生成此机器人的webho ...

  9. Zabbix实现企业微信报警

    zabbix实现企业微信报警 1.下载安装手机版企业微信 2.注册账户,并创建一个企业,之后向企业中添加员工 3.找到我的企业->记录企业ID:ww1d1845f4b25a12fb 4.在企业下 ...

最新文章

  1. python多层数组合成一个数组后循环打印出数组内的每一项元素的方法
  2. 关于CAShapeLayer的一些实用案例和技巧
  3. mysql之 binlog维护详细解析(开启、binlog相关参数作用、mysqlbinlog解读、binlog删除)...
  4. 数据智能是未来商业的核心竞争力之一
  5. Python面试题总结(9)--高级特性
  6. 垃圾分类智能化-垃圾分类机器人
  7. teamlab与redmine试用对比报告
  8. POJ 2039 Floyd
  9. Open3d之点云体素下采样
  10. 在mysql中存储生日,php中计算今天是否为用户生日
  11. linux常用命令之压缩打包
  12. opencv:基于颜色空间的肤色检测方法
  13. 电脑常见故障_电脑知识打印机常见故障及排除方法系列(四)
  14. Navicat Premiumx64 使用注册机激活
  15. 关于PC套件显示红外连接出现问题而导致连接不上的解决方案(zz)
  16. DELL服务器 RAID 配置详解
  17. 大型机、小型机、x86架构以及ARM架构服务器的区别
  18. diskpart命令创建GPT磁盘分区
  19. 【Linux】如何将ntfs硬盘挂载到home目录下并具有读写权限
  20. vue+node.js+mysql的数据库课程设计有感

热门文章

  1. 如何使用ghost制作系统镜像
  2. 喻世明言 第三十一卷 闹阴司司马貌断狱(讲三国是如何开始的)
  3. L2-039 清点代码库
  4. Glide加载图片模糊问题
  5. oracle 10g R2数据库的安装部署
  6. cocos2d-x2.1.2精灵表单与表单编辑器
  7. docker exec -it container1 /bin/bash 异常
  8. android 字符串中截取,【安卓按键精灵】几种字符串提取的方法(源码)
  9. 用python画皮卡丘的代码-用python画一只可爱的皮卡丘
  10. adb shell 查看手机根目录