Ejemplo n.º 1
0
func loadJSONConfig(cfgFile string) {
	cfgFile = filepath.Clean(cfgFile)
	cfgPath := getCfgAbsPath(cfgFile)

	if !file.IsExist(cfgPath) {
		log.Fatalln("Configuration file [", cfgFile, "] doesn't exist")
	}

	configContent, err := file.ToTrimString(cfgPath)
	if err != nil {
		log.Fatalln("Reading configuration file [", cfgFile, "] failed:", err)
	}

	var c JSONConfigFile
	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		log.Fatalln("Parsing configuration file [", cfgFile, "] failed:", err)
	}

	jsonCfgLock.Lock()
	defer jsonCfgLock.Unlock()

	jsonConfig = &c

	log.Println("Reading configuration file [", cfgFile, "] succeeded")
}
Ejemplo n.º 2
0
func ParseConfig(cfg string) error {
	if cfg == "" {
		return fmt.Errorf("use -c to specify configuration file")
	}

	if !file.IsExist(cfg) {
		return fmt.Errorf("config file %s is nonexistent", cfg)
	}

	ConfigFile = cfg

	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		return fmt.Errorf("read config file %s fail %s", cfg, err)
	}

	var c GlobalConfig
	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		return fmt.Errorf("parse config file %s fail %s", cfg, err)
	}

	configLock.Lock()
	defer configLock.Unlock()

	config = &c

	log.Println("read config file:", cfg, "successfully")
	return nil
}
Ejemplo n.º 3
0
Archivo: cfg.go Proyecto: donh/query
func ParseConfig(cfg string) {
	if cfg == "" {
		log.Fatalln("config file not specified: use -c $filename")
	}

	if !file.IsExist(cfg) {
		log.Fatalln("config file specified not found:", cfg)
	}

	ConfigFile = cfg

	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		log.Fatalln("read config file", cfg, "error:", err.Error())
	}

	var c GlobalConfig
	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		log.Fatalln("parse config file", cfg, "error:", err.Error())
	}

	SetConfig(&c)

	logger.InitLogger(c.Debug)
	log.Println("g.ParseConfig ok, file", cfg)
}
Ejemplo n.º 4
0
Archivo: cfg.go Proyecto: niean/godemos
func ParseConfig(cfg string) {
	if cfg == "" {
		log.Fatalln("use -c to specify one config file")
	}

	if !file.IsExist(cfg) {
		log.Fatalln("config file:", cfg, "not exist")
	}

	ConfigFile = cfg

	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		log.Fatalln("read config file:", cfg, "failed:", err)
	}

	var c GlobalConfig
	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		log.Fatalln("parse config file:", cfg, "failed:", err)
	}

	// check
	if !checkConfig(c) {
		log.Fatalln("check config file:", cfg, "failed")
	}

	configLock.Lock()
	defer configLock.Unlock()
	config = &c

	log.Println("g.ParseConfig ok, file ", cfg)
}
Ejemplo n.º 5
0
// Reference to Open-Falcon's ParseConfig()
func ParseConfig(cfg string) {
	if cfg == "" {
		log.Fatalln("Config file not specified: use -c $filename")
	}

	if !file.IsExist(cfg) {
		log.Fatalln("Config file specified not found:", cfg)
	}

	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		log.Fatalln("Read config file", cfg, "error:", err.Error())
	}

	var c GlobalConfig
	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		log.Fatalln("Parse config file", cfg, "error:", err.Error())
	}

	// set config
	configLock.Lock()
	defer configLock.Unlock()
	Config = &c

	printDebug(2, "ParseConfig:", cfg, "[DONE.]")
}
Ejemplo n.º 6
0
Archivo: cfg.go Proyecto: donh/agent
func ParseConfig(cfg string) {
	if cfg == "" {
		log.Fatalln("use -c to specify configuration file")
	}

	if !file.IsExist(cfg) {
		log.Fatalln("config file:", cfg, "is not existent. maybe you need `mv cfg.example.json cfg.json`")
	}

	ConfigFile = cfg

	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		log.Fatalln("read config file:", cfg, "fail:", err)
	}

	var c GlobalConfig
	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		log.Fatalln("parse config file:", cfg, "fail:", err)
	}

	lock.Lock()
	defer lock.Unlock()

	config = &c

	log.Println("read config file:", cfg, "successfully")
}
Ejemplo n.º 7
0
func ParseConfig(cfg string) {
	if cfg == "" {
		log.Fatalln("use -c to specify configuration file")
	}

	if !file.IsExist(cfg) {
		log.Fatalln("config file:", cfg, "is not existent")
	}

	ConfigFile = cfg

	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		log.Fatalln("read config file:", cfg, "fail:", err)
	}

	var c GlobalConfig
	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		log.Fatalln("parse config file:", cfg, "fail:", err)
	}

	configLock.Lock()
	defer configLock.Unlock()

	config = &c

	logger.SetLevel(config.LogLevel)
	log.Println("read config file:", cfg, "successfully")
}
Ejemplo n.º 8
0
func ParseConfig(cfg string) {
	if cfg == "" {
		log.Fatalln("config file not specified: use -c $filename")
	}

	if !file.IsExist(cfg) {
		log.Fatalln("config file specified not found:", cfg)
	}

	ConfigFile = cfg

	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		log.Fatalln("read config file", cfg, "error:", err.Error())
	}

	var c GlobalConfig
	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		log.Fatalln("parse config file", cfg, "error:", err.Error())
	}

	// set config
	configLock.Lock()
	defer configLock.Unlock()
	config = &c

	log.Println("g.ParseConfig ok, file", cfg)
}
Ejemplo n.º 9
0
func ParseConfig(cfg string) {
	if cfg == "" {
		log.Fatalln("use -c to specify configuration file")
	}

	if !file.IsExist(cfg) {
		log.Fatalln("config file:", cfg, "is not existent")
	}

	ConfigFile = cfg

	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		log.Fatalln("read config file:", cfg, "fail:", err)
	}

	var c GlobalConfig
	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		log.Fatalln("parse config file:", cfg, "fail:", err)
	}
	if !file.IsExist(c.ExternalNodes) {
		log.Printf("WARN: the external_nodes file [%s] is not exist!", c.ExternalNodes)
		c.ExternalNodes = ""
	}

	configLock.Lock()
	defer configLock.Unlock()

	config = &c

	log.Println("read config file:", cfg, "successfully")
}
Ejemplo n.º 10
0
func StopAgentOf(agentName, newVersion string) error {
	agentDir := path.Join(g.SelfDir, agentName)
	versionFile := path.Join(agentDir, ".version")

	if !file.IsExist(versionFile) {
		log.Printf("WARN: %s is nonexistent", versionFile)
		return nil
	}

	version, err := file.ToTrimString(versionFile)
	if err != nil {
		log.Printf("WARN: read %s fail %s", version, err)
		return nil
	}

	if version == newVersion {
		// do nothing
		return nil
	}

	versionDir := path.Join(agentDir, version)
	if !file.IsExist(versionDir) {
		log.Printf("WARN: %s nonexistent", versionDir)
		return nil
	}

	return ControlStopIn(versionDir)
}
Ejemplo n.º 11
0
func ParseConfig(cfg string) {
	if cfg == "" {
		log.Fatalln("use -c to specify configuration file")
	}

	if !file.IsExist(cfg) {
		log.Fatalln("config file:", cfg, "is not existent. maybe you need `mv cfg.example.json cfg.json`")
	}

	ConfigFile = cfg

	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		log.Fatalln("read config file:", cfg, "fail:", err)
	}

	var c GlobalConfig
	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		log.Fatalln("parse config file:", cfg, "fail:", err)
	}

	// split cluster config
	c.Judge.Cluster2 = formatClusterItems(c.Judge.Cluster)
	c.Graph.Cluster2 = formatClusterItems(c.Graph.Cluster)
	c.Graph.ClusterMigrating2 = formatClusterItems(c.Graph.ClusterMigrating)

	configLock.Lock()
	defer configLock.Unlock()
	config = &c

	log.Println("g.ParseConfig ok, file ", cfg)
}
Ejemplo n.º 12
0
func ParseConfig(cfg string) {
	if cfg == "" {
		log.Fatalln("config file not specified: use -c $filename")
	}

	if !file.IsExist(cfg) {
		log.Fatalln("config file specified not found:", cfg)
	}

	ConfigFile = cfg

	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		log.Fatalln("read config file", cfg, "error:", err.Error())
	}

	var c GlobalConfig
	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		log.Fatalln("parse config file", cfg, "error:", err.Error())
	}

	if c.Migrate.Enabled && len(c.Migrate.Cluster) == 0 {
		c.Migrate.Enabled = false
	}

	// set config
	atomic.StorePointer(&ptr, unsafe.Pointer(&c))

	log.Println("g.ParseConfig ok, file", cfg)
}
Ejemplo n.º 13
0
Archivo: system.go Proyecto: ziyel/nux
func SystemUptime() (days, hours, mins int64, err error) {
	var content string
	content, err = file.ToTrimString("/proc/uptime")
	if err != nil {
		return
	}

	fields := strings.Fields(content)
	if len(fields) < 2 {
		err = fmt.Errorf("/proc/uptime format not supported")
		return
	}

	secStr := fields[0]
	var secF float64
	secF, err = strconv.ParseFloat(secStr, 64)
	if err != nil {
		return
	}

	minTotal := secF / 60.0
	hourTotal := minTotal / 60.0

	days = int64(hourTotal / 24.0)
	hours = int64(hourTotal) - days*24
	mins = int64(minTotal) - (days * 60 * 24) - (hours * 60)

	return
}
Ejemplo n.º 14
0
func BuildHeartbeatRequest(hostname string, agentDirs []string) model.HeartbeatRequest {
	req := model.HeartbeatRequest{Hostname: hostname}

	realAgents := []*model.RealAgent{}
	now := time.Now().Unix()

	for _, agentDir := range agentDirs {
		// 如果目录下没有.version,我们认为这根本不是一个agent
		versionFile := path.Join(g.SelfDir, agentDir, ".version")
		if !f.IsExist(versionFile) {
			continue
		}

		version, err := f.ToTrimString(versionFile)
		if err != nil {
			log.Printf("read %s/.version fail: %v", agentDir, err)
			continue
		}

		controlFile := path.Join(g.SelfDir, agentDir, version, "control")
		if !f.IsExist(controlFile) {
			log.Printf("%s is nonexistent", controlFile)
			continue
		}

		cmd := exec.Command("./control", "status")
		cmd.Dir = path.Join(g.SelfDir, agentDir, version)
		bs, err := cmd.CombinedOutput()

		status := ""
		if err != nil {
			status = fmt.Sprintf("exec `./control status` fail: %s", err)
		} else {
			status = strings.TrimSpace(string(bs))
		}

		realAgent := &model.RealAgent{
			Name:      agentDir,
			Version:   version,
			Status:    status,
			Timestamp: now,
		}

		realAgents = append(realAgents, realAgent)
	}

	req.RealAgents = realAgents
	return req
}
Ejemplo n.º 15
0
func KernelAllocateFiles() (ret uint64, err error) {
	var content string
	file_nr := "/proc/sys/fs/file-nr"
	content, err = file.ToTrimString(file_nr)
	if err != nil {
		return
	}

	arr := strings.Fields(content)
	if len(arr) != 3 {
		err = fmt.Errorf("%s format error", file_nr)
		return
	}

	return strconv.ParseUint(arr[0], 10, 64)
}
Ejemplo n.º 16
0
func ParseConfig(cfg string) error {
	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		return err
	}

	var c GlobalConfig

	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		return err
	}

	Cfg = &c

	return nil
}
Ejemplo n.º 17
0
func ParseNodeConfig(cfg string) {
	if cfg == "" {
		log.Println("请配置nodepath")
		cfg = "node.file"
	}

	if !file.IsExist(cfg) {
		log.Println("node file:", cfg, "is not existent.`")
		return
	}

	NodeFilePath = cfg

	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		log.Println("read node file:", cfg, "fail:", err)
		return
	}

	c := Nodefile{}
	err = json.Unmarshal([]byte(configContent), &c)
	if err != nil {
		log.Println("parse node file:", cfg, "fail:", err)
		return
	}

	nm := make(map[string]string)
	for _, node := range c.Data {
		if len(node.Nodeout_ports) == 0 {
			continue
		}
		//fmt.Printf("%v\n", node)
		//fmt.Printf("%#v\n", len(node.Nodeout_ports))
		for _, port := range node.Nodeout_ports {
			nm[node.Manage_ip+port.Name] = node.Node.ID
			//fmt.Printf("%s%s:%s\n", node.Manage_ip, port.Name, node.Node.ID)
		}
	}
	lock.Lock()
	defer lock.Unlock()

	nodemap = &nm

	log.Println("read node file:", cfg, "successfully")
}
Ejemplo n.º 18
0
func InitFlows() {
	cfg := revel.BasePath + "/conf/flow.json"

	if !file.IsExist(cfg) {
		file, _ := os.Getwd()
		revel.ERROR.Fatalln("config file:", cfg, "is not existent. getcwd", file)
	}
	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		revel.ERROR.Fatalln("read config file:", cfg, "fail:", err)
	}

	err = json.Unmarshal([]byte(configContent), &Flows)
	if err != nil {
		revel.ERROR.Fatalln("parse config file:", cfg, "fail:", err)
	} else {
		revel.INFO.Println("InitFlows ok")
	}
}
Ejemplo n.º 19
0
/**
 * @function name:   func parseConfig()
 * @description:     This function parses config file cfg.json.
 * @related issues:  OWL-201, OWL-115, OWL-085
 * @param:           void
 * @return:          void
 * @author:          Don Hsieh
 * @since:           09/14/2015
 * @last modified:   12/10/2015
 * @called by:       func Register(r *macaron.Macaron)
 */
