kapacitor将告警信息推送到微信

kapacitor alert_node: https://docs.influxdata.com/kapacitor/v1.5/nodes/alert_node

kapacitor本身是不支持微信推送的,没有提供微信推送的api。

api 

post: https://docs.influxdata.com/kapacitor/v1.5/event_handlers/post/

exec: https://docs.influxdata.com/kapacitor/v1.5/nodes/alert_node/#exec

由于我的需求是把告警内容推送到微信,别的服务提供的接口是post 发送json数据(数据是动态生成的)调用微信通知接口。

post这个api很显然是不符合我的需求的。

exec这个api只要你给它可执行指令和参数就能帮你实现,当然你也可以把你的指令放到脚本文件里面,让kapacitor执行。

其实脚本文件无非也就是个字符串而已,我没有写脚本文件:

1. 数据是动态的,我要根据不同的数据写不同的脚本文件,要通过代码把数据传入脚本文件

2. 公司的告警服务是放在pos里面执行的,如果使用脚本文件,要么挂载进去,要么把脚本文件写在container的某个目录。当告警内容多了以后,就会写很多文件。也可以在每次写文件的时候判断当前这个告警的数据文件是否存在,存在就删掉再创建

要使用脚本文件其实好复杂的,干脆直接使用exec执行一个能发送post的指令:curl

需要注意的,exec里面应该使用单引号, curl指令应该是绝对路径(kapacitor是支持curl指令的)

完整的脚本(检测k8s endpoint是否ready)

stream
   |from()
        .database('telegraf')
        .retentionPolicy('autogen')
        .measurement('kubernetes_endpoint')
        .where(lambda: "namespace" == 'fd3204f5aa7634f07be2b48521f795397')
   |window()
      .period(180s)
      .every(30s)
   |alert()
      .message('{{.Level}}: pod:{{index .Fields "pod"}} is unready, please check ! \n host: {{ index .Fields "host"}}, node name: {{ index .Fields "node_name"}}')
      .warn(lambda: "ready" == bool('0'))
      .log('/root/pod-alert.log')
      .email('[email protected]')
      .exec('/usr/bin/curl', '-l', '-H', 'Content-type:application/json', '-X', 'POST', '-d', '{"templateCode":"factory.alarm.notify.pod","receivers":["1111111"],"params":[{"name":"Time","value":"2019-11-29 16:17:32"},{"name":"Type","value":"pod"},{"name":"ServiceName","value":"bp-golang"},{"name":"Namespace","value":"fd3204f5aa7634f07be2b48521f795397"},{"name":"Path","value":".bp1"}]}', 'http://xx.xx.xx.xxx:xxx/template/send')

猜你喜欢

转载自blog.csdn.net/u010918487/article/details/103313360