一、prometheus 实现钉钉和企业微信告警

基础流程

1.1 钉钉通知

altermanager基础设置可以参照: https://editor.csdn.net/md/?articleId=121845743

钉钉群设置
群设置 -> 智能群助手 -> 添加机器人 -> 自定义 -> 添加 -> 保存生成的webhook地址

1.1.1 测试发送信息 - 关键字认证

root@prometheus:~# mkdir data/scripts -p
root@prometheus:~# cd data/scripts/
root@prometheus:~/data/scripts# vim dinngding-keyworlds.sh
#/bin/bash
source /etc/profile
MESSAGE=$1
curl -X  "POST" '你生成的Webhook地址' \
-H 'Content-Type:application/json' \
-d '{ "msgtype" : "text","text" : {"content":"'${MESSAGE}'"}
}'
root@prometheus:~/data/scripts# chmod +x dinngding-keyworlds.sh
root@prometheus:~/data/scripts# bash dinngding-keyworlds.sh "namespace=defalt\npod=pod1\ncpu=90%\n持续时间=8s\nalertname=pod"
{"errcode":0,"errmsg":"ok"}

1.1.1.1 部署webhook-dingtalk

github地址 : https://github.com/timonwong/prometheus-webhook-dingtalk

root@prometheus:/apps# wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v1.4.0/prometheus-webhook-dingtalk-1.4.0.linux-amd64.tar.gz
root@prometheus:/apps# tar xf prometheus-webhook-dingtalk-1.4.0.linux-amd64.tar.gz
root@prometheus:/apps/prometheus-webhook-dingtalk-1.4.0.linux-amd64# ./prometheus-webhook-dingtalk --web.listen-address="0.0.0.0:8060" --ding.profile="alertname=你的webhook地址"
level=info ts=2022-02-10T03:49:21.015Z caller=main.go:62 msg="Starting prometheus-webhook-dingtalk" version="(version=1.4.0, branch=HEAD, revision=02fe8265a98ab4caaa78ebbed209d3f06b87b4a6)"
level=info ts=2022-02-10T03:49:21.016Z caller=main.go:63 msg="Build context" (gogo1.13.5,userroot@eb9f8d8f0437,date20191211-03:00:38)=(MISSING)
level=warn ts=2022-02-10T03:49:21.016Z caller=main.go:105 msg="DEPRECATION: Detected one of the following flags: --ding.profile, --ding.timeout, --template.file"
level=warn ts=2022-02-10T03:49:21.016Z caller=main.go:106 msg="DEPRECATION: Now working in compatibility mode, please consider upgrading your configurations"
level=info ts=2022-02-10T03:49:21.016Z caller=main.go:117 component=configuration msg="Loading templates" templates=
ts=2022-02-10T03:49:21.016Z caller=main.go:133 component=configuration msg="Webhook urls for prometheus alertmanager" urls=http://0.0.0.0:8060/dingtalk/alertname/send
level=info ts=2022-02-10T03:49:21.016Z caller=web.go:210 component=web msg="Start listening for connections" address=0.0.0.0:8060
level=info ts=2022-02-10T03:49:21.428Z caller=entry.go:22 component=web http_scheme=http http_proto=HTTP/1.1 http_method=POST remote_addr=10.0.0.61:55076 user_agent=Alertmanager/0.23.0 uri=http://10.0.0.61:8060/dingtalk/alertname/send resp_status=200 resp_bytes_length=2 resp_elapsed_ms=184.460895 msg="request complete"#测试一下
root@prometheus:~# telnet 10.0.0.61 8060
Trying 10.0.0.61...
Connected to 10.0.0.61.
Escape character is '^]'.HTTP/1.1 400 Bad Request
Content-Type: text/plain; charset=utf-8
Connection: close400 Bad RequestConnection closed by foreign host.

1.1.1.2 配置alertmanager

root@prometheus:/apps/alertmanager# vim alertmanager.yml
---#修改接受者receiver: 'dingding'
receivers:#添加钉钉
- name: dingdingwebhook_configs:- url:'http://10.0.0.61:8060/dingtalk/altername/send'send_resolved: true

1.1.1.3 验证

1.1.2 测试发送信息 - 加签认证

1.1.2.1 配置加签

1.1.2.2 加签认证-获取认证

