forked from Cepave/open-falcon-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.go
91 lines (74 loc) · 2.22 KB
/
callback.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
89
90
91
package cron
import (
"fmt"
"github.com/Cepave/open-falcon-backend/common/model"
"github.com/Cepave/open-falcon-backend/modules/alarm/api"
"github.com/Cepave/open-falcon-backend/modules/alarm/redis"
"github.com/toolkits/net/httplib"
"strings"
"time"
)
func HandleCallback(event *model.Event, action *api.Action) {
// falcon,dinp
teams := action.Uic
phones := []string{}
mails := []string{}
if teams != "" {
phones, mails = api.ParseTeams(teams)
smsContent := GenerateSmsContent(event)
mailContent := GenerateMailContent(event)
if action.BeforeCallbackSms == 1 {
redis.WriteSms(phones, smsContent)
}
if action.BeforeCallbackMail == 1 {
redis.WriteMail(mails, smsContent, mailContent)
}
}
message := Callback(event, action)
if teams != "" {
if action.AfterCallbackSms == 1 {
redis.WriteSms(phones, message)
}
if action.AfterCallbackMail == 1 {
redis.WriteMail(mails, message, message)
}
}
}
func Callback(event *model.Event, action *api.Action) string {
if action.Url == "" {
return "callback url is blank"
}
L := make([]string, 0)
if len(event.PushedTags) > 0 {
for k, v := range event.PushedTags {
L = append(L, fmt.Sprintf("%s:%s", k, v))
}
}
tags := ""
if len(L) > 0 {
tags = strings.Join(L, ",")
}
req := httplib.Get(action.Url).SetTimeout(3*time.Second, 20*time.Second)
req.Param("endpoint", event.Endpoint)
req.Param("metric", event.Metric())
req.Param("func", event.Func())
req.Param("leftValue", fmt.Sprintf("%.3f", event.LeftValue))
req.Param("operator", event.Operator())
req.Param("rightValue", fmt.Sprintf("%.3f", event.RightValue()))
req.Param("note", event.Note())
req.Param("status", event.Status)
req.Param("step", fmt.Sprintf("%d", event.CurrentStep))
req.Param("priority", fmt.Sprintf("%d", event.Priority()))
req.Param("time", event.FormattedTime())
req.Param("tpl_id", fmt.Sprintf("%d", event.TplId()))
req.Param("exp_id", fmt.Sprintf("%d", event.ExpressionId()))
req.Param("stra_id", fmt.Sprintf("%d", event.StrategyId()))
req.Param("tags", tags)
resp, e := req.String()
success := "success"
if e != nil {
success = fmt.Sprintf("fail:%s", e.Error())
}
message := fmt.Sprintf("curl %s %s. resp: %s", action.Url, success, resp)
return message
}