-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.go
91 lines (79 loc) · 2.69 KB
/
backend.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
package main
// Backend represents a backend server with its possible alarms and connected to a database.
type Backend struct {
Server
// ProcAlarm is True if the backend process is not running
ProcAlarm AlarmStatus
// DBConnectionAlarm is True if the database is not working
DBConnectionAlarm AlarmStatus
DBEngine *Database
// dnsAvailable used to store the state of the DNS server the last time CheckAlarms was called.
dnsAvailable bool
}
// NewBackend create a new backend server, start it and return the pointer to it
func NewBackend(name string, connectedDB *Database, mon MonitorSystem) *Backend {
return &Backend{
Server: Server{
Name: name,
mon: mon,
},
DBEngine: connectedDB,
}
}
func (b *Backend) GetName() string {
return b.Name
}
func (s *Backend) GetAlarms() []string {
serverAlarms := s.Server.GetAlarms()
return append(serverAlarms, []string{"Proc", "DBConnection"}...)
}
func (s *Backend) GetType() string {
return string(BackendNode)
}
// CheckAlarms print a message if the server has alarms.
// It check alarms specific to the backend, plus generic alarms for the server
// and also generate an alarm if the database is not available.
func (b *Backend) CheckAlarms(t float64) {
if b.ProcAlarm == AlarmTriggered {
b.ProcAlarm = AlarmACK
b.mon.handleAlarm(b.Name, "Proc", t)
}
// Set the local db connection alarm based on the state of the database.
// Generate a new alarm if we are moving from enabled to triggered.
if b.DBEngine.Available() {
b.DBConnectionAlarm = AlarmEnabled
} else {
if b.DBConnectionAlarm == AlarmEnabled {
b.DBConnectionAlarm = AlarmACK
b.mon.handleAlarm(b.Name, "DBConnection", t)
}
}
// If DNS server is not available, backend could not communicate with the database,
// so we also trigger the DBConnection alarm.
if b.Server.DNSAlarm == AlarmTriggered && b.DBConnectionAlarm == AlarmEnabled {
b.DBConnectionAlarm = AlarmACK
b.dnsAvailable = false
b.mon.handleAlarm(b.Name, "DBConnection", t)
}
// If DNS was unavailable and is now available, we can clear the DBConnection alarm.
if !b.dnsAvailable && b.Server.DNSAlarm == AlarmEnabled && b.DBConnectionAlarm == AlarmACK {
b.dnsAvailable = true
b.DBConnectionAlarm = AlarmEnabled
}
b.Server.CheckAlarms(t)
}
// Available returns true if the backend server is considered available, that is,
// if the backend process is running and the database is available.
func (b *Backend) Available() bool {
return b.Server.Available() && b.ProcAlarm == AlarmEnabled
}
func (b *Backend) SetAlarm(alarm string, status AlarmStatus) {
switch alarm {
case "Proc":
b.ProcAlarm = status
case "DBConnection":
b.DBConnectionAlarm = status
default:
b.Server.SetAlarm(alarm, status)
}
}