func parseConfig() {
	cfg := "cfg.json"
	if !file.IsExist(cfg) {
		log.Fatal(4, "config file:", cfg, "is not existent. maybe you need `mv cfg.example.json cfg.json`")
	}
	configContent, err := file.ToTrimString(cfg)
	if err != nil {
		log.Fatal(4, "read config file:", cfg, "fail:", err)
	}

	var configGlobal GlobalConfig
	err = json.Unmarshal([]byte(configContent), &configGlobal)
	if err != nil {
		log.Fatal(4, "parse config file:", cfg, "fail:", err)
		return
	}
	lock.Lock()
	defer lock.Unlock()
	ConfigOpenFalcon = &configGlobal
}
Ejemplo n.º 20
0
func ReadDesiredAgents() []*model.DesiredAgent {
	var desiredAgents []*model.DesiredAgent
	strJson, err := f.ToTrimString(path.Join(f.SelfDir(), "desired_agent.json"))
	if nil != err {
		logger.Errorln("read desired agent file error", err)
		return desiredAgents
	}
	err = json.Unmarshal([]byte(strJson), &desiredAgents)
	if nil != err {
		logger.Errorln("unmarshal json error", strJson, err)
	}

	for _, da := range desiredAgents {
		actualVersion := ReadVersion(da.RunUser, path.Join(da.WorkDir, da.Name))
		if "" != actualVersion {
			da.Version = actualVersion
		}
	}
	return desiredAgents
}
Ejemplo n.º 21
0
func LoadAvg() (*Loadavg, error) {

	loadAvg := Loadavg{}

	data, err := file.ToTrimString("/proc/loadavg")
	if err != nil {
		return nil, err
	}

	L := strings.Fields(data)
	if loadAvg.Avg1min, err = strconv.ParseFloat(L[0], 64); err != nil {
		return nil, err
	}
	if loadAvg.Avg5min, err = strconv.ParseFloat(L[1], 64); err != nil {
		return nil, err
	}
	if loadAvg.Avg15min, err = strconv.ParseFloat(L[2], 64); err != nil {
		return nil, err
	}

	return &loadAvg, nil
}