func main() { flag.Parse() if *domain == "" { flag.PrintDefaults() os.Exit(1) } analysis, err := debugger.Analyze(*domain) if err != nil { log.Fatal(err) } fmt.Print(analysis) if analysis.Status() != debugger.OK { os.Exit(1) } }
func main() { flag.Parse() if *flagDomain == "" || *flagFrom == "" || *flagTo == "" { flag.PrintDefaults() os.Exit(1) } var state debugger.Status var warnings int for ; ; time.Sleep(*flagDuration) { var result debugger.Status var details string analysis, err := debugger.Analyze(*flagDomain) if err != nil { log.Printf("[Analyze (%s)] Error: %v", state, err) result = debugger.WARNING details = err.Error() } else { log.Printf("[Analyze (%s)] Status: %s", state, analysis.Status()) result = analysis.Status() details = analysis.String() } /* State machine: +----> OK <--+ v v WARNING ------> ERROR - 3 successive warnings becomes an error - transitions in or out of error state generates an alert */ var newState debugger.Status switch state { case debugger.OK: switch result { case debugger.WARNING: newState = debugger.WARNING case debugger.ERROR: newState = debugger.ERROR } case debugger.WARNING: switch result { case debugger.OK: newState = debugger.OK warnings = 0 case debugger.WARNING: warnings++ if warnings > 3 { newState = debugger.ERROR warnings = 0 } case debugger.ERROR: newState = debugger.ERROR warnings = 0 } case debugger.ERROR: switch result { case debugger.OK: newState = debugger.OK } } if state == newState { continue } if state == debugger.ERROR || newState == debugger.ERROR { log.Printf("[Analyze (%s)] New state: %s", state, newState) if err := email(newState.String(), details); err != nil { log.Print(err) } } state = newState } }