-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.go
282 lines (241 loc) · 6.64 KB
/
server.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"net/http/cookiejar"
"os"
"path/filepath"
"runtime"
"time"
)
var (
slackTemplate Slack
checkRequestChannel chan *Check
checkConfigs []Check
)
// Slack config
type Slack struct {
URL string `json:"url"`
Username string `json:"username"`
Icon string `json:"icon_emoji"`
Channel string `json:"channel"`
Text string `json:"text"`
}
// Check is a struct representing info for an http checker
type Check struct {
ID string `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Description string `json:"description"`
Method string `json:"method"`
Interval int64 `json:"interval"`
Timeout int64 `json:"timeout"`
// results
Timestamp time.Time `json:"timestamp"`
StatusCode int `json:"statusCode"`
ResponseTime int64 `json:"responseTime"`
Error string `json:"error"`
PreviousOK bool `json:"previousOk"`
}
// Config is a struct representing data for checkers
type Config struct {
Slack Slack `json:"slack"`
Checks []Check `json:"checks"`
}
// Broker tracks attached clients and broadcasts events to those clients.
type Broker struct {
clients map[chan *Check]bool
newClients chan chan *Check
oldClients chan chan *Check
checkResult chan *Check
}
// Start creates a new goroutine, handling addition & removal of clients, and
// broadcasting of checkResult out to clients that are currently attached.
func (broker *Broker) Start() {
go func() {
for {
select {
case sendChannel := <-broker.newClients:
broker.clients[sendChannel] = true
case sendChannel := <-broker.oldClients:
delete(broker.clients, sendChannel)
close(sendChannel)
case check := <-broker.checkResult:
for sendChannel := range broker.clients {
sendChannel <- check
}
}
}
}()
}
func (broker *Broker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
return
}
checkResult := make(chan *Check)
broker.newClients <- checkResult
notify := w.(http.CloseNotifier).CloseNotify()
go func() {
<-notify
broker.oldClients <- checkResult
}()
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
for {
check, open := <-checkResult
if !open {
// disconnected client
break
}
stringified, err := json.Marshal(*check)
if err != nil {
fmt.Fprint(w, "data: {}\n\n")
} else {
fmt.Fprintf(w, "data: %s\n\n", stringified)
}
f.Flush()
}
}
// createCheckRequests makes url request check at the specified interval
func createCheckRequests() {
for i := range checkConfigs {
go func(check *Check) {
ticker := time.NewTicker(time.Duration(check.Interval) * time.Second)
for {
<-ticker.C
checkRequestChannel <- check
}
}(&checkConfigs[i])
}
}
// checkRequestChannelListener listens to checkRequestChannel and performs requests
func checkRequestChannelListener(broker *Broker) {
for check := range checkRequestChannel {
go doRequest(check, broker)
}
}
// doRequest performs check requests
// and sends it to checkResults of Broker
func doRequest(check *Check, broker *Broker) {
jar, err := cookiejar.New(nil)
if err != nil {
check.Error = err.Error()
broker.checkResult <- check
return
}
client := http.Client{
Timeout: time.Duration(check.Timeout) * time.Millisecond,
Jar: jar,
}
request, err := http.NewRequest(check.Method, check.URL, nil)
if err != nil {
check.Error = err.Error()
broker.checkResult <- check
return
}
start := time.Now()
check.Timestamp = start
resp, err := client.Do(request)
if err != nil {
check.Error = err.Error()
broker.checkResult <- check
if err, ok := err.(net.Error); ok && err.Timeout() {
check.ResponseTime = check.Timeout
// notify slack only if status changed
if check.PreviousOK {
text := fmt.Sprintf("%s [%s] timed out after %dms.", check.Name, check.URL, check.Timeout)
notifySlack(text)
}
} else {
// notify slack only if status changed
if check.PreviousOK {
text := fmt.Sprintf("%s [%s] is down.", check.Name, check.URL)
notifySlack(text)
}
}
// set PreviousOK for next check
check.PreviousOK = false
return
}
check.Error = ""
check.StatusCode = resp.StatusCode
elapsed := time.Since(start)
check.ResponseTime = int64(elapsed / time.Millisecond)
log.Printf("%s %s - %dms - %d %s", check.Method, check.URL, check.ResponseTime, check.StatusCode, check.Error)
broker.checkResult <- check
// notify slack if status changed
if !check.PreviousOK {
text := fmt.Sprintf("%s [%s] is up.", check.Name, check.URL)
notifySlack(text)
}
// set PreviousOK for next check
check.PreviousOK = true
}
// notifySlack sends an alert message to slack
func notifySlack(text string) {
slackTemplate.Text = text
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(slackTemplate)
if err != nil {
return
}
if slackTemplate.URL != "" {
http.Post(slackTemplate.URL, "application/json; charset=utf-8", b)
}
}
func main() {
// read from config file
_, currentFilePath, _, ok := runtime.Caller(0)
if !ok {
fmt.Println("Error recovering current file path.")
}
absConfigFile := filepath.Join(filepath.Dir(currentFilePath), "static", "config.json")
configFile, err := os.Open(absConfigFile)
if err != nil {
fmt.Println("Error opening config file:\n", err.Error())
os.Exit(1)
}
// parse config file
jsonParser := json.NewDecoder(configFile)
var config Config
if err = jsonParser.Decode(&config); err != nil {
fmt.Println("Error parsing config file:\n", err.Error())
os.Exit(1)
}
// create slackTemplate
slackTemplate = config.Slack
// create buffered channel for check requests
checkRequestChannel = make(chan *Check, len(config.Checks))
// create check configurations
checkConfigs = config.Checks
for i := range checkConfigs {
// defaults
checkConfigs[i].Error = ""
checkConfigs[i].PreviousOK = true
}
// create check requests
createCheckRequests()
// make new broker instance
broker := &Broker{
make(map[chan *Check]bool),
make(chan (chan *Check)),
make(chan (chan *Check)),
make(chan *Check),
}
broker.Start()
// goroutine that listens in on channel receiving check requests
go checkRequestChannelListener(broker)
// set broker as the HTTP handler for /events
http.Handle("/events", broker)
// serve static folder
http.Handle("/", http.FileServer(http.Dir(filepath.Join(filepath.Dir(currentFilePath), "static"))))
log.Println("Running checks...serving on port 8080.")
http.ListenAndServe(":8080", nil)
}