Пример #1
2
func main() {
	c, err := config.ReadDefault("wiki.ini")
	panicIni(err)
	wikiName, err = c.String("wiki", "name")
	panicIni(err)
	servAddr, err := c.String("wiki", "serv_addr")
	panicIni(err)
	inDevMode, err = c.Bool("wiki", "dev_mode")
	panicIni(err)
	log.Printf("Read wiki.ini")

	views = template.Must(template.ParseGlob("views/[a-z]*.html"))
	log.Printf("Parsed page templates\n")

	http.HandleFunc("/", rootHandler)
	http.HandleFunc("/delete/", deleteHandler)
	http.HandleFunc("/restore/", restoreHandler)
	http.HandleFunc("/edit/", editHandler)
	http.HandleFunc("/preview", previewHandler)
	http.HandleFunc("/save/", saveHandler)
	http.HandleFunc("/pages", pagesHandler)
	http.HandleFunc("/deleted", deletedHandler)
	http.HandleFunc("/versions/", versionsHandler)
	http.HandleFunc("/search", searchHandler)
	http.Handle("/pub/", http.StripPrefix("/pub/", http.FileServer(http.Dir("pub"))))
	http.HandleFunc("/favicon.ico", faviconHandler)
	log.Printf("Serving wiki pages from %s...\n", servAddr)
	log.Fatal(http.ListenAndServe(servAddr, nil))
}
Пример #2
0
// SetFile parses a config file.  If the filename that is provided does not
// exist, the file is created and the default sections and options are written
// as comments.  This function should only be called after all options are
// registered using the Register Function, so take care putting it in init
// functions.
func SetFile(fileName string) {
	var err error
	c, err = config.ReadDefault(fileName)
	if err != nil {
		WriteDefaultFile(fileName)
		c, err = config.ReadDefault(fileName)
		if err != nil {
			panic(err)
		}
	}
}
Пример #3
0
func NewFileStorageResolver(schemasPath, aggregationPath string) (*fileStorageResolver, error) {
	resolver := new(fileStorageResolver)
	schemas, err := config.ReadDefault(schemasPath)
	if err != nil {
		return nil, err
	}
	resolver.schemas = schemas
	aggregation, err := config.ReadDefault(aggregationPath)
	if err != nil {
		return nil, err
	}
	resolver.aggregation = aggregation

	return resolver, nil
}
Пример #4
0
func new_address_matcher() *address_matcher {
	var cfg *config.Config
	var err os.Error

	// honor NOTMUCH_CONFIG
	home := os.Getenv("NOTMUCH_CONFIG")
	if home == "" {
		home = os.Getenv("HOME")
	}

	if cfg, err = config.ReadDefault(path.Join(home, ".notmuch-config")); err != nil {
		log.Fatalf("error loading config file:", err)
	}

	db_path, _ := cfg.String("database", "path")
	primary_email, _ := cfg.String("user", "primary_email")
	addrbook_tag, err := cfg.String("user", "addrbook_tag")
	if err != nil {
		addrbook_tag = "addressbook"
	}

	self := &address_matcher{db: nil,
		user_db_path:       db_path,
		user_primary_email: primary_email,
		user_addrbook_tag:  addrbook_tag}
	return self
}
Пример #5
0
// Config parser with reflection to fill fields.
//
func NewConfig(filename string) (*Config, error) {
	c, e := config.ReadDefault(filename)
	if e != nil {
		return nil, e
	}
	return &Config{Config: *c}, nil
}
Пример #6
0
func main() {
	flag.Parse()

	c, err := config.ReadDefault(*configFile)
	if err != nil {
		log.Fatal("Couldn't find the config file: ", err)
	}

	host, _ := c.String("network", "listen-ip")
	port, _ := c.Int("network", "listen-port")
	listen := fmt.Sprintf("%s:%d", host, port)

	StorageLocation, err = c.String("storage", "location")
	if err != nil {
		StorageLocation = "./"
	}
	log.Println("Storing/Retrieving data from ", StorageLocation)

	_, err = os.Stat(StorageLocation)
	if err != nil {
		log.Fatal("Folder not exist: ", err)
	}

	http.HandleFunc("/", DataServer)

	log.Println("Server listening: ", listen)
	err = http.ListenAndServe(listen, nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
Пример #7
0
//Searches for, and loads, a configuration file.
func loadConfig() *config.Config {
	configDirs := strings.Split(os.Getenv("XDG_CONFIG_HOME")+":"+os.Getenv("XDG_CONFIG_DIRS"), ":")
	for _, d := range configDirs {
		cfg, _ := config.ReadDefault(d + "/android-notify-lite/config")
		if cfg != nil {
			configLocation = d + "/android-notify-lite/config"
			return cfg
		}
	}
	cfg, _ := config.ReadDefault(os.Getenv("HOME") + "/.android-notify-lite")
	configLocation = os.Getenv("HOME") + "/.android-notify-lite"
	if cfg == nil {
		fmt.Println("Error: No configuration file found.")
		os.Exit(1)
	}
	return cfg
}
Пример #8
0
func config_init(file string) error {
	f, err := os.Open(file)
	defer f.Close()
	if err == nil {
		Config, err = config.ReadDefault(file)
		main_config_file = file
	}
	return err
}
Пример #9
0
func main() {
	flag.Parse()
	c, err := config.ReadDefault(*configPath)
	if err != nil {
		log.Fatal(err)
		os.Exit(-1)
	}
	pm := new(PageManager)
	pm.StartServer(c)
}
func main() {
	c, _ := config.ReadDefault("../../etc/config.cfg")

	client_public_key, _ := c.String("Client", "public_key")
	server_public_key, _ := c.String("Server", "public_key")
	server_max_cores, _ := c.Int("Server", "max_cores")

	fmt.Println("client public key: ", client_public_key)
	fmt.Println("server public key: ", server_public_key)
	fmt.Println("server max cores: ", server_max_cores)
}
Пример #11
0
func loadConfig() {
	//read config
	c, _ := config.ReadDefault("config.ini")
	mode, _ = c.String("main", "mode")
	server, _ = c.String("client", "server")
	client_listen, _ = c.String("client", "listen")
	server_listen, _ = c.String("server", "listen")
	ck, _ = c.String("encrypto", "client-key")
	sk, _ = c.String("encrypto", "server-key")
	http_proxy, _ = c.String("client", "http-proxy")
}
Пример #12
0
func init() {
	flag.StringVar(&CONFIGFILE, "conf", "/usr/local/shock/conf/shock.cfg", "path to config file")
	flag.Parse()
	c, _ := config.ReadDefault(CONFIGFILE)
	// Shock
	SITEPORT, _ = c.Int("Shock", "site-port")
	APIPORT, _ = c.Int("Shock", "api-port")

	// Admin
	ADMINEMAIL, _ = c.String("Admin", "email")
	SECRETKEY, _ = c.String("Admin", "secretkey")

	// Directories
	SITEPATH, _ = c.String("Directories", "site")
	DATAPATH, _ = c.String("Directories", "data")
	LOGSPATH, _ = c.String("Directories", "logs")

	// Mongodb
	MONGODB, _ = c.String("Mongodb", "hosts")

	// parse Node-Indices
	NODEIDXS = map[string]idxOpts{}
	nodeIdx, _ := c.Options("Node-Indices")
	for _, opt := range nodeIdx {
		val, _ := c.String("Node-Indices", opt)
		opts := idxOpts{}
		for _, parts := range strings.Split(val, ",") {
			p := strings.Split(parts, ":")
			if p[0] == "unique" {
				if p[1] == "true" {
					opts.unique = true
				} else {
					opts.unique = false
				}
			} else if p[0] == "dropDups" {
				if p[1] == "true" {
					opts.dropDups = true
				} else {
					opts.dropDups = false
				}
			} else if p[0] == "sparse" {
				if p[1] == "true" {
					opts.sparse = true
				} else {
					opts.sparse = false
				}
			}
		}
		NODEIDXS[opt] = opts
	}
}
Пример #13
0
func ReadConfig() error {
	conf, err := config.ReadDefault(ConfigFilename)
	if err != nil {
		return err
	}
	var (
		repostr string
		hoststr string
		license string
	)
	AppConfig.Name, err = conf.String("variables", "name")
	AppConfig.Email, err = conf.String("variables", "email")
	AppConfig.HostUser, err = conf.String("general", "hostuser")
	AppConfig.AltRoot, err = conf.String("general", "templates")
	AppConfig.Markdown, err = conf.Bool("general", "markdown")

	license, err = conf.String("general", "license")
	switch license {
	case "":
		AppConfig.License = NilLicense
	case "newbsd":
		AppConfig.License = NewBSDLicense
	}

	repostr, err = conf.String("general", "repo")
	switch repostr {
	case "":
		AppConfig.Repo = NilRepo
	case "git":
		AppConfig.Repo = Git
	//case "hg":
	//...
	default:
		AppConfig.Repo = NilRepo
	}

	hoststr, err = conf.String("general", "host")
	switch hoststr {
	case "":
		AppConfig.Host = NilHost
	case "github":
		AppConfig.Host = GitHubHost
		AppConfig.Repo = Git
	//case "googlecode":
	//...
	default:
		AppConfig.Host = NilHost
	}

	return nil
}
Пример #14
0
// LoadConfig reads and validates the server and oauth provider configuration from
// the provided config file path. If for some reason it cannot load the settings
// defined in the config file, or the config file cannot be read at all, it will
// return an error.
func LoadConfig(path string) error {
	// load config file
	configFile, err := config.ReadDefault(path)
	if err != nil {
		log.Fatal("Can't read config file:", err)
	}
	// validate server configuration
	serverConfig.ListenAddress = readConfigString(configFile, "server", "ListenAddress", "localhost:8080")
	serverConfig.ListenAddressTLS = readConfigString(configFile, "server", "ListenAddressTLS", "localhost:4443")
	if serverConfig.ListenAddress == "" && serverConfig.ListenAddressTLS == "" {
		return errors.New("At least one of ListenAddress or ListenAddressTLS must be present")
	}

	// Validate certs if SSL is enabled
	if serverConfig.ListenAddressTLS != "" {
		serverConfig.SSLCert = readConfigString(configFile, "server", "SSLCert", "")
		serverConfig.SSLKey = readConfigString(configFile, "server", "SSLKey", "")

		if serverConfig.SSLCert == "" {
			return errors.New("A SSL Certificate is required")
		}

		if serverConfig.SSLKey == "" {
			return errors.New("A SSL Key is required")
		}
	}

	serverConfig.CallbackPath = readConfigString(configFile, "server", "CallbackPath", "/oauth2callback")
	serverConfig.ProtectPath = readConfigString(configFile, "server", "ProtectPath", "/")
	serverConfig.CookieName = readConfigString(configFile, "server", "CookieName", "oauthproxy")
	serverConfig.ProxyURL = readConfigURL(configFile, "server", "ProxyURL", "http://example.com/")
	// Validate OAuth settings
	oauthProviderConfig.EmailRegexp = readConfigRegexp(configFile, "oauth", "EmailRegexp", ".*")
	oauthProviderConfig.oauthConfig.ClientId = readConfigString(configFile, "oauth", "ClientId", "")
	oauthProviderConfig.oauthConfig.ClientSecret = readConfigString(configFile, "oauth", "ClientSecret", "")
	scope := readConfigURL(configFile, "oauth", "Scope", "https://www.googleapis.com/auth/userinfo.email")
	oauthProviderConfig.oauthConfig.Scope = scope.String()
	authURL := readConfigURL(configFile, "oauth", "AuthURL", "https://accounts.google.com/o/oauth2/auth")
	oauthProviderConfig.oauthConfig.AuthURL = authURL.String()
	tokenURL := readConfigURL(configFile, "oauth", "TokenURL", "https://accounts.google.com/o/oauth2/token")
	oauthProviderConfig.oauthConfig.TokenURL = tokenURL.String()
	redirectURL := readConfigURL(configFile, "oauth", "RedirectURL", "http://testsite.com/oauth2callback")
	oauthProviderConfig.oauthConfig.RedirectURL = redirectURL.String()
	userInfoAPI := readConfigURL(configFile, "oauth", "UserInfoAPI", "https://www.googleapis.com/oauth2/v1/userinfo")
	oauthProviderConfig.UserInfoAPI = userInfoAPI.String()
	return nil
}
Пример #15
0
func read_config() {
	if len(os.Args) == 1 {
		panic("config")
	}
	cf := os.Args[1]
	conf, err := config.ReadDefault(cf)
	if err != nil {
		panic("config")
	}
	binlog_root_path, err = conf.String("DEFAULT", "base_path")
	if err != nil {
		panic("config")
	}
	root_path, err = conf.String("DEFAULT", "store_path0")
	if err != nil {
		panic("config")
	}
}
Пример #16
0
// Setup a config type from a filename.
func getConfig(configFileLoc string) (c *vafanConfig) {
	c = &vafanConfig{}
	file, err := config.ReadDefault(configFileLoc)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed reading configuration: %v", err)
		panic(err)
	}
	c.file = file

	// default
	c.baseDir = c.getString("default", "base-dir")
	c.host = c.getString("default", "host")
	c.port = c.getString("default", "port")
	c.youtubeDevKey = c.getString("default", "youtube-dev-key")

	// twitter
	tw := twitterConfig{}
	tw.user = c.getString("twitter", "user")
	tw.password = c.getString("twitter", "password")
	tw.consumerKey = c.getString("twitter", "consumer-key")
	tw.consumerSecret = c.getString("twitter", "consumer-secret")
	tw.accessToken = c.getString("twitter", "access-token")
	tw.accessSecret = c.getString("twitter", "access-secret")
	c.twitter = tw

	// mysql
	my := mysqlConfig{}
	my.user = c.getString("mysql", "user")
	my.password = c.getString("mysql", "password")
	c.mysql = my

	// redis
	rd := redisConfig{}
	rd.address = c.getString("redis", "address")
	c.redis = rd

	return
}
Пример #17
0
// UserConfig loads configuration per user, if any.
func (c *Conf) UserConfig() error {
	home := os.Getenv("HOME")
	if home == "" {
		return fmt.Errorf("environment variable $HOME is not set")
	}

	pathUserConfig := filepath.Join(home, _USER_CONFIG)

	// To know if the file exist.
	switch stat, err := os.Stat(pathUserConfig); {
	case os.IsNotExist(err):
		return nil
	case stat.Mode()&os.ModeType != 0:
		return fmt.Errorf("expected file: %s", _USER_CONFIG)
	}

	cfg, err := config.ReadDefault(pathUserConfig)
	if err != nil {
		return fmt.Errorf("error parsing configuration: %s", err)
	}

	// == Get values
	var errKeys []string
	ok := true

	if c.Org == "" {
		if c.Org, err = cfg.String("DEFAULT", "org"); err != nil {
			ok = false
			errKeys = append(errKeys, "org")
		}
	}
	if c.Author == "" {
		if c.Author, err = cfg.String("DEFAULT", "author"); err != nil {
			ok = false
			errKeys = append(errKeys, "author")
		}
	}
	if c.Email == "" {
		if c.Email, err = cfg.String("DEFAULT", "email"); err != nil {
			ok = false
			errKeys = append(errKeys, "email")
		}
	}
	if c.License == "" {
		if c.License, err = cfg.String("DEFAULT", "license"); err != nil {
			ok = false
			errKeys = append(errKeys, "license")
		}
	}
	if c.VCS == "" {
		if c.VCS, err = cfg.String("DEFAULT", "vcs"); err != nil {
			ok = false
			errKeys = append(errKeys, "vcs")
		}
	}
	if len(c.ImportPaths) == 0 {
		var imports string

		if imports, err = cfg.String("DEFAULT", "import"); err != nil {
			ok = false
			errKeys = append(errKeys, "import")
		} else {
			c.ImportPaths = strings.Split(imports, ":")
		}
	}

	if !ok {
		return fmt.Errorf("error at user configuration: %s\n",
			strings.Join(errKeys, ","))
	}
	return nil
}
Пример #18
0
func Errplane_main(defaults econfig.Consts) {
	fmt.Printf("ERRPlane Local Agent starting, Version %s \n", defaults.Current_build)

	goopt.Description = func() string {
		return "ERRPlane Local Agent."
	}
	goopt.Version = defaults.Current_build
	goopt.Summary = "ErrPlane Log and System Monitor"
	goopt.Parse(nil)

	var fconfig_file string
	fconfig_file = *config_file

	fmt.Printf("Loading config file %s.\n", fconfig_file)

	c, err := config.ReadDefault(fconfig_file)
	if err != nil {
		log.Fatal("Can not find the Errplane Config file, please install it in /etc/ranger/ranger.conf.")
	}

	api_key, _ := c.String("DEFAULT", "api_key")

	if len(*install_api_key) > 1 {
		fmt.Printf("Saving new Config!\n")
		c.AddOption("DEFAULT", "api_key", *install_api_key)
		c.WriteFile(fconfig_file, 0644, "")

		c, err := config.ReadDefault(fconfig_file)
		if err != nil {
			log.Fatal("Can not find the Errplane Config file, please install it in /etc/ranger/ranger.conf.")
		}
		api_key, _ = c.String("DEFAULT", "api_key")
	}

	if len(api_key) < 1 {
		log.Fatal("No api key found. Please rerun this with --install-api-key <api_key_here> ")
		os.Exit(1)
	}

	if !*amForeground {
		//Daemonizing requires root
		if os.Getuid() == 0 {
			daemonize.Do_fork(os.Args[0], fconfig_file)
			l4g.Fine("Exiting parent process-%s\n", os.Args[0])
			os.Exit(0)
		} else {
			fmt.Printf("Daemoning requires root \n")
			os.Exit(1)
		}
	}

	setup_logger()

	api_url, _ := c.String("DEFAULT", "api_host")
	config_url, _ := c.String("DEFAULT", "config_host")
	output_dir, _ := c.String("DEFAULT", "agent_path")
	if len(output_dir) < 1 {
		output_dir = "/usr/local/ranger/"
	}
	agent_bin, _ := c.String("DEFAULT", "agent_bin")
	if len(agent_bin) < 1 {
		agent_bin = "/usr/local/bin/ranger-local-agent"
	}
	pid_location, _ := c.String("DEFAULT", "pid_file")
	if len(pid_location) < 1 {
		pid_location = "/var/run/ranger/ranger.pid"
	}
	auto_update, _ := c.String("DEFAULT", "auto_upgrade")

	write_pid(pid_location)

	config_data := econfig.ParseJsonFromHttp(config_url, api_key)

	l4g.Debug("Expected agent version-%s\n", config_data.Version)

	if auto_update == "true" && config_data.Version != defaults.Current_build {
		econfig.Upgrade_version(config_data.Version, config_data.Sha256, output_dir, agent_bin, defaults)
		os.Exit(1)
	} else {
		l4g.Debug("Don't need to upgrade versions\n")
	}

	_, err = exec.LookPath("tail")
	if err != nil {
		log.Fatal("installing tail is in your future")
		//        exit(1)
	}

	configChan := make(chan *econfig.AgentConfigType)

	go theBrain(configChan, api_key, api_url)

	go econfig.CheckForUpdatedConfigs(auto_update, config_url, api_key, output_dir, agent_bin, configChan, defaults)

	if err != nil {
		log.Fatal(err)
	}

	err = nil
	for err == nil {
		//TODO monitor go routines, if one exists reload it
		time.Sleep(0)
		runtime.Gosched()
	}
}
Пример #19
0
func main() {
	goopt.Version = version
	goopt.Summary = "send emc vnx performance data to graphite"
	goopt.Parse(nil)

	if f, _ := exists(*opt_conf); f == false {
		fmt.Print(goopt.Help())
		fmt.Println("ERROR: config file " + *opt_conf + " doesn't exist")
		return
	}
	c, _ := config.ReadDefault(*opt_conf)

	host, _ := c.String("graphite", "host")
	port, _ := c.Int("graphite", "port")
	timeout, _ := c.Int("graphite", "timeout")
	basename, _ := c.String("graphite", "basename")
	//todo: we also can parse this from the output...
	timestamp := time.Now().Unix()

	log(fmt.Sprintf("using graphite with host %s:%d", host, port))

	if f, _ := exists(*opt_data); f == false {
		fmt.Print(goopt.Help())
		fmt.Println("ERROR: data file " + *opt_data + " doesn't exist")
		return
	}
	lines, err := readLines(*opt_data)
	if err != nil {
		fmt.Println("ERROR:", err)
		return
	}
	//we only want to use the head and the first line of data
	head := strings.Split(lines[0], ",")
	data := strings.Split(lines[1], ",")

	if len(head) != len(data) {
		fmt.Println("ERROR: malformed csv (length of head != length of data")
		return
	}

	//create the graphite connection.
	// Todo: export this in a small pkg

	cstr := fmt.Sprintf("%s:%d", host, port)
	log("trying to connect to " + cstr)
	conn, err := net.DialTimeout("tcp", cstr, time.Duration(timeout)*time.Second)

	if err != nil {
		fmt.Println("ERROR:", err)
		return
	}

	for n, val := range head {
		key := stringify(val)
		value := stringify(data[n])
		if key != "Timestamp" {
			msg := fmt.Sprintf("%s.%s.%s.%s %s %d", basename, *opt_mover, *opt_statsname, key, value, timestamp)
			log("sending: " + msg)
			fmt.Fprint(conn, "\n"+msg+"\n")
		} else {
			log("Timestamp: ... next...")
		}
	}
	conn.Close()
}
Пример #20
0
func loadConf(conf string) (*config.Config, error) {
	return config.ReadDefault(conf)
}
Пример #21
0
func init() {
	flag.StringVar(&CONFIG_FILE, "conf", "/usr/local/shock/conf/shock.cfg", "path to config file")
	flag.StringVar(&RELOAD, "reload", "", "path or url to shock data. WARNING this will drop all current data.")
	flag.Parse()
	c, err := config.ReadDefault(CONFIG_FILE)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: error reading conf file: %v\n", err)
		os.Exit(1)
	}

	// Ports
	SITE_PORT, _ = c.Int("Ports", "site-port")
	API_PORT, _ = c.Int("Ports", "api-port")

	// SSL
	SSL_ENABLED, _ = c.Bool("SSL", "enable")
	if SSL_ENABLED {
		SSL_KEY_FILE, _ = c.String("SSL", "key")
		SSL_CERT_FILE, _ = c.String("SSL", "cert")
	}

	// Access-Control
	ANON_WRITE, _ = c.Bool("Anonymous", "write")
	ANON_READ, _ = c.Bool("Anonymous", "read")
	ANON_CREATEUSER, _ = c.Bool("Anonymous", "create-user")

	// Auth
	AUTH_TYPE, _ = c.String("Auth", "type")
	switch AUTH_TYPE {
	case "globus":
		GLOBUS_TOKEN_URL, _ = c.String("Auth", "globus_token_url")
		GLOBUS_PROFILE_URL, _ = c.String("Auth", "globus_profile_url")
	case "oauth":
		OAUTH_REQUEST_TOKEN_URL, _ = c.String("Auth", "oauth_request_token_url")
		OAUTH_AUTH_TOKEN_URL, _ = c.String("Auth", "oauth_auth_token_url")
		OAUTH_ACCESS_TOKEN_URL, _ = c.String("Auth", "oauth_access_token_url")
	case "basic":
		// nothing yet
	}

	// Admin
	ADMIN_EMAIL, _ = c.String("Admin", "email")
	SECRET_KEY, _ = c.String("Admin", "secretkey")

	// Directories
	SITE_PATH, _ = c.String("Directories", "site")
	DATA_PATH, _ = c.String("Directories", "data")
	LOGS_PATH, _ = c.String("Directories", "logs")

	// Mongodb
	MONGODB, _ = c.String("Mongodb", "hosts")

	// parse Node-Indices
	NODE_IDXS = map[string]idxOpts{}
	nodeIdx, _ := c.Options("Node-Indices")
	for _, opt := range nodeIdx {
		val, _ := c.String("Node-Indices", opt)
		opts := idxOpts{}
		for _, parts := range strings.Split(val, ",") {
			p := strings.Split(parts, ":")
			if p[0] == "unique" {
				if p[1] == "true" {
					opts.unique = true
				} else {
					opts.unique = false
				}
			} else if p[0] == "dropDups" {
				if p[1] == "true" {
					opts.dropDups = true
				} else {
					opts.dropDups = false
				}
			} else if p[0] == "sparse" {
				if p[1] == "true" {
					opts.sparse = true
				} else {
					opts.sparse = false
				}
			}
		}
		NODE_IDXS[opt] = opts
	}

}
Пример #22
0
import (
	"log"

	"fmt"
	"math/rand"
	"net"
	"time"

	"./pb"
	"code.google.com/p/goprotobuf/proto"

	"github.com/kless/goconfig/config"
)

