公司使用的企业微信,但是想把报警通知到群里。但是企业微信机器人找了下没有现成的方案。
所以使用go封装了自己的一个企业微信机器人接口,
封装规则是根据告警等级通知到某个企业微信群。
prometheus的severity字段分为了warning critical emergency info等告警级别
其中需要开发看的级别是warning我选择通知到wechatdev的这个企业微信机器人中
其他的报警需要运维看选择报警到运维在的wechatpush企业微信群中。
缺点:有些情况不兼容,收敛需要配合Alertmanager进行抑制。(下面的镜像不建议到生产中使用)开发环境测试下还是可以的!
如果你有开发能力可以把报警状态写入redis,然后根据redis的状态决定多少时间后再次报警。
下面是我们准备的yaml文件
config_yaml.yaml
port: 0.0.0.0:8888
webhook:
wechatpush: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=您的企业微信机器人key"
wechatdev: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=您的企业微信机器人"
webkuboard: "http://ip:31011/graph" //我这里选择报警级别的一个打开链接地址,因为有些告警需要上普罗米修斯看。
[root@yx images]# cat Dockerfile
#处理时区问题不然报警相差8小时
FROM alpine
MAINTAINER www.g6k.cn
RUN apk add -U tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./wechat /opt
CMD ["/opt/wechat"]
打包推送镜像(因为是改的别人的闭源代码不方便公布,有开发能力的自己写一个接口是比较简单的,后续可能会开放出来)
docker build -t wechat-webhook:v9 .
docker tag wechat-webhook:v9 registry.cn-hangzhou.aliyuncs.com/yx-k8s/wechat-webhook:latest
docker push registry.cn-hangzhou.aliyuncs.com/yx-k8s/wechat-webhook:latest
k8s准备yaml文件首先准备ConfigMap文件方便我们配置
vim wechat-webhook.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: wechat-webhook
namespace: monitoring
data:
config_yaml.yaml: |-
port: 0.0.0.0:8888
webhook:
wechatpush: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=您的企业微信机器人key"
wechatdev: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=您的企业微信机器人"
然后准备wechat的yaml文件
注意需要定义工作目录到go的运行目录,不然会导致出错。然后使用了subPath挂载了配置文件,不使用会直接清空opt目录。
在使用镜像拉取策略时候选择拉取最新的。
vim wechat-webhook.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wechat-webhook
namespace: monitoring
spec:
replicas: 1
selector:
matchLabels:
app: wechat-webhook
template:
metadata:
name: wechat-webhook
labels:
app: wechat-webhook
spec:
containers:
- name: wechat-webhook
image: registry.cn-hangzhou.aliyuncs.com/yx-k8s/wechat-webhook
imagePullPolicy: Always
ports:
- containerPort: 8888
volumeMounts:
- name: config
mountPath: /opt/config_yaml.yaml
subPath: config_yaml.yaml
workingDir: /opt
volumes:
- name: config
configMap:
name: wechat-webhook
---
apiVersion: v1
kind: Service
metadata:
name: wechat-webhook
namespace: monitoring
labels:
app: wechat-webhook
annotations:
prometheus.io/scrape: 'false'
spec:
selector:
app: wechat-webhook
ports:
- name: wechat-webhook
port: 8888
protocol: TCP
targetPort: 8888
pod启动成功后。效果如下
最后一步修改prometheus operator的密文配置文件alertmanager-main(就不截图了和钉钉的配置一样改下地址即可)
"global":
"resolve_timeout": "5m"
"receivers":
- "name": "Webhook"
"webhook_configs":
- "url": "http://wechat-webhook.monitoring.svc.cluster.local:8888/alert"
"route":
"group_by":
- "namespace"
"group_wait": "30s"
"receiver": "Webhook"
"repeat_interval": "12h"
"routes":
- "matchers":
- "alertname = Webhook"
"receiver": "Webhook"
报警效果如下分别不同的报警通知到不同的群里面了。因为没有测试完成后续还会进行测试,可以先使用本人的镜像,也可以自己编写。
One comment
今天修复了一个warning字段下没有job字段后不能报警的bug