-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller.go
319 lines (279 loc) · 8.25 KB
/
controller.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package gobearmon
import "bufio"
import "database/sql"
import "encoding/json"
import "errors"
import "fmt"
import "log"
import "math/rand"
import "net"
import "strings"
import "sync"
import "time"
type Controller struct {
Addr string
Databases []*sql.DB
Confirmations int
mu sync.Mutex
checks map[CheckId]*Check
reloadErrorCount int
}
func (this *Controller) Start() {
this.checks = make(map[CheckId]*Check)
ln, err := net.Listen("tcp", this.Addr)
if err != nil {
panic(err)
}
go func() {
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("controller: error while accepting connection: %s", err.Error())
continue
}
log.Printf("controller: new connection from %s", conn.RemoteAddr().String())
go this.handle(conn)
}
}()
go func() {
for {
this.reload()
time.Sleep(time.Minute)
}
}()
}
func (this *Controller) GetCheck(checkId CheckId) *Check {
this.mu.Lock()
defer this.mu.Unlock()
return this.checks[checkId]
}
func (this *Controller) randomDB() *sql.DB {
return this.Databases[rand.Intn(len(this.Databases))]
}
func (this *Controller) handle(conn net.Conn) {
defer conn.Close()
in := bufio.NewReader(conn)
conn.SetReadDeadline(time.Now().Add(5 * time.Second))
password, err := in.ReadString('\n')
if err != nil || len(password) == 0 || password[:len(password) - 1] != cfg.Default.Password {
log.Printf("controller: terminating connection from %s due to incorrect password", conn.RemoteAddr().String())
return
}
for {
conn.SetReadDeadline(time.Now().Add(5 * time.Minute))
line, err := in.ReadString('\n')
if err != nil {
log.Printf("controller: worker at %s disconnected: %s", conn.RemoteAddr().String(), err.Error())
break
}
request := MakeControllerRequest()
err = json.Unmarshal([]byte(strings.TrimSpace(line)), request)
if err != nil {
log.Printf("controller: invalid request from %s: %s", conn.RemoteAddr().String(), err.Error())
break
}
response := this.request(conn.RemoteAddr().String(), request)
bytes, err := json.Marshal(response)
if err != nil {
panic(err)
}
conn.Write([]byte(string(bytes) + "\n"))
}
}
func (this *Controller) request(requestor string, request *ControllerRequest) *ControllerResponse {
this.mu.Lock()
defer this.mu.Unlock()
for checkId, checkResult := range request.Results {
check := this.checks[checkId]
if check == nil {
// probably got deleted from database during checking
continue
} else if requestor != check.Lock {
continue
}
check.Lock = ""
check.LastTime = time.Now()
check.LastWorker = requestor
if checkResult.Status != StatusOnline && checkResult.Status != StatusOffline {
continue
}
if checkResult.Status != check.Status {
check.TurnSet[requestor] = true
if len(check.TurnSet) >= this.Confirmations {
for id := range check.TurnSet {
delete(check.TurnSet, id)
}
check.TurnCount++
debugPrintf("check [%s]: turn count incremented to %d/%d", check.Name, check.TurnCount, check.Delay + 1)
if check.TurnCount > check.Delay {
check.Status = checkResult.Status
check.LastStatusChange = time.Now()
log.Printf("status of check %s changed to %s", check.Name, check.Status)
go this.reportAndUpdate(check, checkResult)
}
}
} else {
check.TurnCount = 0
for id := range check.TurnSet {
delete(check.TurnSet, id)
}
}
}
var response ControllerResponse
for checkId, check := range this.checks {
if len(response.Checks) >= request.Count {
break
} else if check.Lock != "" {
continue
}
assign := false
if len(check.TurnSet) > 0 {
if !check.TurnSet[requestor] {
assign = true
}
} else if time.Now().After(check.LastTime.Add(time.Duration(check.Interval) * time.Second)) && check.LastWorker != requestor {
assign = true
}
if assign {
check.Lock = requestor
check.LockTime = time.Now()
response.Checks = append(response.Checks, checkId)
}
}
return &response
}
func (this *Controller) reportAndUpdate(check *Check, result *CheckResult) {
// attempt reporting
// if we succeed, then update the database
// if we fail, then reset the check status
success := retry(func() error {
return this.report(check, result)
}, 10)
if success {
retry(func() error {
_, err := this.Databases[rand.Intn(len(this.Databases))].Exec("UPDATE checks SET status = ? WHERE id = ?", string(result.Status), check.Id)
return err
}, 10)
retry(func() error {
_, err := this.Databases[rand.Intn(len(this.Databases))].Exec("INSERT INTO check_events (check_id, type) VALUES (?, ?)", check.Id, string(result.Status))
return err
}, 10)
} else {
this.mu.Lock()
if result.Status == StatusOnline {
check.Status = StatusOffline
} else if result.Status == StatusOffline {
check.Status = StatusOnline
}
this.mu.Unlock()
}
}
func (this *Controller) report(check *Check, result *CheckResult) error {
rows, err := this.randomDB().Query("SELECT contacts.type, contacts.data FROM contacts, alerts WHERE alerts.check_id = ? AND alerts.contact_id = contacts.id AND (alerts.type = 'both' OR alerts.type = ?) AND alerts.enabled = 1", check.Id, string(result.Status))
if err != nil {
return errors.New("database query failed")
}
var alerts []*Alert
for rows.Next() {
var alert Alert
err := rows.Scan(&alert.Type, &alert.Data)
if err != nil {
return errors.New("database query failed")
}
alerts = append(alerts, &alert)
}
if len(alerts) == 0 {
return nil
}
// we iterate over the contacts and alert each one
// if at least one succeeds, we report success to caller to avoid duplicate alerting
// we retry the ones that failed after some delay; only one retry is attempted
atLeastOneSuccess := false
var failedAlerts []*Alert
for _, alert := range alerts {
err := DoAlert(alert, check, result, this.randomDB())
if err == nil {
atLeastOneSuccess = true
} else {
failedAlerts = append(failedAlerts, alert)
debugPrintf("failed to alert %s/%s: %s (trying again later)", alert.Type, alert.Data, err.Error())
}
}
if !atLeastOneSuccess {
return errors.New("all alerts failed")
}
if len(failedAlerts) > 0 {
go func() {
time.Sleep(30 * time.Second)
for _, alert := range failedAlerts {
err := DoAlert(alert, check, result, this.randomDB())
if err != nil {
log.Printf("permanently failed to alert %s/%s: %s", alert.Type, alert.Data, err.Error())
}
}
}()
}
return nil
}
func (this *Controller) incrementReloadError() {
var shouldNotify bool
this.mu.Lock()
this.reloadErrorCount++
shouldNotify = this.reloadErrorCount >= 10 && this.reloadErrorCount % 10 == 0
this.mu.Unlock()
if shouldNotify {
mailAdmin("gobearmon: successive reload errors", fmt.Sprintf("Got %d successive reload errors!", this.reloadErrorCount))
}
}
func (this *Controller) reload() {
db := this.randomDB()
rows, err := db.Query("SELECT id, name, type, data, check_interval, delay, status FROM checks WHERE enabled = 1")
if err != nil {
log.Printf("controller: reload error on query: %s", err.Error())
this.incrementReloadError()
return
}
var dbChecks []*Check
existCheckIds := make(map[CheckId]bool)
for rows.Next() {
check := MakeCheck()
var statusString string
err := rows.Scan(&check.Id, &check.Name, &check.Type, &check.Data, &check.Interval, &check.Delay, &statusString)
check.SetStatusFromString(statusString)
if err != nil {
log.Printf("controller: reload error on scan: %s", err.Error())
this.incrementReloadError()
return
}
existCheckIds[check.Id] = true
dbChecks = append(dbChecks, check)
}
this.mu.Lock()
defer this.mu.Unlock()
this.reloadErrorCount = 0
// insert/update
for _, dbCheck := range dbChecks {
check := this.checks[dbCheck.Id]
if check == nil {
this.checks[dbCheck.Id] = dbCheck
} else {
check.Name = dbCheck.Name
check.Type = dbCheck.Type
check.Data = dbCheck.Data
check.Interval = dbCheck.Interval
check.Delay = dbCheck.Delay
// only copy status if we didn't update the status recently
if time.Now().After(check.LastStatusChange.Add(10 * time.Minute)) {
check.Status = dbCheck.Status
}
}
}
// delete; also hijack to remove locks
for checkId, check := range this.checks {
if !existCheckIds[checkId] {
delete(this.checks, checkId)
} else if check.Lock != "" && time.Now().After(check.LockTime.Add(2 * time.Minute)) {
check.Lock = ""
}
}
}