This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
checks.go
422 lines (351 loc) · 11 KB
/
checks.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package resolver
import (
"bytes"
"encoding/json"
"fmt"
"path"
"sync"
"time"
etcd "github.com/coreos/etcd/client"
"github.com/gogo/protobuf/jsonpb"
"github.com/opsee/basic/clients/hugs"
"github.com/opsee/basic/schema"
opsee "github.com/opsee/basic/service"
log "github.com/opsee/logrus"
opsee_types "github.com/opsee/protobuf/opseeproto/types"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
type checkCompostResponse struct {
response interface{}
}
const (
RoutePath = "/opsee.co/routes"
MagicExecutionGroup = "127a7354-290e-11e6-b178-2bc1f6aefc14"
)
// ListChecks fetches Checks from Bartnet and CheckResults from Beavis
// concurrently, then zips them together. If the request to Beavis fails,
// then checks are returned without results.
func (c *Client) ListChecks(ctx context.Context, user *schema.User, checkId string, transitionId int) ([]*schema.Check, error) {
var (
responseChan = make(chan *checkCompostResponse, 2)
notifMap = make(map[string][]*schema.Notification)
wg sync.WaitGroup
)
wg.Add(1)
go func() {
var (
notifs []*hugs.Notification
err error
)
if checkId != "" {
notifs, err = c.Hugs.ListNotificationsCheck(user, checkId)
} else {
notifs, err = c.Hugs.ListNotifications(user)
}
if err != nil {
responseChan <- &checkCompostResponse{err}
} else {
responseChan <- &checkCompostResponse{notifs}
}
wg.Done()
}()
var (
checks []*schema.Check
err error
)
if checkId != "" && transitionId > 0 {
req := &opsee.GetCheckSnapshotRequest{
Requestor: user,
CheckId: checkId,
TransitionId: int64(transitionId),
}
resp, err := c.Cats.GetCheckSnapshot(ctx, req)
if err != nil {
log.WithError(err).Error("Error getting check snapshot from cats.")
return nil, err
}
check := resp.Check
checks = append(checks, check)
} else {
if checkId != "" {
check, err := c.Bartnet.GetCheck(user, checkId)
if err != nil {
log.WithError(err).Error("couldn't list checks from bartnet")
return nil, err
}
checks = append(checks, check)
} else {
checks, err = c.Bartnet.ListChecks(user)
if err != nil {
log.WithError(err).Error("couldn't list checks from bartnet")
return nil, err
}
}
}
wg.Wait()
close(responseChan)
for resp := range responseChan {
switch t := resp.response.(type) {
case []*hugs.Notification:
for _, notif := range t {
notifMap[notif.CheckId] = append(notifMap[notif.CheckId], &schema.Notification{Type: notif.Type, Value: notif.Value})
}
case error:
log.WithError(t).Error("error composting checks")
}
}
for _, check := range checks {
if transitionId == 0 {
results, err := c.CheckResults(ctx, user, check.Id)
if err != nil {
log.WithError(err).Error("Error getting check results.")
return nil, err
}
check.Results = results
if check.Spec == nil {
if check.CheckSpec == nil {
continue
}
any, err := opsee_types.UnmarshalAny(check.CheckSpec)
if err != nil {
log.WithError(err).Error("couldn't list checks from bartnet")
return nil, err
}
switch spec := any.(type) {
case *schema.HttpCheck:
check.Spec = &schema.Check_HttpCheck{spec}
case *schema.CloudWatchCheck:
check.Spec = &schema.Check_CloudwatchCheck{spec}
}
}
}
check.Notifications = notifMap[check.Id]
}
return checks, nil
}
func (c *Client) UpsertChecks(ctx context.Context, user *schema.User, checksInput []interface{}) ([]*schema.Check, error) {
notifs := make([]*hugs.NotificationRequest, 0, len(checksInput))
checksResponse := make([]*schema.Check, len(checksInput))
for i, checkInput := range checksInput {
check, ok := checkInput.(map[string]interface{})
if !ok {
err := fmt.Errorf("error decoding check input")
log.WithError(err).Error("Error in UpsertChecks request")
return nil, err
}
notifList, _ := check["notifications"].([]interface{})
delete(check, "notifications")
checkJson, err := json.Marshal(check)
if err != nil {
log.WithError(err).Error("Error marshalling check from request.")
return nil, err
}
checkProto := &schema.Check{}
err = jsonpb.Unmarshal(bytes.NewBuffer(checkJson), checkProto)
if err != nil {
log.WithError(err).Error("Error unmarshalling check protobuf.")
return nil, err
}
var checkResponse *schema.Check
if checkProto.Id == "" {
checkResponse, err = c.Bartnet.CreateCheck(user, checkProto)
if err != nil {
log.WithError(err).Error("Error creating check.")
return nil, err
}
} else {
checkResponse, err = c.Bartnet.UpdateCheck(user, checkProto)
if err != nil {
log.WithError(err).Error("Error updating check.")
return nil, err
}
}
if notifList != nil {
notif := &hugs.NotificationRequest{
CheckId: checkResponse.Id,
}
for _, n := range notifList {
nl, _ := n.(map[string]interface{})
t, _ := nl["type"].(string)
v, _ := nl["value"].(string)
if t != "" && v != "" {
// due to our crappy backend, we send a bulk request to hugs of all notifications...
notif.Notifications = append(notif.Notifications, &hugs.Notification{
Type: t,
Value: v,
})
// ... then we add each notification to the check object
checkResponse.Notifications = append(checkResponse.Notifications, &schema.Notification{
Type: t,
Value: v,
})
}
}
notifs = append(notifs, notif)
}
err = c.Hugs.CreateNotificationsMulti(user, notifs)
if err != nil {
log.WithError(err).Error("Error creating notification")
return nil, err
}
checksResponse[i] = checkResponse
}
return checksResponse, nil
}
func (c *Client) DeleteChecks(ctx context.Context, user *schema.User, checksInput []interface{}) ([]string, error) {
deleted := make([]string, 0, len(checksInput))
for _, ci := range checksInput {
id, ok := ci.(string)
if !ok {
err := fmt.Errorf("unable to decode check id")
log.WithError(err).Error("Error in delete check request.")
return nil, err
}
err := c.Bartnet.DeleteCheck(user, id)
if err != nil {
log.WithError(err).Error("Error deleting check: %d", id)
continue
}
deleted = append(deleted, id)
}
return deleted, nil
}
func (c *Client) TestCheck(ctx context.Context, user *schema.User, checkInput map[string]interface{}) (*opsee.TestCheckResponse, error) {
var (
responses []*schema.CheckResponse
exgroupId = user.CustomerId
)
checkJson, err := json.Marshal(checkInput)
if err != nil {
log.WithError(err).Error("Error marshalling check from request.")
return nil, err
}
checkProto := &schema.Check{}
err = jsonpb.Unmarshal(bytes.NewBuffer(checkJson), checkProto)
if err != nil {
log.WithError(err).Error("Error unmarshalling check protobuf.")
return nil, err
}
checkProto.Interval = int32(30)
// backwards compat with old bastion proto: TODO(mark) remove
switch t := checkProto.Spec.(type) {
case *schema.Check_HttpCheck:
checkProto.CheckSpec, err = opsee_types.MarshalAny(t.HttpCheck)
case *schema.Check_CloudwatchCheck:
checkProto.CheckSpec, err = opsee_types.MarshalAny(t.CloudwatchCheck)
}
if err != nil {
log.WithError(err).Error("Error marshalling check spec.")
return nil, err
}
if checkProto.Target == nil {
err := fmt.Errorf("test check is missing target")
log.WithError(err).Error("Error in test check request.")
return nil, err
}
if checkProto.Target.Type == "external_host" {
exgroupId = MagicExecutionGroup
}
// use customer id or execution group id ok!!
response, err := c.EtcdKeys.Get(ctx, path.Join(RoutePath, exgroupId), &etcd.GetOptions{
Recursive: true,
Quorum: true,
})
if len(response.Node.Nodes) == 0 {
return nil, fmt.Errorf("no bastions found")
}
// the deadline for the TestCheckRequest, this gets folded into the bastion check runner's
// context, but i'm not sure why it's different than our grpc request context
deadline := &opsee_types.Timestamp{}
deadline.Scan(time.Now().Add(time.Minute))
node := response.Node.Nodes[0]
responseChan := make(chan *opsee.TestCheckResponse)
errChan := make(chan error)
// going to set a timeout for our grpc context that's a bit bigger than the
// TestCheckRequest deadline
ctx, _ = context.WithTimeout(ctx, time.Minute)
go func(node *etcd.Node) {
services := make(map[string]interface{})
err = json.Unmarshal([]byte(node.Value), &services)
if err != nil {
log.WithError(err).Errorf("error unmarshaling portmapper: %#v", node.Value)
errChan <- err
return
}
if checker, ok := services["checker"].(map[string]interface{}); ok {
checkerHost, _ := checker["hostname"].(string)
checkerPort, _ := checker["port"].(float64)
addr := fmt.Sprintf("%s:%d", checkerHost, int(checkerPort))
conn, err := grpc.Dial(
addr,
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithTimeout(3*time.Second),
)
if err != nil {
log.WithError(err).Errorf("coudln't contact bastion at: %s ... ignoring", addr)
errChan <- err
return
}
log.Info("established grpc connection to bastion at: %s", addr)
defer conn.Close()
resp, err := opsee.NewCheckerClient(conn).TestCheck(ctx, &opsee.TestCheckRequest{Deadline: deadline, Check: checkProto})
if err != nil {
log.WithError(err).Errorf("got error from bastion at: %s ... ignoring", addr)
errChan <- err
return
}
responseChan <- resp
}
}(node)
select {
case resp := <-responseChan:
responses = append(responses, resp.Responses...)
case <-errChan:
// idk what to do with errors here
case <-ctx.Done():
// idk what to do with errors here
}
return &opsee.TestCheckResponse{Responses: responses}, nil
}
func (c *Client) CheckResults(ctx context.Context, user *schema.User, checkId string) (results []*schema.CheckResult, err error) {
resp, err := c.Cats.GetCheckResults(ctx, &opsee.GetCheckResultsRequest{
CustomerId: user.CustomerId,
CheckId: checkId,
})
if err != nil {
return nil, err
}
return resp.Results, nil
}
// Get check state transitions from cats
func (c *Client) GetCheckStateTransitions(ctx context.Context, user *schema.User, checkId string, startTime, endTime *opsee_types.Timestamp) ([]*schema.CheckStateTransition, error) {
req := &opsee.GetCheckStateTransitionsRequest{
CheckId: checkId,
CustomerId: user.CustomerId,
AbsoluteStartTime: startTime,
AbsoluteEndTime: endTime,
}
resp, err := c.Cats.GetCheckStateTransitions(ctx, req)
if err != nil {
return nil, err
}
return resp.Transitions, nil
}
// Get a single check state transition from cats
func (c *Client) GetCheckStateTransition(ctx context.Context, user *schema.User, checkId string, transitionId int) (*schema.CheckStateTransition, error) {
req := &opsee.GetCheckStateTransitionsRequest{
CheckId: checkId,
CustomerId: user.CustomerId,
StateTransitionId: int64(transitionId),
}
resp, err := c.Cats.GetCheckStateTransitions(ctx, req)
if err != nil {
return nil, err
}
if len(resp.Transitions) == 1 {
return resp.Transitions[0], nil
}
return nil, fmt.Errorf("Received incorrect number of state transitions from cats: %v", resp.Transitions)
}