forked from CiscoCloud/distributive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
380 lines (362 loc) · 11.5 KB
/
main.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
// Distributive is a tool for running distributed health checks in server clusters.
// It was designed with Consul in mind, but is platform agnostic.
// The idea is that the checks are run locally, but executed by a central server
// that records and logs their output. This model distributes responsibility to
// each node, instead of one central server, and allows for more types of checks.
package main
import (
"encoding/json"
"fmt"
"github.com/CiscoCloud/distributive/workers"
"github.com/CiscoCloud/distributive/wrkutils"
log "github.com/Sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// where remote checks are downloaded to
var remoteCheckDir = "/var/run/distributive/"
// Check is a struct for a unified interface for health checks
// It passes its check-specific fields to that check's workers.Worker
type Check struct {
Name, Notes string
Check string // type of check to run
Parameters []string
Work wrkutils.Worker
}
// Checklist is a struct that provides a concise way of thinking about doing
// several checks and then returning some kind of output.
type Checklist struct {
Name, Notes string
Checklist []Check // list of Checks to run
Codes []int
Messages []string
Origin string // where did it come from?
Report string // printable status update
Failed bool // did any of the checks fail?
}
// makeReport returns a string used for a checklist.Report attribute, printed
// after all the checks have been run
// TODO transform use of makeReport into a logReport that uses logrus
func (chklst *Checklist) makeReport() {
if chklst == nil {
return
}
// countInt counts the occurences of int in this []int
countInt := func(i int, slice []int) (counter int) {
for _, in := range slice {
if in == i {
counter++
}
}
return counter
}
// get fail messages
failMessages := []string{}
for i, code := range chklst.Codes {
if code != 0 {
failMessages = append(failMessages, "\n"+chklst.Messages[i])
}
}
// output global stats
total := len(chklst.Codes)
passed := countInt(0, chklst.Codes)
report := "↴\nTotal: " + fmt.Sprint(total)
report += "\nPassed: " + fmt.Sprint(passed)
report += "\nFailed: " + fmt.Sprint(total-passed)
for _, msg := range failMessages {
report += msg
}
chklst.Report = report
}
// validateParameters asks whether or not this check has the correct number of
// parameters specified
func validateParameters(chk Check) {
// checkParameterLength ensures that the Check has the proper number of
// parameters, and exits otherwise. Can't do much with a broken check!
checkParameterLength := func(chk Check, expected int) {
given := len(chk.Parameters)
if expected == 0 {
log.WithFields(log.Fields{
"name": chk.Name,
"check type": chk.Check,
"parameters": chk.Parameters,
}).Fatal("Invalid check")
} else if given != expected {
log.WithFields(log.Fields{
"name": chk.Name,
"check type": chk.Check,
"expected": expected,
"given": given,
"parameters": chk.Parameters,
}).Fatal("Invalid check parameters")
}
}
// for testing this independently of main, shouldn't run outside of testing
if len(wrkutils.ParameterLength) < 1 {
workers.RegisterAll()
}
if len(wrkutils.ParameterLength) < 1 {
log.WithFields(log.Fields{
"name": chk.Name,
"check type": chk.Check,
"given": len(chk.Parameters),
"parameters": chk.Parameters,
}).Fatal("wrkutils.ParameterLength table is empty")
}
expected := wrkutils.ParameterLength[strings.ToLower(chk.Check)]
checkParameterLength(chk, expected)
}
// getworkers.Worker returns a workers.Worker based on the Check's name. It
// ensures that any invalid checks are reported appropriately.
func getWorker(chk Check) wrkutils.Worker {
work := wrkutils.Workers[strings.ToLower(chk.Check)]
if work == nil {
msg := "JSON file included one or more unsupported health checks"
msg2 := "(check lookup returned nil function)"
log.WithFields(log.Fields{
"name": chk.Name,
"type": chk.Check,
"parameters": chk.Parameters,
}).Fatal(msg + " " + msg2)
return nil
}
return work
}
// checklistFromBytes takes a bytestring of utf8 encoded JSON and turns it into
// a checklist struct. Used by all checklist constructors below. It validates
// the number of parameters that each check has.
func checklistFromBytes(data []byte) (chklst Checklist) {
err := json.Unmarshal(data, &chklst)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"content": string(data),
}).Fatal("Couldn't parse checklist JSON.")
}
// get workers for each check
out := make(chan Check)
defer close(out)
for _, chk := range chklst.Checklist {
go func(chk Check, out chan Check) {
validateParameters(chk)
chk.Work = getWorker(chk)
out <- chk
}(chk, out)
}
// grab all the data from the channel, mutating the checklist
for i := range chklst.Checklist {
chklst.Checklist[i] = <-out
}
return chklst
}
// checklistFromFile reads the file at the path and parses its utf8 encoded json
// data, turning it into a checklist struct.
func checklistFromFile(path string) (chklst Checklist) {
return checklistFromBytes(wrkutils.FileToBytes(path))
}
// checklistFromStdin reads the stdin pipe and parses its utf8 encoded json
// data, turning it into a checklist struct.
func checklistFromStdin() (chklst Checklist) {
stdinAsBytes := func() []byte {
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Fatal("Couldn't read from stdin")
}
return bytes
}
return checklistFromBytes(stdinAsBytes())
}
// checklistsFromDir reads all of the files in the path and parses their utf8
// encoded json data, turning it into a checklist struct.
func checklistsFromDir(dirpath string) (chklsts []Checklist) {
paths := wrkutils.GetFilesWithExtension(dirpath, ".json")
// send one checklist per path to the channel
out := make(chan Checklist)
for _, path := range paths {
go func(path string, out chan Checklist) {
out <- checklistFromFile(path)
}(path, out)
}
// get all values from the channel, return them
for _ = range paths {
chklsts = append(chklsts, <-out)
}
return chklsts
}
// checklistsFromDir reads data retrieved from the URL and parses its utf8
// encoded json data, turning it into a checklist struct. It also caches this
// data at remoteCheckDir, currently "/var/run/distributive/"
func checklistFromURL(urlstr string) (chklst Checklist) {
// ensure temp files dir exists
log.Debug("Creating/checking remote checklist dir")
if err := os.MkdirAll(remoteCheckDir, 0775); err != nil {
log.WithFields(log.Fields{
"dir": remoteCheckDir,
"error": err.Error(),
}).Warn("Could not create remote check directory")
remoteCheckDir = "./.remote-checks"
if err := os.MkdirAll(remoteCheckDir, 0755); err != nil {
wrkutils.CouldntWriteError(remoteCheckDir, err)
}
}
log.Debug("Using " + remoteCheckDir + " for remote check storage")
// pathSanitize filters these (path illegal) chars: /?%*:|<^>. \
pathSanitize := func(str string) (filename string) {
filename = str
disallowed := []string{
`/`, `?`, `%`, `*`, `:`, `|`, `"`, `<`, `^`, `>`, `.`, `\`, ` `,
}
for _, c := range disallowed {
filename = strings.Replace(filename, c, "", -1)
}
return filename
}
filename := pathSanitize(urlstr) + ".json"
fullpath := filepath.Join(remoteCheckDir, filename)
// only create it if it doesn't exist
if _, err := os.Stat(fullpath); err != nil {
log.Info("Fetching remote checklist")
body := wrkutils.URLToBytes(urlstr, true) // secure connection
log.Debug("Writing remote checklist to cache")
wrkutils.BytesToFile(body, fullpath)
return checklistFromBytes(body)
}
log.WithFields(log.Fields{
"path": fullpath,
}).Info("Using local copy of remote checklist")
return checklistFromFile(fullpath)
}
// getChecklists returns a list of checklists based on the supplied sources
func getChecklists(file string, dir string, url string, stdin bool) (checklists []Checklist) {
msg := "Creating checklist(s)..."
switch {
// checklists from file are already tagged with their origin
// this applies to FromFile, FromDir, FromURL
case file != "":
log.WithFields(log.Fields{
"type": "file",
"path": file,
}).Info(msg)
checklists = append(checklists, checklistFromFile(file))
case dir != "":
log.WithFields(log.Fields{
"type": "dir",
"path": dir,
}).Info(msg)
checklists = append(checklists, checklistsFromDir(dir)...)
case url != "":
log.WithFields(log.Fields{
"type": "url",
"path": url,
}).Info(msg)
checklists = append(checklists, checklistFromURL(url))
case stdin == true:
log.WithFields(log.Fields{
"type": "url",
"path": url,
}).Info(msg)
checklist := checklistFromStdin()
checklist.Origin = "stdin"
checklists = append(checklists, checklist)
default:
log.Fatal("Neither file, URL, directory, nor stdin specified. Try --help.")
}
return checklists
}
// runChecks takes a checklist, performs every worker, and collects the results
// in that checklist's Codes and Messages fields.
func (chklst *Checklist) runChecks() {
codes := make(chan int)
msgs := make(chan string)
for _, chk := range chklst.Checklist {
// concurrently execute the checklist's checks, passing their
go func(chk Check, codes chan int, msgs chan string) {
if chk.Work == nil {
msg := "Nil function associated with this check."
msg += " Please submit a bug report with this message."
log.WithFields(log.Fields{
"check": chk.Check,
"check map": fmt.Sprint(wrkutils.Workers),
}).Fatal(msg)
}
code, msg := chk.Work(chk.Parameters)
// Log an informational message on the check's status
if code == 0 {
log.WithFields(log.Fields{
"name": chk.Name,
"type": chk.Check,
}).Info("Check passed")
} else {
log.WithFields(log.Fields{
"name": chk.Name,
"type": chk.Check,
}).Warn("Check failed")
chklst.Failed = true
}
// send back results
codes <- code
msgs <- msg
}(chk, codes, msgs)
}
// consume codes and messages, adding them to the Checklist struct
for _ = range chklst.Checklist {
code := <-codes
msg := <-msgs
chklst.Codes = append(chklst.Codes, code)
chklst.Messages = append(chklst.Messages, msg)
}
}
// main reads the command line flag -f, runs the Check specified in the JSON,
// and exits with the appropriate message and exit code.
func main() {
// Set up and parse flags
file, URL, directory, stdin := getFlags()
validateFlags(file, URL, directory)
// add workers to workers, parameterLength
workers.RegisterAll()
chklsts := getChecklists(file, directory, URL, stdin)
// run all checklists
out := make(chan Checklist)
defer close(out)
for i, chklst := range chklsts {
go func(chklst Checklist, out chan Checklist) {
// run checks, populate error codes and messages
log.Info("Running checklist: " + chklsts[i].Name)
chklst.runChecks()
chklst.makeReport()
// If any of the checks failed, mark this checklist as failed
for _, code := range chklsts[i].Codes {
if code != 0 {
chklst.Failed = true
}
}
// send out results
out <- chklst
}(chklst, out)
}
failed := false
for _ = range chklsts {
chklst := <-out
if chklst.Failed {
log.WithFields(log.Fields{
"checklist": chklst.Name,
"report": chklst.Report,
}).Warn("Check(s) failed, printing checklist report")
failed = true
} else {
log.WithFields(log.Fields{
"checklist": chklst.Name,
"report": chklst.Report,
}).Info("All checks passed, printing checklist report")
}
}
// see if any checks failed, exit accordingly
if failed {
os.Exit(1)
}
os.Exit(0)
}