root@prometheus:/apps/alertmanager# apt install python2
root@prometheus:~# vim data/scripts/dingding-label-sign.py
#!/usr/bin/python2.7
import time
import hmac
import hashlib
import base64
import urllib
timestamp=long(round(time.time())*1000)
secret='你的加签生成的秘钥'
secret_enc=bytes(secret).encode('utf-8')
string_to_sign='{}\n{}'.format(timestamp,secret)
string_to_sign_enc=bytes(string_to_sign).encode('utf-8')
hmac_code=hmac.new(secret_enc,string_to_sign_enc,digestmod=hashlib.sha256).digest()
sign=urllib.quote_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)
#生成时间戳和认证
root@prometheus:~# python2.7 data/scripts/dingding-label-sign.py

1.1.2.3 消息发送脚本

#测试脚本可用
root@prometheus:~# vim /root/data/scripts/dingding-label-send.sh
#!/bin/bash
source /etc/profile
MESSAGE=$1
secret='你的加签生成的秘钥'
getkey=$(python2.7 /root/data/scripts/dingding-label-sign.py)
timestamp=${getkey:0:13}
sign=$(echo "${getkey:13:100}"|tr -d '\n')
# DateStamp=$(date -d @${getkey:0:10}"+%F%H:%m:%S")curl -X  "POST" "你的webhook地址&timestamp=${timestamp}&sign=${sign}" \
-H 'Content-Type:application/json' \
-d '{ "msgtype" : "text","text" : {"content":"'${MESSAGE}'"}
}'root@prometheus:~# bash /root/data/scripts/dingding-label-send.sh sss
{"errcode":0,"errmsg":"ok"}

1.1.2.4 webhook启动

#先获取当前时间戳和认证秘钥
root@prometheus:~# python2.7 /root/data/scripts/dingding-label-sign.py
#启动webhook的dingtalk
root@prometheus:/apps/prometheus-webhook-dingtalk-1.4.0.linux-amd64# ./prometheus-webhook-dingtalk --web.listen-address="0.0.0.0:8060" --ding.profile="alertname=你的webhook地址&timestamp=生成的时间戳&sign=生成的认证秘钥"
level=info ts=2022-02-10T05:22:45.778Z caller=main.go:62 msg="Starting prometheus-webhook-dingtalk" version="(version=1.4.0, branch=HEAD, revision=02fe8265a98ab4caaa78ebbed209d3f06b87b4a6)"
level=info ts=2022-02-10T05:22:45.778Z caller=main.go:63 msg="Build context" (gogo1.13.5,userroot@eb9f8d8f0437,date20191211-03:00:38)=(MISSING)
level=warn ts=2022-02-10T05:22:45.779Z caller=main.go:105 msg="DEPRECATION: Detected one of the following flags: --ding.profile, --ding.timeout, --template.file"
level=warn ts=2022-02-10T05:22:45.779Z caller=main.go:106 msg="DEPRECATION: Now working in compatibility mode, please consider upgrading your configurations"
level=info ts=2022-02-10T05:22:45.779Z caller=main.go:117 component=configuration msg="Loading templates" templates=
ts=2022-02-10T05:22:45.779Z caller=main.go:133 component=configuration msg="Webhook urls for prometheus alertmanager" urls=http://0.0.0.0:8060/dingtalk/alertname/send
level=info ts=2022-02-10T05:22:45.779Z caller=web.go:210 component=web msg="Start listening for connections" address=0.0.0.0:8060
level=info ts=2022-02-10T05:22:46.788Z caller=entry.go:22 component=web http_scheme=http http_proto=HTTP/1.1 http_method=POST remote_addr=10.0.0.61:59396 user_agent=Alertmanager/0.23.0 uri=http://10.0.0.61:8060/dingtalk/alertname/send resp_status=200 resp_bytes_length=2 resp_elapsed_ms=908.904779 msg="request complete"

1.1.2.5 进行验证

1.2 企业微信通知

1.2.1 创建应用

登录pc的企业微信 -> 应用管理 ->创建应用

1.2.2 测试发送信息

1.2.3 验证测试信息

1.2.4 alertmanager配置

#修改配置文件
root@prometheus:/apps/alertmanager# vim alertmanager.yml
---
route:group_by: ['alertname']group_wait: 10sgroup_interval: 2srepeat_interval: 2m#receiver: 'web.hook'#receiver: dingdingreceiver: wechat
----
- name: wechatwechat_configs:- corp_id: 你的企业IDto_user: '@all' #发送给所有人agent_id: 你的应用idapi_secret: 你的应用秘钥send_resolved: true

1.2.5 验证信息

1.2.6 消息发送给指定组

1.2.6.1 获取部门ID

1.2.6.2 alertmanager配置

