商业网站建设规划书,长沙网站设计精选柚v米科技,网站设计到底做多宽,wordpress简单网址导航模板欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码)#xff1a;https://github.com/zq2599/blog_demos 《prometheus实战》系列链接
prometheus实战之一#xff1a;用ansible部署prometheus实战之二#xff1a;使用常见指标prometheus实战之三#xff1a;告警…欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码)https://github.com/zq2599/blog_demos 《prometheus实战》系列链接
prometheus实战之一用ansible部署prometheus实战之二使用常见指标prometheus实战之三告警规则prometheus实战之四alertmanager的部署和配置prometheus实战之五飞书通知告警
本篇概览 本文是《prometheus实战》系列的第五篇主要内容是完成任务应用服务器CPU使用率偏高时飞书APP收到告警通知完整的数据流如下图 前文的进度是完成了上图的绿色部分今天要做的就是完成红色部分让完整的功能可以使用 前文咱们部署好了alertmanager也在alertmanager上配置了告警时的webhook地址然后还触发告警试了一下不过由于webhook地址对应的服务并不存在于是alertmanager调用失败通过日志咱们观察到以下错误
May 13 10:04:40 deskmini alertmanager[767]: ts2023-05-13T02:04:40.869Z callernotify.go:732 levelwarn componentdispatcher receiverweb.hook integrationwebhook[0] msgNotify attempt failed, will retry later attempts1 errPost \http://192.168.50.134:8888/webhook\: dial tcp http://192.168.50.134:8888/webhook: connect: connection refused
May 13 10:09:40 deskmini alertmanager[767]: ts2023-05-13T02:09:40.869Z callerdispatch.go:352 levelerror componentdispatcher msgNotify for alerts failed num_alerts1 errweb.hook/webhook[0]: notify retry canceled after 16 attempts: Post \http://192.168.50.134:8888/webhook\: dial tcp http://192.168.50.134:8888/webhook: connect: connection refused
May 13 10:09:40 deskmini alertmanager[767]: ts2023-05-13T02:09:40.869Z callernotify.go:732 levelwarn componentdispatcher receiverweb.hook integrationwebhook[0] msgNotify attempt failed, will retry later attempts1 errPost \http://192.168.50.134:8888/webhook\: dial tcp http://192.168.50.134:8888/webhook: connect: connection refused今天的任务就是把这个web服务开发出来并运行起来这样整个功能就完善了
准备工作飞书机器人
本次实战打算用飞书作为通知消息的途径因此要先把飞书机器人准备好步骤如下先创建一个群聊名称随意 点击设置 点击群机器人打开群机器人设置页面 点击添加 选择自定义机器人 对机器人的名称和描述做简单的说明 如下图页面会给出这个机器人唯一的webhook地址也就是说只要向这个地址发送请求机器人就会发言内容就是请求body 把上面的webhook地址准备好稍后编码的时候会用到机器人已经准备好了接下来开始编码这里要声明一下选用飞书机器人仅仅是欣宸个人觉得它简单方便您完全可以按自己喜好选择其他通知途径
源码下载
接下来要开发web服务如果您不想写代码可以在下面的仓库下载完整源码更换飞书机器人的webhook地址后即可正常使用
名称链接备注项目主页https://github.com/zq2599/blog_demos该项目在GitHub上的主页git仓库地址(https)https://github.com/zq2599/blog_demos.git该项目源码的仓库地址https协议git仓库地址(ssh)gitgithub.com:zq2599/blog_demos.git该项目源码的仓库地址ssh协议
这个git项目中有多个文件夹本篇的文件在tutorials/prometheus/webhook/文件夹下如下图红框所示
开发web服务梳理开发步骤
本篇打算使用go语言来开发web服务web框架选用的是hertz其实这不重要您可以选择自己擅长的语言和框架来完成之所以用hertz是因为它提供了命令可以快速创建工程仅此而已动手写代码之前先梳理好编码的具体步骤 #mermaid-svg-jEzOoCNvxiSvVZSZ {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-jEzOoCNvxiSvVZSZ .error-icon{fill:#552222;}#mermaid-svg-jEzOoCNvxiSvVZSZ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-jEzOoCNvxiSvVZSZ .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-jEzOoCNvxiSvVZSZ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-jEzOoCNvxiSvVZSZ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-jEzOoCNvxiSvVZSZ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-jEzOoCNvxiSvVZSZ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-jEzOoCNvxiSvVZSZ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-jEzOoCNvxiSvVZSZ .marker.cross{stroke:#333333;}#mermaid-svg-jEzOoCNvxiSvVZSZ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-jEzOoCNvxiSvVZSZ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-jEzOoCNvxiSvVZSZ .cluster-label text{fill:#333;}#mermaid-svg-jEzOoCNvxiSvVZSZ .cluster-label span{color:#333;}#mermaid-svg-jEzOoCNvxiSvVZSZ .label text,#mermaid-svg-jEzOoCNvxiSvVZSZ span{fill:#333;color:#333;}#mermaid-svg-jEzOoCNvxiSvVZSZ .node rect,#mermaid-svg-jEzOoCNvxiSvVZSZ .node circle,#mermaid-svg-jEzOoCNvxiSvVZSZ .node ellipse,#mermaid-svg-jEzOoCNvxiSvVZSZ .node polygon,#mermaid-svg-jEzOoCNvxiSvVZSZ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-jEzOoCNvxiSvVZSZ .node .label{text-align:center;}#mermaid-svg-jEzOoCNvxiSvVZSZ .node.clickable{cursor:pointer;}#mermaid-svg-jEzOoCNvxiSvVZSZ .arrowheadPath{fill:#333333;}#mermaid-svg-jEzOoCNvxiSvVZSZ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-jEzOoCNvxiSvVZSZ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-jEzOoCNvxiSvVZSZ .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-jEzOoCNvxiSvVZSZ .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-jEzOoCNvxiSvVZSZ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-jEzOoCNvxiSvVZSZ .cluster text{fill:#333;}#mermaid-svg-jEzOoCNvxiSvVZSZ .cluster span{color:#333;}#mermaid-svg-jEzOoCNvxiSvVZSZ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-jEzOoCNvxiSvVZSZ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 1. 创建工程 2. 搭好web框架 3. 定义数据结构:alertmanager的请求体 4. 定义数据结构:飞书消息的请求体和响应 5. 转换器,将alertmanager请求体转换为飞书请求体 6. 主逻辑,收到alertmanager请求时向飞书发请求 7. 路由配置,将web path和主逻辑绑定 接下来的操作就按照上述步骤进行先来创建web工程吧
创建web工程
再次强调这里的web服务只是个helloworld级别的小工程不论用什么语言什么框架都无所谓您完全可以随心所欲接下来介绍我这边的开发步骤请确保本地golang已经部署成功并且将$GOPATH/bin添加到 PATH 环境变量之中(例如 export PATH$GOPATH/bin:$PATH)首先是安装hertz请确保本地golang已经部署成功执行以下命令
go install github.com/cloudwego/hertz/cmd/hzlatest新建名为webhook的文件夹在里面执行以下命令就会创建名为webhook的web工程
hz new -module webhook下载依赖包
go mod tidy此时再看webhook目录整个代码框架已经准备好了接下来只要把业务代码填上去即可
tree webhook
webhook
├── biz
│ ├── handler
│ │ └── ping.go
│ └── router
│ └── register.go
├── build.sh
├── go.mod
├── main.go
├── router_gen.go
├── router.go
└── script└── bootstrap.sh编码alertmanager请求体定义
先定义model这是接受alertmanager请求的数据结构新建文件webhook/biz/model/alert.go
package modelimport timetype Alert struct {Labels map[string]string json:labelsAnnotations map[string]string json:annotationsStartsAt time.Time json:startsAtEndsAt time.Time json:endsAt
}type Notification struct {Version string json:versionGroupKey string json:groupKeyStatus string json:statusReceiver string json:receiverGroupLabels map[string]string json:groupLabelsCommonLabels map[string]string json:commonLabelsCommonAnnotations map[string]string json:commonAnnotationsExternalURL string json:externalURLAlerts []Alert json:alerts
}编码飞书消息的请求和响应
由于要请求飞书服务器因此请求和响应的数据结构也要定义好新建文件webhook/biz/model/lark.go
package model// 飞书机器人支持的POST数据结构
// 请求体相关
type LarkRequest struct {MsgType string json:msg_typeContent Content json:content
}
type Content struct {Text string json:text
}// 响应体相关
type LarkResponse struct {Code int json:codeMsg string json:msgData Data json:data
}
type Data struct {
}编码alertmanager到飞书请求体的转换工具
收到alertmanager请求后要把有效内容转换成飞书请求体这里做一个转换工具新建文件webhook/biz/util/lark_transformer.go
package utilimport (bytesfmtwebhook/biz/model
)// TransformToLarkRequest 根据alertmanager的对象创建出飞书消息的对象
func TransformToLarkRequest(notification model.Notification) (larkRequest *model.LarkRequest, err error) {var buffer bytes.Buffer// 先拿到分组情况buffer.WriteString(fmt.Sprintf(通知组%s状态[%s]\n告警项\n\n, notification.GroupKey, notification.Status))// 每条告警逐个获取拼接到一起for _, alert : range notification.Alerts {buffer.WriteString(fmt.Sprintf(摘要%s\n详情%s\n, alert.Annotations[summary], alert.Annotations[description]))buffer.WriteString(fmt.Sprintf(开始时间: %s\n\n, alert.StartsAt.Format(15:04:05)))}// 构造出飞书机器人所需的数据结构larkRequest model.LarkRequest{MsgType: text,Content: model.Content{Text: buffer.String(),},}return larkRequest, nil
}编码主逻辑 数据结构和工具方法都准备好了接下来就是主逻辑收到alertmanager的请求后根据请求体转为飞书消息请求体再向飞书发送请求 接着是响应请求的handler新建文件webhook/biz/handler/alertmanager.go
// Code generated by hertz generator.package handlerimport (bytescontextencoding/jsonionet/httpwebhook/biz/modelwebhook/biz/utilgithub.com/cloudwego/hertz/pkg/appgithub.com/cloudwego/hertz/pkg/common/hloggithub.com/cloudwego/hertz/pkg/common/utilsgithub.com/cloudwego/hertz/pkg/protocol/consts
)const (// 请使用您自己的机器人的webhook地址LARK_URL https://open.feishu.cn/open-apis/bot/v2/hook/12345678-1234-1234-1234-123456789012
)// Ping .
func AlertmanagerWebhook(ctx context.Context, c *app.RequestContext) {var notification model.Notification// 绑定对象err : c.BindAndValidate(notification)if err ! nil {c.JSON(consts.StatusBadRequest, utils.H{error: err.Error(),})return}hlog.Info(收到alertmanager告警\n%s, notification)// 根据alertmanager的请求构造飞书消息的请求数据结构larkRequest, _ : util.TransformToLarkRequest(notification)// 向飞书服务器发送POST请求将飞书服务器返回的内容转为对象bytesData, _ : json.Marshal(larkRequest)req, _ : http.NewRequest(POST, LARK_URL, bytes.NewReader(bytesData))req.Header.Add(content-type, application/json)res, err : http.DefaultClient.Do(req)// 飞书服务器可能通信失败if err ! nil {hlog.Error(请求飞书服务器失败%s, err)c.JSON(consts.StatusInternalServerError, utils.H{error: err.Error(),})return}defer res.Body.Close()body, _ : io.ReadAll(res.Body)var larkResponse model.LarkResponseerr json.Unmarshal([]byte(body), larkResponse)// 飞书服务器返回的包可能有问题if err ! nil {hlog.Error(获取飞书服务器响应失败%s, err)c.JSON(consts.StatusInternalServerError, utils.H{error: err.Error(),})return}hlog.Info(向飞书服务器发送消息成功)c.JSON(consts.StatusOK, utils.H{message: successful receive alert notification message!,})
}编码结束可以运行起来试试了
vscode配置
如果您的IDE是vscode将launch.json写成下面这样就能在vscode启动这个项目了
{version: 0.2.0,configurations: [{name: Launch Package,type: go,request: launch,mode: auto,program: ${workspaceFolder}}]
}shell脚本配置
如果想在linux下编译、构建、运行可以用hertz准备好的shell脚本不过要先做一点修改打开script/bootstrap.sh内容如下
#!/bin/bash
CURDIR$(cd $(dirname $0); pwd)
BinaryName
echo $CURDIR/bin/${BinaryName}
exec $CURDIR/bin/${BinaryName}上述内容中BinaryName变量没有值这会导致运行程序时找不到二进制文件这里给它加上修改后如下
#!/bin/bash
CURDIR$(cd $(dirname $0); pwd)
BinaryNamewebhook
echo $CURDIR/bin/${BinaryName}
exec $CURDIR/bin/${BinaryName}现在可以用现成的shell编译和运行项目了构建命令是sh build.sh启动命令是sh output/bootstrap.sh
验证 将web服务运行起来确保其地址和alertmanager配置的一致 想办法触发告警我这里是用ffmpeg使得应用服务器CPU使用类升高如下图prometheus的告警进入Firing状态 alertmanager收到告警 很快飞书消息也到达了内容符合预期 想办法让应用服务器退出告警状态我这里是杀掉ffmpeg进程让应用服务器的CPU回到正常状态 很快飞书的第二条消息到来状态是resolved表示应用服务器已经退出告警状态 两条告警消息的间隔是5分钟这和alertmanager的配置有关如下图group_interval表示5分钟内不在firing状态就表示故障已恢复 至此从部署到配置再到最终飞书通知整个告警的流程咱们都动手实现了希望这一系列实战操作能给您一些参考助您搭建出匹配业务的告警系统
你不孤单欣宸原创一路相伴
Java系列Spring系列Docker系列kubernetes系列数据库中间件系列DevOps系列 文章转载自: http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.hdscx.cn.gov.cn.hdscx.cn http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.qqpg.cn.gov.cn.qqpg.cn http://www.morning.hxxwq.cn.gov.cn.hxxwq.cn http://www.morning.prxqd.cn.gov.cn.prxqd.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.yqgny.cn.gov.cn.yqgny.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.krzrg.cn.gov.cn.krzrg.cn http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.nzfjm.cn.gov.cn.nzfjm.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.xblrq.cn.gov.cn.xblrq.cn http://www.morning.qrpdk.cn.gov.cn.qrpdk.cn http://www.morning.pwfwk.cn.gov.cn.pwfwk.cn http://www.morning.qgjgsds.com.cn.gov.cn.qgjgsds.com.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.fhghy.cn.gov.cn.fhghy.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.snrhg.cn.gov.cn.snrhg.cn http://www.morning.qlxgc.cn.gov.cn.qlxgc.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.cnqwn.cn.gov.cn.cnqwn.cn http://www.morning.dqxph.cn.gov.cn.dqxph.cn http://www.morning.kpypy.cn.gov.cn.kpypy.cn http://www.morning.qghjc.cn.gov.cn.qghjc.cn http://www.morning.mhybs.cn.gov.cn.mhybs.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.wtcyz.cn.gov.cn.wtcyz.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.rfkyb.cn.gov.cn.rfkyb.cn http://www.morning.glkhx.cn.gov.cn.glkhx.cn http://www.morning.cfhwn.cn.gov.cn.cfhwn.cn http://www.morning.tpmnq.cn.gov.cn.tpmnq.cn http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.rrqbm.cn.gov.cn.rrqbm.cn http://www.morning.nrzkg.cn.gov.cn.nrzkg.cn http://www.morning.ptlwt.cn.gov.cn.ptlwt.cn http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.nfbkz.cn.gov.cn.nfbkz.cn http://www.morning.ktmbp.cn.gov.cn.ktmbp.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.bwrbm.cn.gov.cn.bwrbm.cn http://www.morning.xqffq.cn.gov.cn.xqffq.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.fkyqm.cn.gov.cn.fkyqm.cn http://www.morning.dmrjx.cn.gov.cn.dmrjx.cn http://www.morning.ztmnr.cn.gov.cn.ztmnr.cn http://www.morning.sxbgc.cn.gov.cn.sxbgc.cn http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.bbyqz.cn.gov.cn.bbyqz.cn http://www.morning.nnykz.cn.gov.cn.nnykz.cn http://www.morning.zyrcf.cn.gov.cn.zyrcf.cn http://www.morning.bpmns.cn.gov.cn.bpmns.cn http://www.morning.qrhh.cn.gov.cn.qrhh.cn http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn http://www.morning.qtwd.cn.gov.cn.qtwd.cn http://www.morning.zrwlz.cn.gov.cn.zrwlz.cn http://www.morning.frpb.cn.gov.cn.frpb.cn http://www.morning.tgts.cn.gov.cn.tgts.cn http://www.morning.qsbcg.cn.gov.cn.qsbcg.cn http://www.morning.dktyc.cn.gov.cn.dktyc.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.hwprz.cn.gov.cn.hwprz.cn http://www.morning.fsrtm.cn.gov.cn.fsrtm.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.rdwm.cn.gov.cn.rdwm.cn http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.fsjcn.cn.gov.cn.fsjcn.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.qgghj.cn.gov.cn.qgghj.cn