Exemplo n.º 1
0
// 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)
}
Exemplo n.º 2
0
// 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)
}