var c, _ = config.ReadDefault("config.conf")
var sensorHost, _ = c.String("sensor", "host")
var sensorDelayRaw, _ = c.String("sensor", "delay")
var sensorDelay, _ = time.ParseDuration(sensorDelayRaw)
var monitorHost, _ = c.String("monitor", "host")
var monitorPort, _ = c.Int("monitor", "port")

func main() {
	log.Printf("Host name configured as: %s", sensorHost)
	log.Printf("Monitor configured at:   %s:%d", monitorHost, monitorPort)
	log.Printf("Will sleep %s between sending stats", sensorDelayRaw)

	//  dial to local monitor
	// go Reader(c)

	for {
Пример #23
0
func readConf(fn string, realm string, common Config) (c Config, err error) {
	if fn == "" {
		fn = ConfigFile
	}
	conf, e := config.ReadDefault(fn)
	if e != nil {
		err = e
		return
	}
	if common.LogConf != "" {
		c.LogConf = common.LogConf
	} else {
		logconf, e2 := conf.String("log", "config")
		if e2 != nil {
			fmt.Printf("cannot get log configuration: %s", e2)
			c.LogConf = DefaultLogConfFile
		} else {
			UseLoggerFromConfigFile(logconf)
			c.LogConf = logconf
		}
	}

	c.StagingDir, err = getDir(conf, "dirs", "staging", realm)
	if err != nil {
		return
	}

	c.IndexDir, err = getDir(conf, "dirs", "index", realm)
	if err != nil {
		return c, err
	}
	if realm != "" {
		for i := 0; i < 2; i++ {
			dn := filepath.Join(c.IndexDir, fmt.Sprintf("L%02d", i))
			if !fileExists(dn) {
				if err = os.MkdirAll(dn, 0755); err != nil {
					return c, err
				}
			}
		}
	}

	c.TarDir, err = getDir(conf, "dirs", "tar", realm)
	if err != nil {
		return c, err
	}

	var i int
	if common.IndexThreshold > 0 {
		c.IndexThreshold = common.IndexThreshold
	} else {
		i, err = conf.Int("threshold", "index")
		if err != nil {
			logger.Warn("cannot get threshold/index: ", err)
			c.IndexThreshold = DefaultIndexThreshold
		} else {
			c.IndexThreshold = uint(i)
		}
	}

	if common.TarThreshold > 0 {
		c.TarThreshold = common.TarThreshold
	} else {
		i, err = conf.Int("threshold", "tar")
		if err != nil {
			logger.Warn("cannot get threshold/tar: ", err)
			c.TarThreshold = DefaultTarThreshold
		} else {
			c.TarThreshold = uint64(i)
		}
	}

	if common.Hostport != "" {
		c.Hostport = common.Hostport
	} else {
		var hp string
		hp, err = conf.String("http", "hostport")
		if err != nil {
			logger.Warn("cannot get hostport: ", err)
			c.Hostport = DefaultHostport
		} else {
			c.Hostport = hp
		}
	}

	if len(common.Realms) > 0 {
		c.Realms = common.Realms
	} else {
		var realms string
		realms, err = conf.String("http", "realms")
		if err != nil {
			logger.Warn("cannot get realms: ", err)
		} else {
			c.Realms = strings.Split(realms, ",")
		}
	}

	hash := DefaultContentHash
	if common.ContentHash != "" {
		hash = common.ContentHash
	} else {
		hash, err = conf.String("hash", "content")
		if err != nil {
			logger.Warn("cannot get content hash: ", err)
			err = nil
		}
	}
	c.ContentHash = hash
	switch hash {
	case "sha512":
		c.ContentHashFunc = sha512.New
	case "sha256":
		c.ContentHashFunc = sha256.New
	default:
		c.ContentHashFunc = sha1.New
		c.ContentHash = "sha1"
	}

	c.CompressMethod = DefaultCompressMethod
	if common.CompressMethod != "" {
		c.CompressMethod = common.CompressMethod
	} else {
		c.CompressMethod, err = conf.String("compress", "method")
		if err != nil {
			logger.Warn("cannot get compress method: ", err)
			err = nil
		}
	}

	return c, err
}
Пример #24
0
func main() {
	flag.Parse()
	c, err := config.ReadDefault(*conf)
	if err != nil {
		log.Fatal("read config failed", *conf, err.Error())
	}
	if threads, e := c.Int("default", "threads"); e == nil {
		runtime.GOMAXPROCS(threads)
	}

	default_server_port, e := c.String("default", "server_port")
	if e != nil {
		default_server_port = "7900"
	}
	serverss, e := c.String("default", "servers")
	if e != nil {
		log.Fatal("no servers in conf")
	}
	servers := strings.Split(serverss, ",")
	for i := 0; i < len(servers); i++ {
		s := servers[i]
		if strings.Contains(s, "-") {
			n := len(s)
			start, _ := strconv.Atoi(s[n-3 : n-2])
			end, _ := strconv.Atoi(s[n-1:])
			for j := start + 1; j <= end; j++ {
				servers = append(servers, fmt.Sprintf("%s%d", s[:n-3], j))
			}
			s = s[:n-2]
			servers[i] = s
		}
		if !strings.Contains(s, ":") {
			servers[i] = s + ":" + default_server_port
		}
	}
	//log.Println(servers)
	sort.Strings(servers)

	if port, e := c.Int("monitor", "port"); e != nil {
		log.Print("no port in conf", e.Error())
	} else {
		server_stats = make([]map[string]interface{}, len(servers))
		bucket_stats = make([]string, 16)
		go update_stats(servers, server_stats, true)

		proxys, e := c.String("monitor", "proxy")
		if e != nil {
			proxys = fmt.Sprintf("localhost:%d", port)
		}
		proxies := strings.Split(proxys, ",")
		proxy_stats = make([]map[string]interface{}, len(proxies))
		go update_stats(proxies, proxy_stats, false)

		http.Handle("/", http.HandlerFunc(Status))
		http.Handle("/static/", http.FileServer(http.Dir("./")))
		go func() {
			listen, e := c.String("monitor", "listen")
			if e != nil {
				listen = "0.0.0.0"
			}
			addr := fmt.Sprintf("%s:%d", listen, port)
			lt, e := net.Listen("tcp", addr)
			if e != nil {
				log.Println("monitor listen failed on ", addr, e)
			}
			log.Println("monitor listen on ", addr)
			http.Serve(lt, nil)
		}()
	}

	AllocLimit = *allocLimit

	if *debug {
		AccessLog = log.New(os.Stdout, "", log.Ldate|log.Ltime)
	} else if accesslog, e := c.String("proxy", "accesslog"); e == nil {
		logf, err := os.OpenFile(accesslog, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
		if err != nil {
			log.Print("open " + accesslog + " failed:" + err.Error())
		}
		AccessLog = log.New(logf, "", log.Ldate|log.Ltime)
	}
	slow, err := c.Int("proxy", "slow")
	if err != nil {
		slow = 100
	}
	SlowCmdTime = time.Duration(int64(slow) * 1e6)

	schd = NewAutoScheduler(servers, 16)
	client := NewClient(schd)
	n := len(servers)
	client.N = min(n, 3)
	client.W = min(n, 2)
	proxy := NewServer(client)
	listen, e := c.String("proxy", "listen")
	if e != nil {
		listen = "0.0.0.0"
	}
	port, e := c.Int("proxy", "port")
	if e != nil {
		log.Fatal("no proxy port in conf", e.Error())
	}
	addr := fmt.Sprintf("%s:%d", listen, port)
	if e = proxy.Listen(addr); e != nil {
		log.Fatal("proxy listen failed", e.Error())
	}

	log.Println("proxy listen on ", addr)
	proxy.Serve()
	log.Print("shut down gracefully.")
}