forked from Cloud-Foundations/Dominator
/
notifier.go
96 lines (89 loc) · 2.5 KB
/
notifier.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
package hypervisors
import (
"bytes"
"flag"
"fmt"
"net/smtp"
"time"
)
var (
emailDomain = flag.String("emailDomain", "",
"Email domain to sent notifications to")
smtpServer = flag.String("smtpServer", "", "Address of SMTP server")
)
func sendEmail(user string, vms []*vmInfoType) error {
fromAddress := "DoNotReply@" + *emailDomain
toAddress := user + "@" + *emailDomain
buffer := &bytes.Buffer{}
fmt.Fprintf(buffer, "From: %s\n", fromAddress)
fmt.Fprintf(buffer, "To: %s\n", toAddress)
fmt.Fprintln(buffer, "Subject: Please migrate your VMs")
fmt.Fprintln(buffer)
fmt.Fprintln(buffer,
"You own the following VMs which are on unhealthy Hypervisors.")
fmt.Fprintln(buffer,
"Please migrate your VMs to healthy Hypervisors ASAP.")
fmt.Fprintln(buffer, "Below is the list of your VMs which are affected:")
fmt.Fprintln(buffer)
for _, vm := range vms {
fmt.Fprintf(buffer, "IP: %s name: %s Hypervisor: %s status: %s\n",
vm.Address.IpAddress, vm.Tags["Name"],
vm.hypervisor.machine.Hostname, vm.hypervisor.getHealthStatus())
}
return smtp.SendMail(*smtpServer, nil, fromAddress, []string{toAddress},
buffer.Bytes())
}
func (h *hypervisorType) addVmsToMap(vmsPerOwner map[string][]*vmInfoType) {
h.mutex.RLock()
defer h.mutex.RUnlock()
for _, vm := range h.vms {
for _, owner := range vm.OwnerUsers {
vmsPerOwner[owner] = append(vmsPerOwner[owner], vm)
}
}
}
func (m *Manager) getBadHypervisors() []*hypervisorType {
m.mutex.RLock()
defer m.mutex.RUnlock()
badHypervisors := make([]*hypervisorType, 0)
for _, hypervisor := range m.hypervisors {
switch hypervisor.probeStatus {
case probeStatusNotYetProbed:
continue
case probeStatusConnected:
if hypervisor.healthStatus == "" {
continue
}
if hypervisor.healthStatus == "healthy" {
continue
}
badHypervisors = append(badHypervisors, hypervisor)
default:
badHypervisors = append(badHypervisors, hypervisor)
}
}
return badHypervisors
}
func (m *Manager) notifierLoop() {
if *emailDomain == "" || *smtpServer == "" {
return
}
for time.Sleep(time.Minute); ; time.Sleep(time.Hour * 48) {
m.notify()
}
}
func (m *Manager) notify() {
badHypervisors := m.getBadHypervisors()
if len(badHypervisors) < 1 {
return
}
vmsPerOwner := make(map[string][]*vmInfoType)
for _, hypervisor := range badHypervisors {
hypervisor.addVmsToMap(vmsPerOwner)
}
for user, vms := range vmsPerOwner {
if err := sendEmail(user, vms); err != nil {
m.logger.Printf("error sending email for %s: %s\n", user, err)
}
}
}