func main() {

	config := api.DefaultConfig()
	config.Address = "localhost:8500"
	client, err := api.NewClient(config)
	if err != nil {
		log.Fatalf("Fatal error: %v", err)
	}
	job := myJob{}
	// params as follows.
	// consul client, leader key, leadership check interval, session ttl, session name, exit on lock found, a DoJob implementation.
	leader_election.MaybeAcquireLeadership(client, "example/leader", 20, 30, "example", true, job)
}
Beispiel #2
0
func main() {

	flag.Parse()
	checkFlags()
	var pusher push_metric.Pusher
	var err error
	if *pushMetric {
		pusher, err = push_metric.SetupProviders()
		if err != nil {
			glog.Exitf("Problem while setting up push provider: %v", err)
		}
	}
	config, err := ioutil.ReadFile(*configFlag)
	if err != nil {
		glog.Exitf("Error reading probe config file: %v", err)
	}
	probes, err := conf.SetupConfig(config)
	if err != nil {
		glog.Exitf("Error in probe config setup, exiting: %v", err)
	}
	err = misc.CheckProbeConfig(probes)
	if err != nil {
		glog.Exitf("Error in probe config, exiting: %v", err)
	}

	probeNames := conf.GetProbeNames(probes)
	mExp, err := metric_export.SetupMetricExporter(*expositionType)
	if err != nil {
		glog.Exitf("Error : %v", err)
	}

	var fh *os.File
	if *webLogDir != "" {
		fh, err = log.SetupWebLog(*webLogDir, time.Now())
		if err != nil {
			glog.Exitf("Failed to set up logging", err)
		}
	} else {
		fh = os.Stdout // logs web accesses to stdout. May not be thread safe.
	}

	ps := misc.NewProbesStatus(probeNames)
	http.Handle("/", handlers.CombinedLoggingHandler(fh, http.HandlerFunc(misc.HandleHomePage)))
	http.Handle("/status", handlers.CombinedLoggingHandler(fh, misc.HandleStatus(ps)))
	http.Handle("/config", handlers.CombinedLoggingHandler(fh, http.HandlerFunc(misc.HandleConfig(config))))
	http.Handle(*metricsPath, handlers.CombinedLoggingHandler(fh, mExp.MetricHttpHandler()))

	glog.Info("Starting goProbe server.")
	glog.Infof("Will expose metrics in %s format via %s http path.", *expositionType, *metricsPath)
	glog.Infof("/config shows current config, /status shows current probe status.")

	if !*dryRun {
		// Start probing.
		stopCh := make(chan bool)
		if *haMode {
			glog.Info("Running in HA mode..")
			client, err := getConsulClient()
			if err != nil {
				glog.Fatalf("Fatal error: %v", err)
			}
			job := NewDoJob(pusher, probes, mExp, ps)
			go leader_election.MaybeAcquireLeadership(client, "goProbe/leader", 20, 30, "goProbe", false, job)
		} else {
			go runProbes(pusher, probes, mExp, ps, stopCh)
		}
		if err = http.ListenAndServe(*listenAddress, nil); err != nil {
			panic(err)
		}
	} else {
		glog.Info("Dry run mode.")
	}
}