/
callback.go
110 lines (90 loc) · 2.99 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2017 Xiaomi, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cron
import (
"fmt"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/open-falcon/falcon-plus/common/model"
"github.com/open-falcon/falcon-plus/common/utils"
"github.com/open-falcon/falcon-plus/modules/alarm/api"
"github.com/open-falcon/falcon-plus/modules/alarm/redi"
"github.com/toolkits/net/httplib"
)
func HandleCallback(event *model.Event, action *api.Action) {
teams := action.Uic
phones := []string{}
mails := []string{}
ims := []string{}
if teams != "" {
phones, mails, ims = api.ParseTeams(teams)
smsContent := GenerateSmsContent(event)
mailContent := GenerateMailContent(event)
imContent := GenerateIMContent(event)
if action.BeforeCallbackSms == 1 {
redi.WriteSms(phones, smsContent)
redi.WriteIM(ims, imContent)
}
if action.BeforeCallbackMail == 1 {
redi.WriteMail(mails, smsContent, mailContent)
}
}
message := Callback(event, action)
if teams != "" {
if action.AfterCallbackSms == 1 {
redi.WriteSms(phones, message)
redi.WriteIM(ims, message)
}
if action.AfterCallbackMail == 1 {
redi.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("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("left_value", utils.ReadableFloat(event.LeftValue))
req.Param("tags", tags)
resp, e := req.String()
success := "success"
if e != nil {
log.Errorf("callback fail, action:%v, event:%s, error:%s", action, event.String(), e.Error())
success = fmt.Sprintf("fail:%s", e.Error())
}
message := fmt.Sprintf("curl %s %s. resp: %s", action.Url, success, resp)
log.Debugf("callback to url:%s, event:%s, resp:%s", action.Url, event.String(), resp)
return message
}