这篇文章主要讲解了“怎么用go开发alertmanger实现钉钉报警”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用go开发alertmanger实现钉钉报警”吧!
通过自己的url实现alertmanager的钉钉报警
webhook在alertmanger的配置
- name: 'dingding' webhook_configs: - send_resolved: true url: 'http://xxxx:8080/Alert'
go实现钉钉报警的程序
package main import ( "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 { Labels map[string]string `json:"labels"` Annotations map[string]string `json:annotations` StartsAt time.Time `json:"startsAt"` EndsAt time.Time `json:"endsAt"` } //通知消息结构体 type Notification struct { Version string `json:"version"` GroupKey string `json:"groupKey"` Status string `json:"status"` Receiver string `json:receiver` GroupLabels map[string]string `json:groupLabels` CommonLabels map[string]string `json:commonLabels` CommonAnnotations map[string]string `json:commonAnnotations` ExternalURL string `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 error fmt.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实现钉钉报警这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。