forked from k8stech/alertmanager-wechatrobot-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (58 loc) · 1.84 KB
/
main.go
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
package main
import (
"flag"
"net/http"
"github.com/k8stech/alertmanager-wechatrobot-webhook/model"
"github.com/k8stech/alertmanager-wechatrobot-webhook/notifier"
"github.com/gin-gonic/gin"
)
var (
h bool
RobotKey string
addr string
grafanaUrl string
alertDomain string
)
func init() {
flag.BoolVar(&h, "h", false, "help")
flag.StringVar(&RobotKey, "RobotKey", "", "global wechatrobot webhook, you can overwrite by alert rule with annotations wechatRobot")
flag.StringVar(&addr, "addr", ":8999", "listen addr")
flag.StringVar(&grafanaUrl, "grafanaUrl", "grafana.vnnox.com/d/PwMJtdvnr/k8s-chu-neng-cnanduat", "grafanaUrl url")
flag.StringVar(&alertDomain, "alertDomain", "emscn-prometheus.ampaura.tech", "alertDomain url")
}
func main() {
flag.Parse()
if h {
flag.Usage()
return
}
router := gin.Default()
router.POST("/webhook", func(c *gin.Context) {
var notification model.Notification
err := c.BindJSON(¬ification)
//bodyBytes, err := ioutil.ReadAll(c.Request.Body)
//if err != nil {
// log.Printf("Error reading request body: %v", err)
// c.AbortWithStatus(http.StatusBadRequest)
// return
//}
//// 重新设置请求体以确保后续处理可以正常进行
//c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
//
//// 打印请求体内容
//log.Printf("Request Body: %s", bodyBytes)
////gin.LogFormatter(c.Request)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
RobotKey := c.DefaultQuery("key", RobotKey)
err = notifier.Send(notification, RobotKey, grafanaUrl, alertDomain)
//fmt.Println("notification:", notification, "RobotKey:", RobotKey)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
c.JSON(http.StatusOK, gin.H{"message": "send to wechatbot successful!"})
})
router.Run(addr)
}