Пример #1
0
func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	flag.Parse()
	var c Conf
	if _, err := toml.DecodeFile(*confFlag, &c); err != nil {
		log.Fatal("failed to decode config file: ", err)
	}
	backends := []backend.Backend{}
	for _, eCluster := range c.ElasticClusters {
		b, err := backend.NewElastic(eCluster.Servers, eCluster.Index)
		if err != nil {
			log.Fatal(err)
		}
		backends = append(backends, b)
	}
	for _, b := range backends {
		if err := b.InitBackend(); err != nil {
			log.Fatal(err)
		}
	}
	router := mux.Router{}
	if err := web.AddRoutes(&router, "", backends, true, *localFlag); err != nil {
		log.Fatal(err)
	}
	http.Handle("/", &router)
	go func() {
		log.Fatal(http.ListenAndServe(c.ListenAddress, nil))
	}()
	select {}
}
Пример #2
0
func Listen(listenAddr string, devMode bool, tsdbHost string, reloadFunc func() error) error {
	if devMode {
		slog.Infoln("using local web assets")
	}
	webFS := FS(devMode)

	indexTemplate = func() *template.Template {
		str := FSMustString(devMode, "/templates/index.html")
		templates, err := template.New("").Parse(str)
		if err != nil {
			slog.Fatal(err)
		}
		return templates
	}

	reload = reloadFunc

	if !devMode {
		tpl := indexTemplate()
		indexTemplate = func() *template.Template {
			return tpl
		}
	}

	if tsdbHost != "" {
		router.HandleFunc("/api/index", IndexTSDB)
		router.Handle("/api/put", Relay(tsdbHost))
	}

	router.HandleFunc("/api/", APIRedirect)
	router.Handle("/api/action", JSON(Action))
	router.Handle("/api/alerts", JSON(Alerts))
	router.Handle("/api/config", miniprofiler.NewHandler(Config))
	router.Handle("/api/config_test", miniprofiler.NewHandler(ConfigTest))
	router.Handle("/api/save_enabled", JSON(SaveEnabled))
	if schedule.SystemConf.ReloadEnabled() { // Is true of save is enabled
		router.Handle("/api/reload", JSON(Reload)).Methods(http.MethodPost)
	}
	if schedule.SystemConf.SaveEnabled() {
		router.Handle("/api/config/bulkedit", miniprofiler.NewHandler(BulkEdit)).Methods(http.MethodPost)
		router.Handle("/api/config/save", miniprofiler.NewHandler(SaveConfig)).Methods(http.MethodPost)
		router.Handle("/api/config/diff", miniprofiler.NewHandler(DiffConfig)).Methods(http.MethodPost)
		router.Handle("/api/config/running_hash", JSON(ConfigRunningHash))
	}
	router.Handle("/api/egraph/{bs}.{format:svg|png}", JSON(ExprGraph))
	router.Handle("/api/errors", JSON(ErrorHistory))
	router.Handle("/api/expr", JSON(Expr))
	router.Handle("/api/graph", JSON(Graph))
	router.Handle("/api/health", JSON(HealthCheck))
	router.Handle("/api/host", JSON(Host))
	router.Handle("/api/last", JSON(Last))
	router.Handle("/api/quiet", JSON(Quiet))
	router.Handle("/api/incidents", JSON(Incidents))
	router.Handle("/api/incidents/open", JSON(ListOpenIncidents))
	router.Handle("/api/incidents/events", JSON(IncidentEvents))
	router.Handle("/api/metadata/get", JSON(GetMetadata))
	router.Handle("/api/metadata/metrics", JSON(MetadataMetrics))
	router.Handle("/api/metadata/put", JSON(PutMetadata))
	router.Handle("/api/metadata/delete", JSON(DeleteMetadata)).Methods("DELETE")
	router.Handle("/api/metric", JSON(UniqueMetrics))
	router.Handle("/api/metric/{tagk}", JSON(MetricsByTagKey))
	router.Handle("/api/metric/{tagk}/{tagv}", JSON(MetricsByTagPair))
	router.Handle("/api/rule", JSON(Rule))
	router.HandleFunc("/api/shorten", Shorten)
	router.Handle("/api/silence/clear", JSON(SilenceClear))
	router.Handle("/api/silence/get", JSON(SilenceGet))
	router.Handle("/api/silence/set", JSON(SilenceSet))
	router.Handle("/api/status", JSON(Status))
	router.Handle("/api/tagk/{metric}", JSON(TagKeysByMetric))
	router.Handle("/api/tagv/{tagk}", JSON(TagValuesByTagKey))
	router.Handle("/api/tagv/{tagk}/{metric}", JSON(TagValuesByMetricTagKey))
	router.Handle("/api/tagsets/{metric}", JSON(FilteredTagsetsByMetric))
	router.Handle("/api/opentsdb/version", JSON(OpenTSDBVersion))
	router.Handle("/api/annotate", JSON(AnnotateEnabled))

	// Annotations
	if schedule.SystemConf.AnnotateEnabled() {
		index := schedule.SystemConf.GetAnnotateIndex()
		if index == "" {
			index = "annotate"
		}
		annotateBackend = backend.NewElastic(schedule.SystemConf.GetAnnotateElasticHosts(), index)

		go func() {
			for {
				err := annotateBackend.InitBackend()
				if err == nil {
					return
				}
				slog.Warningf("could not initalize annotate backend, will try again: %v", err)
				time.Sleep(time.Second * 30)
			}
		}()
		web.AddRoutes(router, "/api", []backend.Backend{annotateBackend}, false, false)

	}

	router.HandleFunc("/api/version", Version)
	router.Handle("/api/debug/schedlock", JSON(ScheduleLockStatus))
	http.Handle("/", miniprofiler.NewHandler(Index))
	http.Handle("/api/", router)
	fs := http.FileServer(webFS)
	http.Handle("/partials/", fs)
	http.Handle("/static/", http.StripPrefix("/static/", fs))
	http.Handle("/favicon.ico", fs)
	slog.Infoln("bosun web listening on:", listenAddr)
	slog.Infoln("tsdb host:", tsdbHost)
	return http.ListenAndServe(listenAddr, nil)
}