-
Notifications
You must be signed in to change notification settings - Fork 494
/
expr.go
533 lines (512 loc) · 15.2 KB
/
expr.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package web
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/mail"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"bosun.org/cmd/bosun/cache"
"bosun.org/cmd/bosun/conf"
"bosun.org/cmd/bosun/conf/rule"
"bosun.org/cmd/bosun/expr"
"bosun.org/cmd/bosun/sched"
"bosun.org/models"
"bosun.org/opentsdb"
"github.com/MiniProfiler/go/miniprofiler"
"github.com/bradfitz/slice"
)
// for executing expressions/rules via the web UI, we use a cache that we retain during the lifetime of bosun
// Matt and I decided not to expire the cache at given points (such as reloading rule page), but I forgot why. ?
// the only risk is that if you query your store for data -5m to now and your store doesn't have the latest points up to date,
// and then 5m from now you query -10min to -5m you'll get the same cached data, including the incomplete last points
var cacheObj = cache.New("web", 100)
func Expr(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (v interface{}, err error) {
defer func() {
if pan := recover(); pan != nil {
v = nil
err = fmt.Errorf("%v", pan)
}
}()
text, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
rawLines := strings.Split(strings.TrimSpace(string(text)), "\n")
var lines []string
for _, line := range rawLines {
// remove comments and empty lines before processing so comments can be after the final line
if line == "" || strings.HasPrefix(line, "#") {
continue
}
lines = append(lines, line)
}
var expression string
vars := map[string]string{}
varRegex := regexp.MustCompile(`(\$\w+)\s*=(.*)`)
for i, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// last line is expression we care about
if i == len(lines)-1 {
expression = schedule.RuleConf.Expand(line, vars, false)
} else { // must be a variable declatation
matches := varRegex.FindStringSubmatch(line)
if len(matches) == 0 {
return nil, fmt.Errorf("Expect all lines before final expression to be variable declarations of form `$foo = something`")
}
name := strings.TrimSpace(matches[1])
value := strings.TrimSpace(matches[2])
vars[name] = schedule.RuleConf.Expand(value, vars, false)
}
}
e, err := expr.New(expression, schedule.RuleConf.GetFuncs(schedule.SystemConf.EnabledBackends()))
if err != nil {
return nil, err
}
now, err := getTime(r)
if err != nil {
return nil, err
}
// it may not strictly be necessary to recreate the contexts each time, but we do to be safe
backends := &expr.Backends{
TSDBContext: schedule.SystemConf.GetTSDBContext(),
GraphiteContext: schedule.SystemConf.GetGraphiteContext(),
InfluxConfig: schedule.SystemConf.GetInfluxContext(),
ElasticHosts: schedule.SystemConf.GetElasticContext(),
AzureMonitor: schedule.SystemConf.GetAzureMonitorContext(),
PromConfig: schedule.SystemConf.GetPromContext(),
}
providers := &expr.BosunProviders{
Cache: cacheObj,
Search: schedule.Search,
Squelched: nil,
History: nil,
Annotate: AnnotateBackend,
}
res, queries, err := e.Execute(backends, providers, t, now, 0, false, "Web: expression execution")
if err != nil {
return nil, err
}
for _, r := range res.Results {
if r.Computations == nil {
r.Computations = make(models.Computations, 0)
}
}
ret := struct {
Type string
Results []*expr.Result
Queries map[string]opentsdb.Request
}{
e.Tree.Root.Return().String(),
res.Results,
make(map[string]opentsdb.Request),
}
for _, q := range queries {
if e, err := url.QueryUnescape(q.String()); err == nil {
ret.Queries[e] = q
}
}
return ret, nil
}
func getTime(r *http.Request) (now time.Time, err error) {
now = time.Now().UTC()
if fd := r.FormValue("date"); len(fd) > 0 {
if ft := r.FormValue("time"); len(ft) > 0 {
fd += " " + ft
} else {
fd += " " + now.Format("15:04:05")
}
now, err = time.Parse("2006-01-02 15:04:05", fd)
if err != nil {
now, err = time.Parse("2006-01-02 15:04", fd)
}
}
return
}
type Res struct {
*models.Event
Key models.AlertKey
}
func procRule(t miniprofiler.Timer, ruleConf conf.RuleConfProvider, a *conf.Alert, now time.Time, summary bool, email string, template_group string, incidentID int) (*ruleResult, error) {
s := &sched.Schedule{}
s.Search = schedule.Search
if err := s.Init("web", schedule.SystemConf, ruleConf, schedule.DataAccess, AnnotateBackend, false, false); err != nil {
return nil, err
}
rh := s.NewRunHistory(now, cacheObj)
if _, err, _ := s.CheckExpr(t, rh, a, a.Warn, models.StWarning, nil); err != nil {
return nil, err
}
if _, err, _ := s.CheckExpr(t, rh, a, a.Crit, models.StCritical, nil); err != nil {
return nil, err
}
keys := make(models.AlertKeys, len(rh.Events))
criticals, warnings, normals := make([]models.AlertKey, 0), make([]models.AlertKey, 0), make([]models.AlertKey, 0)
i := 0
for k, v := range rh.Events {
v.Time = now
keys[i] = k
i++
switch v.Status {
case models.StNormal:
normals = append(normals, k)
case models.StWarning:
warnings = append(warnings, k)
case models.StCritical:
criticals = append(criticals, k)
default:
return nil, fmt.Errorf("unknown state type %v", v.Status)
}
}
sort.Sort(keys)
var rt *models.RenderedTemplates
var data interface{}
var nots map[string]*conf.PreparedNotifications
var aNots map[string]map[string]*conf.PreparedNotifications
warning := make([]string, 0)
if !summary && len(keys) > 0 {
var primaryIncident *models.IncidentState
if template_group != "" {
ts, err := opentsdb.ParseTags(template_group)
if err != nil {
return nil, err
}
for _, ak := range keys {
if ak.Group().Subset(ts) {
primaryIncident = sched.NewIncident(ak)
primaryIncident.Events = []models.Event{*rh.Events[ak]}
break
}
}
}
if primaryIncident == nil {
primaryIncident = sched.NewIncident(keys[0])
primaryIncident.Events = []models.Event{*rh.Events[keys[0]]}
if template_group != "" {
warning = append(warning, fmt.Sprintf("template group %s was not a subset of any result", template_group))
}
}
e := primaryIncident.Events[0]
if e.Crit != nil {
primaryIncident.Result = e.Crit
} else if e.Warn != nil {
primaryIncident.Result = e.Warn
}
var errs []error
primaryIncident.Id = int64(incidentID)
// See if the incidentID corresponds to a real Incident, and if so
// get some information from the real incident
if realIncident, err := schedule.DataAccess.State().GetIncidentState(primaryIncident.Id); err == nil {
primaryIncident.PreviousIds = realIncident.PreviousIds
}
primaryIncident.Start = time.Now().UTC()
primaryIncident.CurrentStatus = e.Status
primaryIncident.LastAbnormalStatus = e.Status
primaryIncident.LastAbnormalTime = models.Epoch{Time: time.Now().UTC()}
func() {
defer func() {
if err := recover(); err != nil {
s := fmt.Sprint(err)
warning = append(warning, s)
errs = append(errs, fmt.Errorf("panic rendering templates: %s", err))
}
}()
rt, errs = s.ExecuteAll(rh, a, primaryIncident, false)
}()
for _, err := range errs {
warning = append(warning, err.Error())
}
if rt == nil {
rt = &models.RenderedTemplates{}
}
if len(errs) > 0 {
var err error
rt.Subject, rt.Body, err = s.ExecuteBadTemplate(errs, rh, a, primaryIncident)
if err != nil {
rt.Subject = fmt.Sprintf("unable to create tempalate error notification: %v", err)
}
} else if email != "" {
m, err := mail.ParseAddress(email)
if err != nil {
return nil, err
}
n := conf.Notification{
Email: []*mail.Address{m},
}
n.PrepareAlert(rt, string(primaryIncident.AlertKey), rt.Attachments...).Send(s.SystemConf)
}
nots, aNots = buildNotificationPreviews(a, rt, primaryIncident, s.SystemConf, ruleConf)
data = s.Data(rh, primaryIncident, a, false)
}
rr := &ruleResult{
Criticals: criticals,
Warnings: warnings,
Normals: normals,
Time: now,
Data: data,
Result: rh.Events,
Warning: warning,
RenderedTemplates: rt,
Notifications: nots,
ActionNotifications: aNots,
}
return rr, nil
}
func buildNotificationPreviews(a *conf.Alert, rt *models.RenderedTemplates, incident *models.IncidentState, c conf.SystemConfProvider, rcp conf.RuleConfProvider, attachments ...*models.Attachment) (map[string]*conf.PreparedNotifications, map[string]map[string]*conf.PreparedNotifications) {
previews := map[string]*conf.PreparedNotifications{}
actionPreviews := map[string]map[string]*conf.PreparedNotifications{}
nots := map[string]*conf.Notification{}
for name, not := range a.CritNotification.GetAllChained() {
nots[name] = not
}
for name, not := range a.WarnNotification.GetAllChained() {
nots[name] = not
}
for name, not := range nots {
previews[name] = not.PrepareAlert(rt, string(incident.AlertKey), attachments...)
actions := map[string]*conf.PreparedNotifications{}
actionPreviews[name] = actions
// for all action types. just loop through known range. Update this if any get added
for at := models.ActionAcknowledge; at <= models.ActionCancelClose; at++ {
if !not.RunOnActionType(at) {
continue
}
incidents := []*models.IncidentState{incident}
actions[at.String()] = not.PrepareAction(at, a.Template, c, incidents, "somebody", "I took care of this", rcp)
}
}
return previews, actionPreviews
}
type ruleResult struct {
Criticals []models.AlertKey
Warnings []models.AlertKey
Normals []models.AlertKey
Time time.Time
*models.RenderedTemplates
Notifications map[string]*conf.PreparedNotifications
ActionNotifications map[string]map[string]*conf.PreparedNotifications
Data interface{}
Result map[models.AlertKey]*models.Event
Warning []string
}
func TestHTTPNotification(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) {
prep := &conf.PreparedHttp{}
dec := json.NewDecoder(r.Body)
if err := dec.Decode(prep); err != nil {
return nil, err
}
code, err := prep.Send()
dat := &struct {
Error string
Status int
}{"", code}
if err != nil {
dat.Error = err.Error()
}
return dat, nil
}
func Rule(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) {
var from, to time.Time
var err error
if f := r.FormValue("from"); len(f) > 0 {
from, err = time.Parse(tsdbFormatSecs, f)
if err != nil {
return nil, err
}
}
if f := r.FormValue("to"); len(f) > 0 {
to, err = time.Parse(tsdbFormatSecs, f)
if err != nil {
return nil, err
}
}
intervals := 1
if i := r.FormValue("intervals"); len(i) > 0 {
intervals, err = strconv.Atoi(r.FormValue("intervals"))
if err != nil {
return nil, err
}
if intervals < 1 {
return nil, fmt.Errorf("must be > 0 intervals")
}
}
incidentID := 42
if incident := r.FormValue("incidentId"); len(incident) > 0 {
incidentID, err = strconv.Atoi(incident)
if err != nil {
return nil, err
}
}
if fz, tz := from.IsZero(), to.IsZero(); fz && tz {
from = time.Now()
} else if fz && !tz {
return nil, fmt.Errorf("cannot specify to without from")
} else if !fz && tz && intervals > 1 {
return nil, fmt.Errorf("cannot specify intervals without from and to")
}
c, a, hash, err := buildConfig(r)
if err != nil {
return nil, err
}
ch := make(chan int)
errch := make(chan error, intervals)
resch := make(chan *ruleResult, intervals)
var wg sync.WaitGroup
diff := -from.Sub(to)
if intervals > 1 {
diff /= time.Duration(intervals - 1)
}
worker := func() {
wg.Add(1)
for interval := range ch {
t.Step(fmt.Sprintf("interval %v", interval), func(t miniprofiler.Timer) {
now := from.Add(diff * time.Duration(interval))
res, err := procRule(t, c, a, now, interval != 0, r.FormValue("email"), r.FormValue("template_group"), incidentID)
resch <- res
errch <- err
})
}
defer wg.Done()
}
for i := 0; i < 20; i++ {
go worker()
}
for i := 0; i < intervals; i++ {
ch <- i
}
close(ch)
wg.Wait()
close(errch)
close(resch)
type Result struct {
Group models.AlertKey
Result *models.Event
}
type Set struct {
Critical, Warning, Normal int
Time string
Results []*Result `json:",omitempty"`
}
type History struct {
Time, EndTime time.Time
Status string
}
type Histories struct {
History []*History
}
ret := struct {
Errors []string `json:",omitempty"`
Warnings []string `json:",omitempty"`
Sets []*Set
AlertHistory map[models.AlertKey]*Histories
*models.RenderedTemplates
Notifications map[string]*conf.PreparedNotifications
ActionNotifications map[string]map[string]*conf.PreparedNotifications
Data interface{} `json:",omitempty"`
Hash string
}{
AlertHistory: make(map[models.AlertKey]*Histories),
Hash: hash,
}
for err := range errch {
if err == nil {
continue
}
ret.Errors = append(ret.Errors, err.Error())
}
for res := range resch {
if res == nil {
continue
}
set := Set{
Critical: len(res.Criticals),
Warning: len(res.Warnings),
Normal: len(res.Normals),
Time: res.Time.Format(tsdbFormatSecs),
}
if res.Data != nil {
ret.RenderedTemplates = res.RenderedTemplates
ret.Notifications = res.Notifications
ret.ActionNotifications = res.ActionNotifications
ret.Data = res.Data
for k, v := range res.Result {
set.Results = append(set.Results, &Result{
Group: k,
Result: v,
})
}
slice.Sort(set.Results, func(i, j int) bool {
a := set.Results[i]
b := set.Results[j]
if a.Result.Status != b.Result.Status {
return a.Result.Status > b.Result.Status
}
return a.Group < b.Group
})
}
for k, v := range res.Result {
if ret.AlertHistory[k] == nil {
ret.AlertHistory[k] = new(Histories)
}
h := ret.AlertHistory[k]
h.History = append(h.History, &History{
Time: v.Time,
Status: v.Status.String(),
})
}
ret.Sets = append(ret.Sets, &set)
ret.Warnings = append(ret.Warnings, res.Warning...)
}
slice.Sort(ret.Sets, func(i, j int) bool {
return ret.Sets[i].Time < ret.Sets[j].Time
})
for _, histories := range ret.AlertHistory {
hist := histories.History
slice.Sort(hist, func(i, j int) bool {
return hist[i].Time.Before(hist[j].Time)
})
for i := 1; i < len(hist); i++ {
if i < len(hist)-1 && hist[i].Status == hist[i-1].Status {
hist = append(hist[:i], hist[i+1:]...)
i--
}
}
for i, h := range hist[:len(hist)-1] {
h.EndTime = hist[i+1].Time
}
histories.History = hist[:len(hist)-1]
}
return &ret, nil
}
func buildConfig(r *http.Request) (c conf.RuleConfProvider, a *conf.Alert, hash string, err error) {
config, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, nil, "", err
}
c, err = rule.NewConf("Test Config", schedule.SystemConf.EnabledBackends(), schedule.SystemConf.GetRuleVars(), string(config))
if err != nil {
return nil, nil, "", err
}
hash, err = sched.DefaultSched.DataAccess.Configs().SaveTempConfig(string(config))
if err != nil {
return nil, nil, "", err
}
alertName := r.FormValue("alert")
if alertName == "" {
return nil, nil, "", fmt.Errorf("must supply alert to run")
}
a = c.GetAlert(alertName)
if a == nil {
return nil, nil, "", fmt.Errorf("alert %s not found", alertName)
}
return c, a, hash, nil
}