示例#1
0
func InitEnv(configPath string) {

	if !filetool.IsExist(configPath) {
		log.Warn("configuration file[%s] is nonexistent", configPath)
		UseSystemConfig = true
	}

	if !UseSystemConfig {
		var err error
		Config, err = config.NewConfig("ini", configPath)
		if err != nil {
			log.Fetal("configuration file[%s] cannot parse. ", configPath)
			os.Exit(1)
		}
	}

	if !UseSystemConfig {
		log.SetLevelWithDefault(Config.String("log::level"), "info")
	} else {
		log.SetLevel("info")
	}

	initCfg()

	defaultPidPath := "/var/run/falcon_eye/falcon_eye.pid"
	if !UseSystemConfig {
		if iniPidPath := Config.String("common::pid"); iniPidPath != "" {
			defaultPidPath = iniPidPath
		}
	}
	systool.WritePidFile(defaultPidPath)

	runtime.GOMAXPROCS(runtime.NumCPU())
}
示例#2
0
func AllProcs() (ps []*Proc, err error) {
	var dirs []string
	dirs, err = filetool.DirsUnder("/proc")
	if err != nil {
		return
	}

	// id dir is a number, it should be a pid. but don't trust it
	dirs_len := len(dirs)
	if dirs_len == 0 {
		return
	}

	var pid int
	var name_state [2]string
	var cmdline string
	for i := 0; i < dirs_len; i++ {
		if pid, err = strconv.Atoi(dirs[i]); err != nil {
			err = nil
			continue
		} else {
			status_file := fmt.Sprintf("/proc/%d/status", pid)
			cmdline_file := fmt.Sprintf("/proc/%d/cmdline", pid)
			if !filetool.IsExist(status_file) || !filetool.IsExist(cmdline_file) {
				continue
			}

			name_state, err = ReadProcStatus(status_file)
			if err != nil {
				log.Error("read %s fail: %s", status_file, err)
				continue
			}

			cmdline, err = filetool.ReadFileToStringNoLn(cmdline_file)
			if err != nil {
				log.Error("read %s fail: %s", cmdline_file, err)
				continue
			}

			p := Proc{Pid: pid, Name: name_state[0], State: name_state[1], Cmdline: cmdline}
			ps = append(ps, &p)
		}
	}

	return
}
示例#3
0
文件: iostat.go 项目: healthly/falcon
func ListDiskStats() ([]*DiskStats, error) {
	proc_diskstats := "/proc/diskstats"
	if !filetool.IsExist(proc_diskstats) {
		return nil, fmt.Errorf("%s not exists", proc_diskstats)
	}

	contents, err := ioutil.ReadFile(proc_diskstats)
	if err != nil {
		return nil, err
	}

	ret := make([]*DiskStats, 0)

	reader := bufio.NewReader(bytes.NewBuffer(contents))
	for {
		line, _, err := reader.ReadLine()
		if err == io.EOF {
			break
		}

		fields := strings.Fields(string(line))
		// shortcut the deduper and just skip disks that
		// haven't done a single read.  This elimiates a bunch
		// of loopback, ramdisk, and cdrom devices but still
		// lets us report on the rare case that we actually use
		// a ramdisk.
		if fields[3] == "0" {
			continue
		}

		size := len(fields)
		// kernel version too low
		if size != 14 {
			continue
		}

		item := &DiskStats{}
		for i := 0; i < size; i++ {
			if item.Major, err = strconv.Atoi(fields[0]); err != nil {
				return nil, err
			}

			if item.Minor, err = strconv.Atoi(fields[1]); err != nil {
				return nil, err
			}

			item.Device = fields[2]

			if item.ReadRequests, err = strconv.ParseUint(fields[3], 10, 64); err != nil {
				return nil, err
			}

			if item.ReadMerged, err = strconv.ParseUint(fields[4], 10, 64); err != nil {
				return nil, err
			}

			if item.ReadSectors, err = strconv.ParseUint(fields[5], 10, 64); err != nil {
				return nil, err
			}

			if item.MsecRead, err = strconv.ParseUint(fields[6], 10, 64); err != nil {
				return nil, err
			}

			if item.WriteRequests, err = strconv.ParseUint(fields[7], 10, 64); err != nil {
				return nil, err
			}

			if item.WriteMerged, err = strconv.ParseUint(fields[8], 10, 64); err != nil {
				return nil, err
			}

			if item.WriteSectors, err = strconv.ParseUint(fields[9], 10, 64); err != nil {
				return nil, err
			}

			if item.MsecWrite, err = strconv.ParseUint(fields[10], 10, 64); err != nil {
				return nil, err
			}

			if item.IosInProgress, err = strconv.ParseUint(fields[11], 10, 64); err != nil {
				return nil, err
			}

			if item.MsecTotal, err = strconv.ParseUint(fields[12], 10, 64); err != nil {
				return nil, err
			}

			if item.MsecWeightedTotal, err = strconv.ParseUint(fields[13], 10, 64); err != nil {
				return nil, err
			}

		}
		ret = append(ret, item)
	}
	return ret, nil
}