-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
utils.go
116 lines (110 loc) · 2.94 KB
/
utils.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
111
112
113
114
115
116
package alert
import (
"encoding/json"
"strings"
"github.com/admpub/nging/v4/application/dbschema"
"github.com/admpub/nging/v4/application/library/common"
"github.com/admpub/nging/v4/application/library/cron/send"
"github.com/admpub/nging/v4/application/library/imbot"
"github.com/admpub/nging/v4/application/registry/alert"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/webx-top/echo/param"
)
type TagValues interface {
TagValues() map[string]string
}
// Send 发送警报
func Send(a *dbschema.NgingAlertRecipient, alertData *alert.AlertData) (err error) {
title := alertData.Title
ct := alertData.Content
if ct == nil {
return
}
params := alertData.Data
switch a.Type {
case `email`:
content := ct.EmailContent(params)
if len(content) == 0 {
return
}
err = send.Mail(a.Account, strings.SplitN(a.Account, `@`, 2)[0], title, content)
case `webhook`:
if a.Platform == alert.RecipientPlatformWebhookCustom { // 自定义webhook
custom := &alert.WebhookCustom{}
extraBytes := com.Str2bytes(a.Extra)
if err := json.Unmarshal(extraBytes, custom); err != nil {
err = common.JSONBytesParseError(err, extraBytes)
return err
}
if len(custom.Url) == 0 {
if len(a.Account) > 7 {
switch a.Account[0:7] {
case `https:/`, `http://`:
custom.Url = a.Account
}
}
}
var tagReplacer func(string) string
var tagValues map[string]string
if tv, ok := ct.(TagValues); ok {
tagValues = tv.TagValues()
} else {
tagValues = map[string]string{
`title`: alertData.Title,
`emailContent`: string(ct.EmailContent(params)),
`markdownContent`: string(ct.MarkdownContent(params)),
}
}
if tagValues != nil {
tagReplacer = func(content string) string {
for tag, value := range tagValues {
content = strings.ReplaceAll(content, `{{`+tag+`}}`, value)
}
return content
}
}
return custom.ToWebhook().Exec(tagReplacer, tagReplacer)
}
// 内置 imbot 的 webhook
mess := imbot.Open(a.Platform)
if mess == nil || mess.Messager == nil {
return
}
content := ct.MarkdownContent(params)
if len(content) == 0 {
return
}
message := string(content)
var apiURL string
if len(a.Account) > 7 {
switch a.Account[0:7] {
case `https:/`, `http://`:
apiURL = a.Account
}
}
if len(apiURL) == 0 {
apiURL = mess.Messager.BuildURL(a.Account)
}
var atMobiles []string
extra := echo.H{}
if len(a.Extra) > 0 {
json.Unmarshal(com.Str2bytes(a.Extra), &extra)
}
if extra.Has(`at`) {
switch v := extra.Get(`at`).(type) {
case []interface{}:
atMobiles = make([]string, len(v))
for k, m := range v {
atMobiles[k] = param.AsString(m)
}
case []string:
atMobiles = v
}
}
go func(apiURL string, title string, message string, atMobiles ...string) {
err = mess.Messager.SendMarkdown(apiURL, title, message, atMobiles...)
}(apiURL, title, message, atMobiles...)
}
return
}