-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathalerts.go
108 lines (92 loc) · 2.74 KB
/
alerts.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
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"math"
"os"
"time"
"github.com/fatih/color"
)
func (alert *Alert) ApplyFunction(values []float64) float64 {
var appliedFunction float64
if len(values) > 0 {
appliedFunction = values[0]
}
if alert.Function == "average" {
for _, i := range values {
appliedFunction += float64(i)
}
appliedFunction = appliedFunction / float64(len(values))
} else if alert.Function == "max" {
for _, i := range values {
appliedFunction = math.Max(appliedFunction, i)
}
} else if alert.Function == "min" {
for _, i := range values {
appliedFunction = math.Min(appliedFunction, i)
}
}
return appliedFunction
}
func (alert *Alert) Setup() {
hash := md5.Sum([]byte(alert.Name))
alert.Hash = hex.EncodeToString(hash[:])
for _, n := range alert.NotifiersRaw {
alert.Notifiers = append(alert.Notifiers, Notifier{Name: n})
}
}
func (alert *Alert) Run() {
if os.Getenv("DEBUG") == "true" {
fmt.Println("Query: ", fmt.Sprintf("%s limit %d", alert.Query, alert.Limit))
}
groupByQuery := ""
if len(alert.GroupBy) > 0 {
groupByQuery = fmt.Sprintf("GROUP BY time(%s)", alert.GroupBy)
}
values := query(fmt.Sprintf("%s where time > now() - %s %s limit %d",
alert.Query, alert.Timeshift, groupByQuery, alert.Limit))
applied_function := alert.ApplyFunction(values)
if os.Getenv("DEBUG") == "true" {
fmt.Println("Applied Func: ", applied_function)
}
alert_triggered := false
switch alert.Trigger.Operator {
case "gt":
alert_triggered = applied_function > float64(alert.Trigger.Value)
case "lt":
alert_triggered = applied_function < float64(alert.Trigger.Value)
}
if alert_triggered {
message := fmt.Sprintf("*[!] %s triggered!* Value: %.2f | Trigger: %s %d",
alert.Name, applied_function, alert.Trigger.Operator, alert.Trigger.Value)
color.Red(message)
alertAlreadyTriggered := false
tMutex.Lock()
if v, ok := triggeredAlerts[alert.Hash]; ok {
color.Yellow(fmt.Sprintf("[already triggered at %s] %s", v.TriggeredAt, message))
alertAlreadyTriggered = true
} else {
triggeredAlerts[alert.Hash] = TriggeredAlert{Hash: alert.Hash, TriggeredAt: time.Now()}
}
tMutex.Unlock()
if !alertAlreadyTriggered {
for _, n := range alert.Notifiers {
n.Run(message, true)
}
}
} else {
tMutex.Lock()
if _, ok := triggeredAlerts[alert.Hash]; ok {
delete(triggeredAlerts, alert.Hash)
message := fmt.Sprintf("*[+] %s resolved * Value: %.2f | Trigger: %s %d",
alert.Name, applied_function, alert.Trigger.Operator, alert.Trigger.Value)
for _, n := range alert.Notifiers {
n.Run(message, false)
}
color.Green("[+] %s - Alert resolved.", alert.Name)
}
tMutex.Unlock()
color.Green(fmt.Sprintf("[+] %s passed. (%.2f)", alert.Name, applied_function))
}
}