Beispiel #1
0
func GetConfig(configFilePath string) (config *Config, err error) {
	var (
		file *os.File
		buf  []byte
	)
	file, err = os.Open(configFilePath)
	if err != nil {
		return
	}
	defer file.Close()
	buf, err = ioutil.ReadAll(file)
	if err != nil {
		return
	}
	config = new(Config)
	if err = toml.Unmarshal(buf, config); err != nil {
		return
	}
	config.Logger.LogFile, err = reopen.NewFileWriter(config.Logger.LogFilePath)

	if config.DatabaseConnectOpts.Log {
		config.DatabaseConnectOpts.logFile = config.Logger.LogFile
	} else {
		config.DatabaseConnectOpts.logFile = ioutil.Discard
	}
	return
}
Beispiel #2
0
// Daemon mode (agent mode)
func CmdDaemon(c *cli.Context) {

	fp, err := reopen.NewFileWriter(c.String("logfile"))
	if err != nil {
		fmt.Println(err)
	}
	log.SetOutput(fp)
	sigHup := make(chan os.Signal, 1)
	signal.Notify(sigHup, syscall.SIGHUP)
	go func() {
		for {
			<-sigHup
			fp.Reopen()
		}
	}()

	m := customClassic()
	m.Use(render.Renderer())
	m.Use(util.ACL(c.StringSlice("allowed-hosts")))
	m.Use(
		secure.Secure(secure.Options{
			SSLRedirect:      true,
			DisableProdCheck: true,
		}))

	// CPU Profiling
	if c.String("cpu-profile") != "" {
		cpuprofile := c.String("cpu-profile")
		f, err := os.Create(cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
		cpuprof := make(chan os.Signal, 1)
		signal.Notify(cpuprof, os.Interrupt)
		go func() {
			for sig := range cpuprof {
				log.Printf("captured %v, stopping profiler and exiting...", sig)
				pprof.StopCPUProfile()
				os.Exit(1)
			}
		}()
	}

	m.Get("/", func() string {
		return "OK"
	})

	util.CommandTimeout = time.Duration(c.Int("command-timeout"))
	model.MetricConfigFile = c.String("metric-config")

	m.Post("/proxy", binding.Json(happo_agent.ProxyRequest{}), model.Proxy)
	m.Post("/inventory", binding.Json(happo_agent.InventoryRequest{}), model.Inventory)
	m.Post("/monitor", binding.Json(happo_agent.MonitorRequest{}), model.Monitor)
	m.Post("/metric", binding.Json(happo_agent.MetricRequest{}), model.Metric)
	m.Post("/metric/config/update", binding.Json(happo_agent.MetricConfigUpdateRequest{}), model.MetricConfigUpdate)
	m.Get("/metric/status", model.MetricDataBufferStatus)

	// Listener
	var lis daemonListener
	lis.Port = fmt.Sprintf(":%d", c.Int("port"))
	lis.Handler = m
	lis.Timeout = happo_agent.HTTP_TIMEOUT
	lis.MaxConnections = c.Int("max-connections")
	lis.PublicKey = c.String("public-key")
	lis.PrivateKey = c.String("private-key")
	go func() {
		err := lis.listenAndServe()
		if err != nil {
			log.Fatal(err)
		}
	}()

	// Metric collect timer
	time_metrics := time.NewTicker(time.Minute).C
	for {
		select {
		case <-time_metrics:
			err := collect.Metrics(c.String("metric-config"))
			if err != nil {
				log.Fatal(err)
			}
		}
	}
}