-
Notifications
You must be signed in to change notification settings - Fork 0
/
whatsupmon.go
336 lines (275 loc) · 7.86 KB
/
whatsupmon.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package whatsupmon
import (
"fmt"
"net/http"
"strings"
"io/ioutil"
"text/template"
"bytes"
"time"
"appengine"
"appengine/urlfetch"
"appengine/datastore"
"appengine/mail"
)
var indexTemplate = template.Must(template.ParseFiles("templates/index.html"))
var notifSubjectTempl = template.Must(template.ParseFiles("templates/notifsubject.txt"))
var notifBodyTempl = template.Must(template.ParseFiles("templates/notifbody.txt"))
type Service struct {
Url string
HealthString string
Up bool
Enabled bool
}
type ServiceRecord struct {
Service Service
Key *datastore.Key
}
type Address struct {
Email string
}
type AddressRecord struct {
Address Address
Key *datastore.Key
}
type Notification struct {
Up []Service
Down []Service
}
type IndexModel struct {
ServiceRecords []ServiceRecord
AddressRecords []AddressRecord
}
func init() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/add", addHandler)
http.HandleFunc("/delete", deleteHandler)
http.HandleFunc("/check", checkHandler)
http.HandleFunc("/enable", enableHandler)
http.HandleFunc("/disable", disableHandler)
http.HandleFunc("/addaddr", addAddrHandler)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
services, err := getServices(c)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return;
}
addresses, err := getAddresses(c)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return;
}
model := IndexModel{
ServiceRecords: services,
AddressRecords: addresses,
}
if err := indexTemplate.Execute(w, model); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func addHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
service := Service{
Url: r.FormValue("url"),
HealthString: r.FormValue("healthstring"),
Up: false,
Enabled: true,
}
key := datastore.NewIncompleteKey(c, "service", serviceKey(c))
_, err := datastore.Put(c, key, &service)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
func deleteHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
key, err := datastore.DecodeKey(r.FormValue("key"));
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return;
}
datastore.Delete(c, key);
http.Redirect(w, r, "/", http.StatusFound)
}
func enableHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
key, err := datastore.DecodeKey(r.FormValue("key"));
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return;
}
err = setServiceEnabled(c, key, true);
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return;
}
http.Redirect(w, r, "/", http.StatusFound)
}
func disableHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
key, err := datastore.DecodeKey(r.FormValue("key"));
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return;
}
err = setServiceEnabled(c, key, false);
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return;
}
http.Redirect(w, r, "/", http.StatusFound)
}
func setServiceEnabled(c appengine.Context, key *datastore.Key, enabled bool) error {
var service Service;
err := datastore.Get(c, key, &service)
if err != nil {
return err;
}
service.Enabled = enabled
_, err = datastore.Put(c, key, &service)
return err
}
func checkHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
timeout, _:= time.ParseDuration("20s");
transport := &urlfetch.Transport{
Context: c,
Deadline: timeout,
AllowInvalidServerCertificate: false,
}
client := &http.Client{
Transport: transport,
Timeout: 0,
}
serviceRecords, err := getServices(c)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return;
}
var notification Notification
for _, serviceRecord := range serviceRecords {
service := serviceRecord.Service
if !service.Enabled {
continue
}
err := validate(client, service.Url, service.HealthString)
if err != nil {
c.Infof("%v", err)
if(service.Up) {
service.Up = false;
datastore.Put(c, serviceRecord.Key, &service)
notification.Down = append(notification.Down, service)
}
} else if(!service.Up) {
service.Up = true;
datastore.Put(c, serviceRecord.Key, &service)
notification.Up = append(notification.Up, service)
}
}
if len(notification.Up) > 0 || len(notification.Down) > 0 {
if err := notify(c, notification); err != nil {
c.Errorf("Couldn't send notification: %v", err)
}
}
c.Infof("%s", "Completed Checks.");
}
func validate(client *http.Client, url string, healthstring string) error {
resp, err := client.Get(url)
if err != nil {
return fmt.Errorf("Could not access %s: %v", url, err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Could not read the body of the response for %s: %v", url, err)
}
if !strings.Contains(string(body), healthstring) {
return fmt.Errorf("Could not find health string on %s", url)
}
return nil
}
func serviceKey(c appengine.Context) *datastore.Key {
return datastore.NewKey(c, "service", "services", 0, nil)
}
func getServices(c appengine.Context) ([]ServiceRecord, error) {
q := datastore.NewQuery("service").Ancestor(serviceKey(c))
var services []Service
keys, err := q.GetAll(c, &services)
if err != nil {
return nil, err
}
var serviceRecords = make([]ServiceRecord, 0, len(services))
for index, service := range services {
sr := ServiceRecord{
Service: service,
Key: keys[index],
}
serviceRecords = append(serviceRecords, sr)
}
return serviceRecords, nil
}
func notify(c appengine.Context, notification Notification) error {
var subjectBuf bytes.Buffer
if err := notifSubjectTempl.Execute(&subjectBuf, notification); err != nil {
return err
}
var bodyBuf bytes.Buffer
if err := notifBodyTempl.Execute(&bodyBuf, notification); err != nil {
return err
}
addrRecords, err := getAddresses(c)
if err != nil {
return err
}
var toAddrs = make([]string, 0, len(addrRecords))
for _, address := range addrRecords {
toAddrs = append(toAddrs, address.Address.Email)
}
msg := &mail.Message{
Sender: "What's Up Mon? <[email protected]>",
To: toAddrs,
Subject: subjectBuf.String(),
Body: bodyBuf.String(),
}
if err := mail.Send(c, msg); err != nil {
return err
}
return nil
}
func addAddrHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
addr := Address {
Email: r.FormValue("addr"),
}
key := datastore.NewIncompleteKey(c, "address", addressKey(c))
_, err := datastore.Put(c, key, &addr)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
func addressKey(c appengine.Context) *datastore.Key {
return datastore.NewKey(c, "address", "addresses", 0, nil)
}
func getAddresses(c appengine.Context) ([]AddressRecord, error) {
q := datastore.NewQuery("address").Ancestor(addressKey(c))
var addresses []Address
keys, err := q.GetAll(c, &addresses)
if err != nil {
return nil, err
}
var addressRecords = make([]AddressRecord, 0, len(addresses))
for index, address := range addresses {
addr := AddressRecord{
Address: address,
Key: keys[index],
}
addressRecords = append(addressRecords, addr)
}
return addressRecords, nil
}