其他分享
首页 > 其他分享> > 【记录一个问题】alert manager要怎么样才能触发告警到企业微信上?

【记录一个问题】alert manager要怎么样才能触发告警到企业微信上?

作者:互联网

作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!


花了几个小时仍然是没走通这个流程,把中间结果记录一下。

1.部署了alert manager

部署的架构主要参考这篇文章:Alertmanager高可用
架构图如下:

为了方便prometheus访问alert manager,在alert manager前面还部署了CLB,可以通过VIP访问alert manager集群。
配置的细节如下:

# 安装amtool
go install github.com/prometheus/alertmanager/cmd/amtool@latest

# 查看服务器端配置
amtool --alertmanager.url=http://9.220.xxx.xxx/ config show

# 查看群集状态
amtool --alertmanager.url=http://9.220.xxx.xxx/ cluster show

2.模拟告警

可以用命令行工具模拟告警:

curl -X POST "http://9.220.xxx.xxx/api/v2/alerts" -H "Content-type: application/json" -d '[{"status": "firing","labels": {"alertname": "name","service": "my-service","severity":"warning","instance": "name.example.net"},"annotations": {"summary": "High latency is high!"},"generatorURL": "http://prometheus.int.example.net/<generating_expression>"}]' -v

打开alert manager的页面,可以看见有这条告警。

3.alertmanager发给webhook的数据是什么格式?

这篇帖子里面有例子:prometheus+alertmanager+webhook实现自定义监控报警系统

json格式如下:

{
    "receiver":"webhook",
    "status":"resolved",
    "alerts":[
        {
            "status":"resolved",
            "labels":{
                "alertname":"hostCpuUsageAlert",
                "instance":"192.168.199.24:9100",
                "severity":"page"
            },
            "annotations":{
                "description":"192.168.199.24:9100 CPU 使用率超过 85% (当前值为: 0.9973333333333395)",
                "summary":"机器 192.168.199.24:9100 CPU 使用率过高"
            },
            "startsAt":"2020-02-29T19:45:21.799548092+08:00",
            "endsAt":"2020-02-29T19:49:21.799548092+08:00",
            "generatorURL":"http://localhost.localdomain:9090/graph?g0.expr=sum+by%28instance%29+%28avg+without%28cpu%29+%28irate%28node_cpu_seconds_total%7Bmode%21%3D%22idle%22%7D%5B5m%5D%29%29%29+%3E+0.85&g0.tab=1",
            "fingerprint":"368e9616d542ab48"
        }
    ],
    "groupLabels":{
        "alertname":"hostCpuUsageAlert"
    },
    "commonLabels":{
        "alertname":"hostCpuUsageAlert",
        "instance":"192.168.199.24:9100",
        "severity":"page"
    },
    "commonAnnotations":{
        "description":"192.168.199.24:9100 CPU 使用率超过 85% (当前值为: 0.9973333333333395)",
        "summary":"机器 192.168.199.24:9100 CPU 使用率过高"
    },
    "externalURL":"http://localhost.localdomain:9093",
    "version":"4",
    "groupKey":"{}:{alertname="hostCpuUsageAlert"}"
}
//版权声明:本文为CSDN博主「昵称2021」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
//原文链接:https://blog.csdn.net/bluuusea/article/details/104619235

然而,企业微信的webhook收的可不是这个格式:消息类型及数据格式

{
    "chatid": "wrkSFfCgAAtMQKg4xqDatM5C9IDHFpTw|@all_subscriber|zhangsan",
	"post_id" : "bpkSFfCgAAWeiHNo2p6lJbG3_F2xxxxx",
	"visible_to_user": "zhangsan|lisi",
    "msgtype": "text",
    "text": {
        "content": "<@zhangsan>广州今日天气:29度,大部分多云,降雨概率:60%",
		"mentioned_list":["wangqing","@all"],
		"mentioned_mobile_list":["13800001111","@all"]
    }
}

很明显,直接把webhook地址配置到alertmanager肯定是发布出去的

4.注册企业微信的开发者

alertmanager里面有很多wechat的配置:

5.使用开源组件来把alert manager webhook 转换为 wechat webhook

还真有,地址在:https://github.com/k8stech/alertmanager-wechatrobot-webhook
后面部署起来试试能不能把alert manager和企业微信串起来。

标签:alertmanager,http,微信,webhook,alert,manager,节点
来源: https://www.cnblogs.com/ahfuzhang/p/16481907.html