forked from baowei/alertmanager-wechatrobot-webhook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
88 lines (62 loc) · 1.83 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"flag"
"net/http"
"alertmanager-wechatrobot-webhook/model"
"alertmanager-wechatrobot-webhook/notifier"
"alertmanager-wechatrobot-webhook/transformer"
"github.com/gin-gonic/gin"
)
var (
h bool
RobotKey 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")
}
func main() {
flag.Parse()
if h {
flag.Usage()
return
}
router := gin.Default()
router.POST("/wechatwebhook", func(c *gin.Context) {
var notification model.Notification
err := c.ShouldBindJSON(¬ification)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
RobotKey := c.DefaultQuery("key", RobotKey)
markdown, robotURL, err := transformer.TransformToMarkdown(notification)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
err = notifier.Send(markdown, robotURL, 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.POST("/elastalertwechat", func(c *gin.Context) {
var elastalert model.ElastalertModel
err := c.ShouldBindJSON(&elastalert)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
RobotKey := c.DefaultQuery("key", RobotKey)
markdown, robotURL, err := transformer.ElastalertTransformToMarkdown(elastalert)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
err = notifier.Send(markdown, robotURL, 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(":8999")
}