go开发alertmanger实现钉钉报警
通过自己的url实现alertmanager的钉钉报警
webhook在alertmanger的配置
- name: 'dingding'webhook_configs:- send_resolved: trueurl: 'http://xxxx:8080/Alert'
go实现钉钉报警的程序
package mainimport ( "bytes" "encoding/json" "fmt" "net/http" "strings" "time" "log" "github.com/gin-gonic/gin")const ( webHook_Alert = "https://oapi.dingtalk.com/robot/send?access_token=xxxx")type Message struct { MsgType string `json:"msgtype"` Text struct {Content string `json:"content"`Mentioned_list string `json:"mentioned_list"`Mentioned_mobile_list string `json:"mentioned_mobile_list"` } `json:"text"`}type Alert struct { Labelsmap[string]string `json:"labels"` Annotations map[string]string `json:annotations` StartsAttime.Time`json:"startsAt"` EndsAttime.Time`json:"endsAt"`}//通知消息结构体type Notification struct { Versionstring`json:"version"` GroupKeystring`json:"groupKey"` Statusstring`json:"status"` Receiverstring`json:receiver` GroupLabelsmap[string]string `json:groupLabels` CommonLabelsmap[string]string `json:commonLabels` CommonAnnotations map[string]string `json:commonAnnotations` ExternalURLstring`json:externalURL` Alerts[]Alert`json:alerts`}//获取报警信息func getAlertInfo(notification Notification) string { var m Message m.MsgType = "text" msg,err := json.Marshal(notification.GroupLabels) if err != nil {log.Println("notification.GroupLabels Marshal failed",err) } msg1,err := json.Marshal(notification.CommonAnnotations["summary"]) if err != nil {log.Println("notification.CommonAnnotations Marshal failed",err) } //告警消息 var buffer bytes.Buffer buffer.WriteString(fmt.Sprintf("告警: %v\n",string(msg))) buffer.WriteString(fmt.Sprintf("Endpoint: %v\n",string(msg1))) buffer.WriteString(fmt.Sprintf("告警描述: \"我挂了,快来救我^OO^\"\n")) buffer.WriteString(fmt.Sprintf("Status:%v\n",notification.Status)) //恢复消息 var buffer2 bytes.Buffer buffer2.WriteString(fmt.Sprintf("告警: %v\n",string(msg))) buffer2.WriteString(fmt.Sprintf("Endpoint: %v\n",string(msg1))) buffer2.WriteString(fmt.Sprintf("告警描述: \"哈哈哈,我又回来了!!!\"\n")) //buffer2.WriteString(fmt.Sprintf("mentioned_mobile_list: %v\n",msgres["mentioned_mobile_list"])) buffer2.WriteString(fmt.Sprintf("Status:%v\n",notification.Status)) if notification.Status == "resolved"{m.Text.Content = buffer2.String() }else {m.Text.Content = buffer.String() } jsons, err := json.Marshal(m) resp := string(jsons) return resp}//钉钉报警func SendAlertDingMsg(msg string) { defer func() {if err := recover(); err != nil {fmt.Println("err")} }() content := `{"msgtype": "text","text": {"content": "` + msg + `"} }` //创建一个请求 req, err := http.NewRequest("POST", webHook_Alert, strings.NewReader(content)) if err != nil {fmt.Println(err)fmt.Println("钉钉报警请求异常") } client := &http.Client{} //设置请求头 req.Header.Set("Content-Type", "application/json; charset=utf-8") //发送请求 resp, err := client.Do(req) if err != nil {// handle errorfmt.Println(err)fmt.Println("顶顶报发送异常!!!") } defer resp.Body.Close()}func alert( c *gin.Context){ var notification Notification err := c.BindJSON(¬ification) if err != nil {c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})return } msg := getAlertInfo(notification) SendAlertDingMsg(msg)}func main(){ t := gin.Default()t.GET("/",func(c *gin.Context){c.String(http.StatusOK,"关于alertmanager实现钉钉报警的方法!!!!")}) t.POST("/Alert",alert) t.Run(":8080")}
可以通过编译二进制打包部署在k8s环境(可以参考下篇文章,如果在k8s环境中部署gin程序)
【go开发alertmanger实现钉钉报警】到此这篇关于go开发alertmanger实现钉钉报警的文章就介绍到这了,更多相关go 钉钉报警内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 深入理解Go之generate
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 标签、语法规范、内联框架、超链接、CSS的编写位置、CSS语法、开发工具、块和内联、常用选择器、后代元素选择器、伪类、伪元素。
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- java中如何实现重建二叉树
- 人脸识别|【人脸识别系列】| 实现自动化妆