forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
103 lines (90 loc) · 2.08 KB
/
events.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
package core
import (
"fmt"
"github.com/fatih/structs"
"github.com/hunterlong/statup/notifications"
"github.com/hunterlong/statup/plugin"
"github.com/hunterlong/statup/types"
"upper.io/db.v3/lib/sqlbuilder"
)
func OnLoad(db sqlbuilder.Database) {
for _, p := range CoreApp.AllPlugins {
p.OnLoad(db)
}
}
func OnSuccess(s *Service) {
for _, p := range CoreApp.AllPlugins {
p.OnSuccess(structs.Map(s))
}
}
func OnFailure(s *Service, f FailureData) {
for _, p := range CoreApp.AllPlugins {
p.OnFailure(structs.Map(s))
}
if notifications.SlackComm != nil {
onFailureSlack(s, f)
}
if notifications.EmailComm != nil {
onFailureEmail(s, f)
}
}
func onFailureSlack(s *Service, f FailureData) {
slack := SelectCommunication(2)
if slack.Enabled {
msg := fmt.Sprintf("Service %v is currently offline! Issue: %v", s.Name, f.Issue)
notifications.SendSlack(msg)
}
}
type failedEmail struct {
Service *Service
FailureData FailureData
Domain string
}
func onFailureEmail(s *Service, f FailureData) {
email := SelectCommunication(1)
if email.Enabled {
data := failedEmail{s, f, CoreApp.Domain}
admin, _ := SelectUser(1)
email := &types.Email{
To: admin.Email,
Subject: fmt.Sprintf("Service %v is Down", s.Name),
Template: "failure.html",
Data: data,
From: email.Var1,
}
notifications.SendEmail(EmailBox, email)
}
}
func OnSettingsSaved(c *Core) {
for _, p := range CoreApp.AllPlugins {
p.OnSettingsSaved(structs.Map(c))
}
}
func OnNewUser(u *User) {
for _, p := range CoreApp.AllPlugins {
p.OnNewUser(structs.Map(u))
}
}
func OnNewService(s *Service) {
for _, p := range CoreApp.AllPlugins {
p.OnNewService(structs.Map(s))
}
}
func OnDeletedService(s *Service) {
for _, p := range CoreApp.AllPlugins {
p.OnDeletedService(structs.Map(s))
}
}
func OnUpdateService(s *Service) {
for _, p := range CoreApp.AllPlugins {
p.OnUpdatedService(structs.Map(s))
}
}
func SelectPlugin(name string) plugin.PluginActions {
for _, p := range CoreApp.AllPlugins {
if p.GetInfo().Name == name {
return p
}
}
return plugin.PluginInfo{}
}