1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| package main
import ( "bytes" "encoding/json" "fmt" "io/ioutil" "log" "net/http" "strings" "time"
"github.com/gin-gonic/gin" )
type Message struct { MsgType string `json:"msg_type"` Content struct { Text string `json:"text"` } `json:"content"` }
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` }
var defaultRobot = "https://open.feishu.cn/open-apis/bot/v2/hook/7f52c85b-7b0c-415f-8428-5b548b3ee973"
func SendMessage(notification Notification, defaultRobot string) { msg, err := json.Marshal(notification.GroupLabels["alertname"]) if err != nil { log.Println("notification.GroupLabels Marshal failed,", err) return }
msg1, err := json.Marshal(notification.CommonAnnotations["description"]) if err != nil { log.Println("notification.CommonAnnotations Marshal failed,", err) return }
msg2, err := json.Marshal(notification.CommonLabels["instance"]) if err != nil { log.Println("notification.CommonAnnotations Marshal failed,", err) return }
var buffer bytes.Buffer buffer.WriteString(fmt.Sprintf("%v\n", string(msg))) buffer.WriteString(fmt.Sprintf("告警主机: %v\n", string(msg2))) buffer.WriteString(fmt.Sprintf("告警详情: %v\n", string(msg1))) buffer.WriteString(fmt.Sprintf("<at user_id=\"all\">所有人</at>")) var buffer2 bytes.Buffer buffer2.WriteString(fmt.Sprintf("告警恢复: %v\n", string(msg))) buffer2.WriteString(fmt.Sprintf("告警主机: %v\n", string(msg2))) buffer2.WriteString(fmt.Sprintf("告警详情: %v\n", string(msg1))) buffer2.WriteString(fmt.Sprintf("<at user_id=\"all\">所有人</at>"))
var m Message m.MsgType = "text" if notification.Status == "resolved" { m.Content.Text = buffer2.String() } else if notification.Status == "firing" { m.Content.Text = buffer.String() }
jsons, err := json.Marshal(m) if err != nil { log.Println("SendMessage Marshal failed,", err) return }
resp := string(jsons) client := &http.Client{}
req, err := http.NewRequest("POST", defaultRobot, strings.NewReader(resp)) if err != nil { log.Println("SendMessage http NewRequest failed,", err) return }
req.Header.Set("Content-Type", "application/json") r, err := client.Do(req) if err != nil { log.Println("SendMessage client Do failed", err) return }
defer r.Body.Close() body, err := ioutil.ReadAll(r.Body) if err != nil { log.Println("SendMessage ReadAll Body failed", err) return }
log.Println("SendMessage success,body:", string(body)) }
func Alter(c *gin.Context) { var notification Notification
err := c.BindJSON(¬ification) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } SendMessage(notification, defaultRobot) }
func main() { t := gin.Default() t.POST("/Alter", Alter) t.Run(":8080") }
|