-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalert.go
90 lines (80 loc) · 3.23 KB
/
alert.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
package gobearmon
import "github.com/sfreiberg/gotwilio"
import "fmt"
import "net/http"
import "net/url"
import "database/sql"
import "strconv"
type AlertFunc func(string, *Check, *CheckResult, *sql.DB) error
var alertFuncs map[string]AlertFunc
func DoAlert(alert *Alert, check *Check, result *CheckResult, db *sql.DB) error {
if alertFuncs == nil {
alertInit()
}
f := alertFuncs[alert.Type]
if f == nil {
return fmt.Errorf("alert type %s does not exist", alert.Type)
} else {
debugPrintf("executing alert %s/%s for check [%s]", alert.Type, alert.Data, check.Name)
return f(alert.Data, check, result, db)
}
}
func alertInit() {
alertFuncs = make(map[string]AlertFunc)
alertFuncs["email"] = func(data string, check *Check, result *CheckResult, db *sql.DB) error {
subject := fmt.Sprintf("Check %s: %s", result.Status, check.Name)
var body string
if result.Status == StatusOnline {
body = fmt.Sprintf("Check [%s] is now online.", check.Name)
} else {
body = fmt.Sprintf("Check [%s] is now %s: %s", check.Name, result.Status, result.Message)
}
body += fmt.Sprintf("\n\nID: %d\nName: %s\nType: %s\nData: %s\n\ngobearmon", check.Id, check.Name, check.Type, check.Data)
return mail(subject, body, data)
}
alertFuncs["http"] = func(data string, check *Check, result *CheckResult, db *sql.DB) error {
resp, err := http.PostForm(data, url.Values{
"check_id": {strconv.Itoa(int(check.Id))},
"name": {check.Name},
"type": {check.Type},
"data": {check.Data},
"status": {string(result.Status)},
"message": {result.Message},
})
if err != nil {
return err
}
resp.Body.Close()
return nil
}
alertFuncs["sms"] = func(data string, check *Check, result *CheckResult, db *sql.DB) error {
var message string
if result.Status == StatusOnline {
message = fmt.Sprintf("Check [%s] is now online.", check.Name)
} else {
message = fmt.Sprintf("Check [%s] is now %s: %s", check.Name, result.Status, result.Message)
}
twilio := gotwilio.NewTwilioClient(cfg.Twilio.AccountSid, cfg.Twilio.AuthToken)
resp, exception, err := twilio.SendSMS(cfg.Twilio.From, data, message, "", "")
if err != nil {
return err
} else if exception != nil {
return fmt.Errorf("error(%d/%d): %s (%s)", exception.Status, exception.Code, exception.Message, exception.MoreInfo)
}
db.Exec("INSERT INTO charges (check_id, type, data) VALUES (?, ?, ?)", check.Id, "sms", resp.Sid)
return nil
}
alertFuncs["voice"] = func(data string, check *Check, result *CheckResult, db *sql.DB) error {
message := fmt.Sprintf("This is a monitoring-related call from go bear mon . The check . %s . has been recorded . %s . Reason is . %s", check.Name, result.Status, result.Message)
twilio := gotwilio.NewTwilioClient(cfg.Twilio.AccountSid, cfg.Twilio.AuthToken)
params := gotwilio.NewCallbackParameters("http://twimlets.com/message?Message=" + url.QueryEscape(message))
resp, exception, err := twilio.CallWithUrlCallbacks(cfg.Twilio.From, data, params)
if err != nil {
return err
} else if exception != nil {
return fmt.Errorf("error(%d/%d): %s (%s)", exception.Status, exception.Code, exception.Message, exception.MoreInfo)
}
db.Exec("INSERT INTO charges (check_id, type, data) VALUES (?, ?, ?)", check.Id, "voice", resp.Sid)
return nil
}
}