-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmonitor.go
175 lines (145 loc) · 4.75 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
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
package gossm
import (
"fmt"
"os"
"time"
"github.com/ssimunic/gossm/dial"
"github.com/ssimunic/gossm/logger"
"github.com/ssimunic/gossm/notify"
"github.com/ssimunic/gossm/track"
)
type Monitor struct {
// Holds settings and servers
config *Config
// Channel used to schedule checks for servers
checkerCh chan *Server
// Notification methods used to send messages when server can't be reached
notifiers notify.Notifiers
// Channel used for receive servers that couldn't be reached
notifierCh chan *Server
// To reduce notification spam, tracker is used to delay notifications
notificationTracker map[*Server]*track.TimeTracker
// Used to test connections
dialer *dial.Dialer
// Sending to stop channel makes program exit
stop chan struct{}
// TODO: For each server, keep map with time and up/down status
serverStatusData *ServerStatusData
}
func NewMonitor(c *Config) *Monitor {
m := &Monitor{
config: c,
checkerCh: make(chan *Server),
notifiers: c.Settings.Notifications.GetNotifiers(),
notifierCh: make(chan *Server),
notificationTracker: make(map[*Server]*track.TimeTracker),
dialer: dial.NewDialer(c.Settings.Monitor.MaxConnections),
stop: make(chan struct{}),
serverStatusData: NewServerStatusData(c.Servers),
}
m.initialize()
return m
}
func (m *Monitor) initialize() {
// Initialize notification methods to reduce overhead
for _, notifier := range m.notifiers {
if initializer, ok := notifier.(notify.Initializer); ok {
logger.Logln("Initializing", initializer)
initializer.Initialize()
}
}
for _, server := range m.config.Servers {
// Initialize notificationTracker
m.notificationTracker[server] = NewTrackerWithExpBackoff(m.config.Settings.Monitor.ExponentialBackoffSeconds)
// Set default CheckInterval and Timeout for servers who miss them
switch {
case server.CheckInterval <= 0:
server.CheckInterval = m.config.Settings.Monitor.CheckInterval
case server.Timeout <= 0:
server.Timeout = m.config.Settings.Monitor.Timeout
}
}
}
// NewTrackerWithExpBackoff creates TimeTracker with ExpBackoff as Delayer
func NewTrackerWithExpBackoff(expBackoffSeconds int) *track.TimeTracker {
return track.NewTracker(track.NewExpBackoff(expBackoffSeconds))
}
// Run runs monitor infinitely
func (m *Monitor) Run() {
m.RunForSeconds(0)
}
// RunForSeconds runs monitor for runningSeconds seconds or infinitely if 0 is passed as an argument
func (m *Monitor) RunForSeconds(runningSeconds int) {
if runningSeconds != 0 {
go func() {
runningSecondsTime := time.Duration(runningSeconds) * time.Second
<-time.After(runningSecondsTime)
m.stop <- struct{}{}
}()
}
for _, server := range m.config.Servers {
go m.scheduleServer(server)
}
logger.Logln("Starting monitor.")
m.monitor()
}
func (m *Monitor) scheduleServer(s *Server) {
// Initial
m.checkerCh <- s
// Periodic
tickerSeconds := time.NewTicker(time.Duration(s.CheckInterval) * time.Second)
for range tickerSeconds.C {
m.checkerCh <- s
}
}
func (m *Monitor) monitor() {
go m.listenForChecks()
go m.listenForNotifications()
// Wait for termination signal then exit monitor
<-m.stop
logger.Logln("Terminating.")
os.Exit(0)
}
func (m *Monitor) listenForChecks() {
for server := range m.checkerCh {
m.checkServerStatus(server)
}
}
func (m *Monitor) listenForNotifications() {
for server := range m.notifierCh {
timeTracker := m.notificationTracker[server]
if timeTracker.IsReady() {
nextDelay, nextTime := timeTracker.SetNext()
logger.Logln("Sending notifications for", server)
go m.notifiers.NotifyAll(fmt.Sprintf("%s (%s)", server.Name, server))
logger.Logln("Next available notification for", server.String(), "in", nextDelay, "at", nextTime)
}
}
}
func (m *Monitor) checkServerStatus(server *Server) {
// NewWorker() blocks if there aren't free slots in dialer for concurrency
worker, output := m.dialer.NewWorker()
go func() {
logger.Logln("Checking", server)
formattedAddress := fmt.Sprintf("%s:%d", server.IPAddress, server.Port)
timeoutSeconds := time.Duration(server.Timeout) * time.Second
worker <- dial.NetAddressTimeout{NetAddress: dial.NetAddress{Network: server.Protocol, Address: formattedAddress}, Timeout: timeoutSeconds}
dialerStatus := <-output
m.serverStatusData.SetStatusAtTimeForServer(server, time.Now(), dialerStatus.Ok)
// Handle error
if !dialerStatus.Ok {
logger.Logln(dialerStatus.Err)
logger.Logln("ERROR", server)
go func() {
m.notifierCh <- server
}()
return
}
// Handle success
logger.Logln("OK", server)
// Reset time tracker for server
if m.notificationTracker[server].HasBeenRan() {
m.notificationTracker[server] = NewTrackerWithExpBackoff(m.config.Settings.Monitor.ExponentialBackoffSeconds)
}
}()
}