root@prometheus:/apps/alertmanager# vim alertmanager.yml
- name: wechatwechat_configs:- corp_id: 你的企业ID#to_user: '@all'to_party: 1 #指定部门IDagent_id: 你的应用IDapi_secret: 你的应用secretsend_resolved: true
root@prometheus:/apps/alertmanager# systemctl restart alertmanager

1.2.6.3 验证信息

1.3 消息分类发送

根据消息中的属性信息设置规则,将消息分类发送,如将severity级别为critical的通知信息发送到邮箱,其他发送到微信

1.3.1 alertmanager设置

route:group_by: ['alertname']group_wait: 10sgroup_interval: 2srepeat_interval: 2m#receiver: 'web.hook'#receiver: dingdingreceiver: wechatroutes: #添加信息路由- receiver: web.hook  #critical级别的信息发送到邮箱group_wait: 10smatch_re:severity: critical

1.3.2 验证信息

1.4 自定义消息模板

默认的消息内容需要调整,而且信息是连接在一起的

1.4.1 定义模板

root@prometheus:/apps/alertmanager# vim alertmanager-wechat.tmpl
{{ define "wechat.default.message" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts -}}=========  监控告警 =========
告警程序:     Alertmanager
告警类型:    {{ $alert.Labels.alertname }}
告警级别:    {{ $alert.Labels.severity }} 级
告警状态:    {{ .Status }}
故障主机:    {{ $alert.Labels.instance }} {{ $alert.Labels.device }}
告警主题:    {{ .Annotations.summary }}
告警详情:    {{ $alert.Annotations.message }}{{ $alert.Annotations.description}}
主机标签:    {{ range .Labels.SortedPairs  }}  [{{ .Name }}: {{ .Value  | html }} ] {{- end }}
故障时间:    {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
========= = end =  =========
{{- end }}
{{- end }}{{- if gt (len .Alerts.Resolved) 0 -}}
{{- range $index, $alert := .Alerts -}}========= 告警恢复 =========
告警程序:     Alertmanager
告警主题:    {{ $alert.Annotations.summary }}
告警主机:    {{ .Labels.instance }}
告警类型:    {{ .Labels.alertname }}
告警级别:    {{ $alert.Labels.severity }} 级
告警状态:    {{   .Status }}
告警详情:    {{ $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" }}
========= = end =  =========
{{- end }}
{{- end }}
{{- end }}

1.4.2 alertmanager引用模板

root@prometheus:/apps/alertmanager# vim alertmanager.yml
---
#添加模板
templates:- /apps/alertmanager/alertmanager-wechat.tmplroot@prometheus:/apps/alertmanager# systemctl restart alertmanager

1.5 告警抑制和静默

1.5.1 告警抑制

基于告警规则,超过80%就不在发60%的告警,即由60%的表达式触发的告警被抑制了

root@prometheus:/apps/prometheus# vim rules.yml
groups:
- name: altermanager_pod.rulesrules:- alert: 磁盘容量expr: 100-(node_filesystem_free_bytes{fstype=~"ext4|xfs"}/node_filesystem_size_bytes{fstype=~"ext4|xfs"}*100)>30 #故意写小for: 2slabels:severity: criticalannotations:description: "{{$labels.mountpoint}} 磁盘分区使用大于30%(目前使用:{{$value}}%)"summary: "{{$labels.mountpoint}} 磁盘分区使用率过高!"- alert: 磁盘容量expr: 100-(node_filesystem_free_bytes{fstype=~"ext4|xfs"}/node_filesystem_size_bytes{fstype=~"ext4|xfs"}*100)>20 #故意写小for: 2slabels:severity: warningannotations:description: "{{$labels.mountpoint}} 磁盘分区使用大于20%(目前使用:{{$value}}%)"summary: "{{$labels.mountpoint}} 磁盘分区使用率过高!"
root@prometheus:/apps/prometheus# systemctl restart prometheus.service
root@prometheus:/apps/prometheus# systemctl restart alertmanager.service

进行验证

1.5.2 手动静默

先找到要静默的告警事件,然后手动静默指定的事件

1.5.2.1 点击静默

1.5.2.2 填写信息并创建

1.5.2.3 查看并验证


进行验证

Prometheus监控(三)—— 钉钉和企业微信告警相关推荐

  1. Zabbix 系统监控(三)VMware 虚拟平台监控、邮件告警、企业微信告警配置

    Vmware 虚拟平台监控.邮件告警.企业微信告警配置 8 Vmware 虚拟平台监控 阅读 zabbix 官方文档,官方提供了 Vmware 虚拟机监控模板,并对模板进行了解释说明,但未对相应名词做 ...

  2. prometheus+grafana监控以及企业微信告警

    prometheus+grafana监控以及企业微信告警(单机二进制部署) 一.下载部署包,更改其中两个包名称,放到/data下 1.安装包以及解压步骤 grafana-enterprise-8.4. ...

  3. 使用Python设置钉钉机器人和企业微信机器人的步骤

    文章目录 一.钉钉机器人 0.参考文档 1.创建一个群 2.设置群助手 3.设置机器人信息 4.编写测试文件 5.访问请求,预警成功 二.企业微信机器人 0.消息发送频率限制 1.创建群聊,增加机器人 ...

  4. Prometheus+Grafana监控安装及配置JVM实现企业微信告警

    背景 本人Java开发工程师一枚,主攻后端,需要搭建一套Prometheus+Grafana的监控系统,采用企业微信告警通知.在网上各种查阅资料的同时,发现很多资料都大致相通且不完整,踩坑无数,经过多 ...

  5. 使用 Prometheus 实现邮件/企业微信告警

    一.安装 AlterManager 如果没有安装 Prometheus 以及监控客户端的话,可以看博主前面的文章:Prometheus(普罗米修斯)监控系统 [root@k8s-master01 ~] ...

  6. Zabbix设置邮件告警+企业微信告警

    一.告警流程 首先要创建模板 在模板中创建触发器和监控项 创建监控项 创建触发器 1.1添加报警媒介 1.2声音设置 1.3创建用户 1.4配置动作 为触发器 系统用户登录数大于3 添加动作 1.5配 ...

  7. Zabbix企业微信告警最新版

    2019独角兽企业重金招聘Python工程师标准>>> Zabbix企业微信告警最新版 2017年11月21日 15:38:33 阅读数:1481 Zabbix企业微信告警最新版 最 ...

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

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

  9. 三分钟实现Prometheus电话短信邮件钉钉飞书企业微信报警

    Prometheus是现在企业用的比较多的开源监控系统,Prometheus电话短信报警更是运维不可缺少的报警渠道,Spug推送助手针对Prometheus内置好了报警模板,可以通过简单的配置就可以实 ...

最新文章

  1. Oracle可以处理LOB字段的常用字符函数
  2. 工作中用的linux命令
  3. 增加fast cgi进程数 php7,使用 FastCGI 模式运行 PHP7 教程
  4. 85、交换机安全MAC层***配置实验之Port-Security
  5. java内部类选择题_java内部类详解(附相关面试题)
  6. 信号中断 与 慢系统调用
  7. 《HTML5 Canvas游戏开发实战》——3.3 自定义画板
  8. php画图抗锯齿,​CSS3如何实现字体抗锯齿渲染效果?-webkit-font-smoothing属性(实例)...
  9. 【解决办法】C++2015安装不上,说是要安装Windows6.1-KB2999226-x64.msu这个补丁,下载下来怎么安装!...
  10. 程序员代码面试指南:IT 名企算法与数据结构题目最优解
  11. 用DIV+Css+Jquery 实现的旧版微信飞机大战。
  12. 小程序样式写了没有用,或许你就差一行代码
  13. 超详细的Python实现新浪微博模拟登陆(小白都能懂)
  14. stm32开发板调试问题记录----去除flash读保护以及debug进不去main.c的问题
  15. 【牛客练习赛41 E.球的体积并】球的体积并模板
  16. 初中英语多词性单词怎么办_初中英语单词按词性分类表
  17. Vue实现表格的分页打印和导出Excel功能
  18. 电脑看斗鱼html5卡,win10浏览器观看斗鱼卡怎么修复_win10浏览器打开斗鱼卡死如何解决...
  19. 表白爱心HTML制作
  20. 逻辑回归——一文带你搞懂逻辑回归原理

热门文章

  1. Understanding OpenStack Authentication: Keyston...
  2. Java数据结构与算法_线性表_顺序表与链表
  3. PET图像的SUV计算
  4. DDN周报|3月26日-4月1日
  5. java代码制作activiti会签_activiti设计器会签人员配置
  6. 验证JDK是否安装成功
  7. 一座适合躺尸的低房价、慢节奏小城
  8. 使用Javascript制作声音按钮
  9. This application failed to start because no Qt platform plugin could be initialized. 报错解决方法
  10. C++设计模式——观察者模式(高屋建瓴)