Ejemplo n.º 1
0
func (ctx *Ctx) LoadConfig(path *string) error {
	err := cfgfile.Read(&ctx.UserConfig, *path)
	if err != nil {
		return fmt.Errorf("loading config file error: %v\n", err)
	}

	// Process top-level configuration
	// server_url
	ctx.ServerUrl, err = url.Parse(ctx.UserConfig.ServerUrl)
	if err != nil || ctx.ServerUrl.Scheme == "" || ctx.ServerUrl.Host == "" {
		log.Fatal("Server-url is not valid. Should be like http://127.0.0.1:9000/api/ ", err)
	}
	if ctx.UserConfig.ServerUrl == "" {
		log.Fatalf("Server-url is empty.")
	}

	// collector_id
	if ctx.UserConfig.CollectorId == "" {
		log.Fatal("No collector ID was configured.")
	}
	ctx.CollectorId = common.GetCollectorId(ctx.UserConfig.CollectorId)

	// node_id
	if ctx.UserConfig.NodeId == "" {
		log.Info("No node-id was configured, falling back to hostname")
		ctx.UserConfig.NodeId, err = common.GetHostname()
		if err != nil {
			log.Fatal("No node-id configured and not able to obtain hostname as alternative.")
		}
	}

	// tags
	if len(ctx.UserConfig.Tags) == 0 {
		log.Fatal("Please define configuration tags.")
	} else {
		log.Info("Fetching configurations tagged by: ", ctx.UserConfig.Tags)
	}

	// cache_path
	if ctx.UserConfig.CachePath == "" {
		var cachePath string
		if runtime.GOOS == "windows" {
			cachePath = filepath.Join(os.Getenv("SystemDrive")+"\\", "Program Files", "graylog", "collector-sidecar", "cache")
		} else {
			cachePath = filepath.Join("/var", "cache", "graylog", "collector-sidecar")
		}
		ctx.UserConfig.CachePath = cachePath
		log.Errorf("No cache directory was configured. Using default: %s", cachePath)
	}

	// log_path
	if ctx.UserConfig.LogPath == "" {
		log.Fatal("No log directory was configured.")
	}

	// log_rotation_time
	if !(ctx.UserConfig.LogRotationTime > 0) {
		log.Fatal("Please set the log rotation time > 0 seconds.")
	}

	// log_max_age
	if !(ctx.UserConfig.LogMaxAge > 0) {
		log.Fatal("Please set the maximum age of log file rotation > 0 seconds.")
	}

	// list log files
	if len(ctx.UserConfig.ListLogFiles) > 0 {
		for _, dir := range ctx.UserConfig.ListLogFiles {
			if !common.IsDir(dir) {
				log.Fatal("Please provide a list of directories for list_log_files.")
			}
		}
	}

	// update_interval
	if !(ctx.UserConfig.UpdateInterval > 0) {
		log.Fatal("Please set update interval > 0 seconds.")
	}

	// backends
	if len(ctx.UserConfig.Backends) == 0 {
		log.Fatal("Please define at least one collector backend.")
	}

	return nil
}
Ejemplo n.º 2
0
func (ctx *Ctx) LoadConfig(path *string) error {
	err := cfgfile.Read(&ctx.UserConfig, *path)
	if err != nil {
		return fmt.Errorf("loading config file error: %v\n", err)
	}

	// Process top-level configuration
	// server_url
	ctx.ServerUrl, err = url.Parse(ctx.UserConfig.ServerUrl)
	if err != nil || ctx.ServerUrl.Scheme == "" || ctx.ServerUrl.Host == "" {
		log.Fatal("Server-url is not valid. Should be like http://127.0.0.1:12900 ", err)
	}
	if ctx.UserConfig.ServerUrl == "" {
		log.Fatalf("Server-url is empty.")
	}

	// collector_id
	if ctx.UserConfig.CollectorId == "" {
		log.Fatal("No collector ID was configured.")
	}
	ctx.CollectorId = common.GetCollectorId(ctx.UserConfig.CollectorId)

	// node_id
	if ctx.UserConfig.NodeId == "" {
		log.Info("No node-id was configured, falling back to hostname")
		ctx.UserConfig.NodeId, err = common.GetHostname()
		if err != nil {
			log.Fatal("No node-id configured and not able to obtain hostname as alternative.")
		}
	}

	// tags
	if len(ctx.UserConfig.Tags) == 0 {
		log.Fatal("Please define configuration tags.")
	} else {
		log.Info("Fetching configurations tagged by: ", ctx.UserConfig.Tags)
	}

	// log_path
	if ctx.UserConfig.LogPath == "" {
		log.Fatal("No log directory was configured.")
	}

	// log_rotation_time
	if !(ctx.UserConfig.LogRotationTime > 0) {
		log.Fatal("Please set the log rotation time > 0 seconds.")
	}

	// log_max_age
	if !(ctx.UserConfig.LogMaxAge > 0) {
		log.Fatal("Please set the maximum age of log file rotation > 0 seconds.")
	}

	// update_interval
	if !(ctx.UserConfig.UpdateInterval > 0) {
		log.Fatal("Please set update interval > 0 seconds.")
	}

	// backends
	if len(ctx.UserConfig.Backends) == 0 {
		log.Fatal("Please define at least one collector backend.")
	}

	return nil
}