-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
143 lines (124 loc) · 2.9 KB
/
monitor.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// +build metrics
package beaconpi
import (
"bytes"
"fmt"
log "github.com/sirupsen/logrus"
"gopkg.in/gomail.v2"
"time"
)
const (
MAX_MONITOR_MSG = 4096
TIMEOUT_EDGE_SYNC = time.Second * 10
TIMEOUT_SEND = time.Second * 600
EDGE_SYNC_MAX = 10.0
)
type monitor struct {
msgqueue chan string
}
var m monitor
func startMonitor() {
m.msgqueue = make(chan string, 1024)
}
func sendInfo(msg string) {
if len(msg) > MAX_MONITOR_MSG {
log.Infof("Length of msg was %d which was too large %d is the max msg: %s", len(msg), MAX_MONITOR_MSG, msg)
return
}
m.msgqueue <- msg
}
func sendWarning(msg string) {
if len(msg) > MAX_MONITOR_MSG {
log.Infof("Length of msg was %d which was too large %d is the max msg: %s", len(msg), MAX_MONITOR_MSG, msg)
return
}
m.msgqueue <- msg
}
func sendQueue() {
log.Info("Sending Message Queue")
//log.Debugf("mp: %v", mp)
d := gomail.NewDialer(mp.SMTPHost, mp.SMTPPort, mp.SMTPUser, mp.SMTPPassphrase)
msg := gomail.NewMessage()
msg.SetHeader("From", mp.SMTPUser)
msg.SetHeader("To", mp.MonitorEmail)
msg.SetHeader("Subject", "Beaconpi Monitoring Service "+time.Now().Format(time.RFC3339))
var buff bytes.Buffer
// Drain until empty
buff.WriteString("Messages: <br><ol>")
count := 0
drain:
for {
select {
case t := <-m.msgqueue:
count += 1
buff.WriteString(fmt.Sprintf("<li>%s</li>\n", t))
default:
break drain
}
}
buff.WriteString("</ol>")
msg.SetBody("text/html", buff.String())
if count > 0 {
log.Info("Sending message")
err := d.DialAndSend(msg)
log.Info("Sent message")
if err != nil {
log.Error("Failed to send message with", err)
}
}
}
func intSliceEqual(l []int, r []int) bool {
if len(l) != len(r) {
return false
}
for i, _ := range l {
if l[i] != r[i] {
return false
}
}
return true
}
func metricsBackgroundTasks() {
startMonitor()
tickES := time.Tick(TIMEOUT_EDGE_SYNC)
tickSend := time.Tick(TIMEOUT_SEND)
var lastid int
var msg []string
sendInfo("Server Started")
sendQueue()
inactEdge, err := changedActiveEdges()
if err != nil {
log.Warnf("getInactiveEdges failed to fetch %s", err)
}
for {
select {
case _ = <-tickES:
dbconfig := dbHandler{mp.DriverName, mp.DataSourceName}
db, err := dbconfig.openDB()
if err == nil {
msg, lastid, err = dbGetErrorsSince(lastid, db)
if err != nil {
log.Warnf("Error getting db errors %s", err)
}
for _, v := range msg {
sendWarning(v)
}
db.Close()
} else {
log.Warnf("Failed to open DB %s", err)
}
// check inactive edges
newedges, err := changedActiveEdges()
if err != nil {
log.Warnf("getInactiveEdges failed to fetch %s", err)
} else {
if !intSliceEqual(newedges, inactEdge) {
sendWarning(fmt.Sprintf("Inactive edges changed from %#v to %#v at %s", inactEdge, newedges, time.Now().Format(time.RFC3339)))
inactEdge = newedges
}
}
case _ = <-tickSend:
sendQueue()
}
}
}