-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmega_notifier.go
More file actions
105 lines (88 loc) · 2.73 KB
/
mega_notifier.go
File metadata and controls
105 lines (88 loc) · 2.73 KB
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
package main
import (
"errors"
"fmt"
"github.com/kevinburke/twilio-go"
"log"
"net/smtp"
)
//Notification dispatcher
func Notify(server Server, contacts []Contact, mailcfg MailSettings, smsinfo TwilioInfo) {
if contacts != nil {
for _, contact := range contacts {
if inArr(contact.Watching, server.ID) && contact.Threshold > (server.Uptime*100) {
var err error
messageId := fmt.Sprintf(TwFormat, server.ID, DownMsg)
safetoAlert := ShouldAlert(messageId)
if !safetoAlert {
return
}
if contact.Email != EmptyString {
err = SendEmail(fmt.Sprintf(DownSub, server.Host), fmt.Sprintf(DownMsg, contact.Nickname, server.Nickname, server.Host, contact.Threshold), contact.Email, mailcfg)
if err != nil {
log.Println(err)
}
}
if contact.Phone != EmptyString {
err = contact.SendSMS(fmt.Sprintf(DownMsg, contact.Nickname, server.Nickname, server.Host, contact.Threshold), smsinfo)
if err != nil {
log.Println(err)
}
}
if err != nil {
RemoveWithID(server.ID)
}
}
}
}
}
// Func used to notify users on kubernetes' stats
func NotifyPodContacts(pod PodConfig, contacts []Contact, mailcfg MailSettings, smsinfo TwilioInfo, message string) {
messageId := fmt.Sprintf(TwFormat, pod.Name, message)
safetoAlert := ShouldAlert(messageId)
if !safetoAlert {
return
}
if contacts != nil {
for _, contact := range contacts {
if inArr(contact.Pods, pod.Name) {
var err error
if contact.Email != EmptyString {
err := SendEmail(fmt.Sprintf(DownSubk8s, pod.Name), fmt.Sprintf(DownMsgk8s, contact.Nickname, pod.Name, message), contact.Email, mailcfg)
if err != nil {
log.Println(err)
}
}
if contact.Phone != EmptyString {
err := contact.SendSMS(fmt.Sprintf(DownMsgk8s, contact.Nickname, pod.Name, message), smsinfo)
if err != nil {
log.Println(err)
}
}
if err != nil {
RemoveWithID(pod.Name)
}
}
}
}
}
// Send email to contact
func SendEmail(subject, body, to string, mail MailSettings) error {
if mail.Host == "" || mail.Email == "" || mail.Password == "" || mail.Port == "" {
return errors.New(SMTPNoSettingsError)
}
msg := fmt.Sprintf(SMTPMessage, mail.Email, to, subject, body)
err := smtp.SendMail(fmt.Sprintf(SMTPAddress, mail.Host, mail.Port),
smtp.PlainAuth(EmptyString, mail.Email, mail.Password, mail.Host),
mail.Email, []string{to}, []byte(msg))
if err != nil {
return err
}
return nil
}
// Send sms to contact
func (user Contact) SendSMS(message string, smsinfo TwilioInfo) error {
client := twilio.NewClient(smsinfo.SID, smsinfo.Token, nil)
_, err := client.Messages.SendMessage(smsinfo.From, fmt.Sprintf(TwFormat, smsinfo.CountryCode, user.Phone), message, nil)
return err
}