/
hook.go
353 lines (306 loc) · 11.3 KB
/
hook.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package model
import (
"fmt"
"strings"
"time"
"github.com/go-gywn/webhook-go/common"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// Hook Hook
type Hook struct {
HookID string `form:"hook_id" json:"hook_id" gorm:"column:hook_id; type:varchar(32) not null default ''; primaryKey"`
AlertName string `form:"alert_name" json:"alert_name" gorm:"column:alert_name; type:varchar(32) not null default ''; index:ix_name,priority:1"`
Instance string `form:"instance" json:"instance" gorm:"column:instance; type:varchar(32) not null default ''; index:ix_inst,priority:1"`
Job string `form:"job" json:"job" gorm:"column:job; type:varchar(20) not null default ''"`
Level string `form:"level" json:"level" gorm:"column:level; type:varchar(20) not null default ''"`
Ignored string `form:"ignored" json:"ignored" gorm:"column:ignored; type:varchar(1) not null default 'N'; index:ix_ignored,priority:1"`
Status string `form:"status" json:"status" gorm:"column:status; type:varchar(10) not null default ''"`
StartsAt *time.Time `form:"starts_at" json:"starts_at" gorm:"column:starts_at; type:datetime(3) null; index:ix_startat; index:ix_name,priority:2; index:ix_inst,priority:2; index:ix_ignored,priority:2"`
EndsAt *time.Time `form:"ends_at" json:"ends_at" gorm:"column:ends_at; type:datetime(3) null"`
HookDetails []HookDetail `json:"hook_details" gorm:"foreignKey:HookID"`
UpdatedAt time.Time `json:"updated_at"`
}
// HookDetail hook detail
type HookDetail struct {
ID int
HookID string `json:"hook_id" gorm:"column:hook_id; type:varchar(32) not null default ''; index:ix_hookid"`
Status string `json:"status" gorm:"column:status; type:varchar(10) not null default '';"`
ReqJSON string `json:"req_json" gorm:"column:req_json; type:json not null"`
Message string `json:"message" gorm:"column:message; type:text not null"`
CreatedAt time.Time `json:"created_at"`
}
// Upsert insert on duplicate update
func (o *Hook) Upsert(hookColumns ...string) error {
return db.Transaction(func(tx *gorm.DB) (err error) {
// upsert hook main
if len(hookColumns) == 0 {
hookColumns = GetUpsertAllColumns(o)
} else {
hookColumns = GetUpsertAppendColumns(o, hookColumns)
}
logger.Debug("Hook.Upsert() > ", "columns ", hookColumns)
hookClause := clause.OnConflict{DoUpdates: clause.AssignmentColumns(hookColumns)}
if result := db.Clauses(hookClause).Create(&o); result.Error != nil {
logger.Error("Hook.Upsert() > ", result.Error)
}
// upsert hook_detail
hookDetailColumns := GetUpsertAllColumns(&HookDetail{})
logger.Debug("HookDetail.Upsert() > ", "columns ", hookDetailColumns)
hookDetailClause := clause.OnConflict{DoUpdates: clause.AssignmentColumns(hookDetailColumns)}
for _, hookDetail := range o.HookDetails {
if result := db.Clauses(hookDetailClause).Create(&hookDetail); result.Error != nil {
logger.Error("HookDetail.Upsert() > ", result.Error)
}
}
return nil
})
}
func (o *Hook) GetList(limit int) (r []Hook, err error) {
logger.Debug("Hook.GetList() start")
clauseMap := map[string]interface{}{}
if o.HookID != "" {
clauseMap["hook_id"] = o.HookID
}
if o.AlertName != "" {
clauseMap["alert_name"] = o.AlertName
}
if o.Instance != "" {
clauseMap["instance"] = o.Instance
}
if o.Job != "" {
clauseMap["job"] = o.Job
}
if o.Status != "" {
clauseMap["status"] = o.Status
}
if o.Level != "" {
clauseMap["level"] = o.Level
}
tx := db.Where(clauseMap)
if o.StartsAt != nil {
tx = tx.Where("starts_at >= ?", *o.StartsAt)
}
if o.EndsAt != nil {
tx = tx.Where("ends_at < ?", *o.EndsAt)
}
tx = tx.Order("starts_at desc").Limit(limit)
if result := tx.Preload(clause.Associations).Find(&r); result.Error != nil {
logger.Error(result.Error)
err = result.Error
}
return
}
// HookIgnore hook ignore target
type HookIgnore struct {
Instance string `form:"instance" json:"instance" gorm:"column:instance; type:varchar(32) not null default '*'; primaryKey"`
AlertName string `form:"alert_name" json:"alert_name" gorm:"column:alert_name; type:varchar(32) not null default '*'; primaryKey"`
Job string `form:"job" json:"job" gorm:"column:job; type:varchar(20) not null default '*'"`
Status string `form:"status" json:"status" gorm:"column:status; type:varchar(10) not null default '*'; primaryKey"`
Forever bool `form:"forever" json:"forever" gorm:"column:forever; type:tinyint not null default false"`
StartsAt *time.Time `form:"starts_at" json:"starts_at" gorm:"column:starts_at; type:datetime not null"`
EndsAt *time.Time `form:"ends_at" json:"ends_at" gorm:"column:ends_at; type:datetime not null"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func (o *HookIgnore) GetList() (r []HookIgnore, err error) {
logger.Debug("HookIgnore.GetList() start")
if result := db.Preload(clause.Associations).Find(&r); result.Error != nil {
logger.Error(result.Error)
err = result.Error
}
return
}
// Upsert insert on duplicate update
func (o *HookIgnore) Upsert() (rows int64, err error) {
o.setDefault()
var columns = GetUpsertAllColumns(o)
var clause = clause.OnConflict{DoUpdates: clause.AssignmentColumns(columns)}
logger.Debug("HookIgnore.Upsert() > ", "columns ", columns)
result := db.Clauses(clause).Create(&o)
if result.Error == nil {
// cache update
logger.Debug("HookIgnore.Upsert() > ", "updateHookCache ", o.GetKey())
o.updateHookCache()
} else {
logger.Error("HookIgnore.Upsert() > ", result.Error)
}
return result.RowsAffected, result.Error
}
// Delete delete row
func (o *HookIgnore) Delete() (int64, error) {
o.setDefault()
result := db.Delete(o)
if result.Error == nil {
// cache delete
logger.Debug("HookIgnore.Delete() > ", "deleteHookCache ", o.GetKey())
o.deleteHookCache()
} else {
logger.Error("HookIgnore.Delete() > ", result.Error)
}
return result.RowsAffected, result.Error
}
// new ignore cache
func (o *HookIgnore) updateHookCache() {
logger.Debug("syncHookCache > ", "Update cache", o.GetKey())
hookIgnoreMap[o.GetKey()] = *o
}
// del ignore cache
func (o *HookIgnore) deleteHookCache() {
logger.Debug("syncHookCache > ", "Delete cache", o.GetKey())
delete(hookIgnoreMap, o.GetKey())
}
// full sync with database
func (o *HookIgnore) syncHookCache() {
logger.Debug("syncHookCache > ", "Update hook ignore map start")
var hookIgnores []HookIgnore
hookIgnoreMtx.Lock()
defer hookIgnoreMtx.Unlock()
// Get all hook ignores from database
if result := db.Find(&hookIgnores); result.Error != nil {
logger.Error("syncHookCache > ", "db.Find(&hookIgnores) - ", result.Error)
return
}
// Cache update
tmpHookIgnoreMap := make(map[string]HookIgnore)
for _, hookIgnore := range hookIgnores {
k := hookIgnore.GetKey()
tmpHookIgnoreMap[k] = hookIgnore
logger.Debug("syncHookCache > ", "** entry: ", k)
}
hookIgnoreMap = tmpHookIgnoreMap
logger.Info("syncHookCache > ", "Updated cache , ", len(hookIgnoreMap), " entries")
}
// IsTarget check map
func (o *HookIgnore) IsTarget() bool {
var key string
// All hook
key = (&HookIgnore{}).GetKey()
if val, ok := hookIgnoreMap[key]; ok && val.isValiadRange() {
logger.Debug("HookIgnore.IsTarget > ", "global_skip:"+o.GetKey())
return true
}
// Instance alert
key = (&HookIgnore{Instance: o.Instance}).GetKey()
if val, ok := hookIgnoreMap[key]; ok && val.isValiadRange() {
logger.Debug("HookIgnore.IsTarget > ", "instance_skip:"+o.GetKey())
return true
}
// Instance & AlertName alert
key = (&HookIgnore{Instance: o.Instance, AlertName: o.AlertName}).GetKey()
if val, ok := hookIgnoreMap[key]; ok && val.isValiadRange() {
logger.Debug("HookIgnore.IsTarget > ", "instance_alertname_skip:"+o.GetKey())
return true
}
// Instance & AlertName & Job alert
key = (&HookIgnore{Instance: o.Instance, AlertName: o.AlertName, Job: o.Job}).GetKey()
if val, ok := hookIgnoreMap[key]; ok && val.isValiadRange() {
logger.Debug("HookIgnore.IsTarget > ", "instance_alertname_job_skip:"+o.GetKey())
return true
}
// Instance & AlertName & Job & Status alert
key = (&HookIgnore{Instance: o.Instance, AlertName: o.AlertName, Job: o.Job, Status: o.Status}).GetKey()
if val, ok := hookIgnoreMap[key]; ok && val.isValiadRange() {
logger.Debug("HookIgnore.IsTarget > ", "instance_alertname_job_status_skip:"+o.GetKey())
return true
}
return false
}
func (o *HookIgnore) isValiadRange() bool {
if o.Forever {
logger.Debug("HookIgnore.isValiadRange > ", "foever_skip :"+o.GetKey())
return true
}
unixNow := time.Now().Unix()
unixStartsAt := o.StartsAt.Unix()
unixEndsAt := o.EndsAt.Unix()
logger.Debug("HookIgnore.isValiadRange > ", "unixNow:", unixNow, "unixStartsAt:", unixStartsAt, "unixEndsAt:", unixEndsAt)
if unixNow >= unixStartsAt && unixNow <= unixEndsAt {
return true
}
return false
}
func (o *HookIgnore) setDefault() {
if o.StartsAt == nil {
startsAt := time.Now()
logger.Debug("HookIgnore.setDefault > ", "startsAt is null, set", startsAt)
o.StartsAt = &startsAt
}
if o.EndsAt == nil {
endsAt := o.StartsAt.Add(24 * time.Hour)
logger.Debug("HookIgnore.setDefault > ", "endsAt is null, set", endsAt)
o.EndsAt = &endsAt
}
if o.Instance == "" {
o.Instance = "*"
o.AlertName = "*"
o.Job = "*"
o.Status = "*"
logger.Debug("HookIgnore.setDefault > ", "Instance empty")
return
}
if o.AlertName == "" {
o.AlertName = "*"
o.Job = "*"
o.Status = "*"
logger.Debug("HookIgnore.setDefault > ", "AlertName empty")
return
}
if o.Job == "" {
o.Job = "*"
o.Status = "*"
logger.Debug("HookIgnore.setDefault > ", "Job empty")
return
}
if o.Status == "" {
o.Status = "*"
logger.Debug("HookIgnore.setDefault > ", "Status empty")
logger.Debug(o)
}
}
// GetKey get hook ignore key
func (o *HookIgnore) GetKey() (md5 string) {
o.setDefault()
k := fmt.Sprintf("[%s]", strings.ToLower(o.Instance))
k += fmt.Sprintf("[%s]", strings.ToLower(o.AlertName))
k += fmt.Sprintf("[%s]", strings.ToLower(o.Job))
k += fmt.Sprintf("[%s]", strings.ToLower(o.Status))
md5 = cryptor.MD5(k)
return
}
func startHookIgnoreMapThread() {
go func() {
for {
(&HookIgnore{}).syncHookCache()
time.Sleep(time.Duration(common.CONF.Webhook.CacheSyncSec) * time.Second)
}
}()
}
// Notification notification alert - single alert
type Notification struct {
Alertname string `form:"alertname" json:"alertname"`
Instance string `form:"instance" json:"instance"`
Level string `form:"level" json:"level"`
Summary string `form:"summary" json:"summary"`
Message string `form:"message" json:"message"`
}
// CheckForm check form
func (o *Notification) CheckForm() error {
var err error
if strings.TrimSpace(o.Instance) == "" {
return fmt.Errorf("instance empty")
}
if _, ok := common.CONF.Webhook.Targets[o.Level]; !ok {
return fmt.Errorf("level '" + o.Level + "' not in target")
}
if strings.TrimSpace(o.Message) == "" {
return fmt.Errorf("empty message")
}
if strings.TrimSpace(o.Alertname) == "" {
o.Alertname = "unknown"
}
if strings.TrimSpace(o.Summary) == "" {
o.Summary = o.Alertname
}
return err
}