예제 #1
0
func (getSystemRunTimeInfoLinux *GetSystemRunTimeInfoLinux) GetInfo() (*RunTimeInfo, error) {
	osErr := getLinuxOSError()
	if nil != osErr {
		return nil, osErr
	}
	var content string
	content, err := utils.ReadFileToStringNoLn("/proc/uptime")
	if nil != err {
		return nil, err
	}

	fields := strings.Fields(content)
	if len(fields) < 2 {
		return nil, errors.New("/proc/uptime parse error")
	}

	secStr := fields[0]
	var secF float64
	secF, err = strconv.ParseFloat(secStr, 64)
	if nil != err {
		return nil, err
	}
	runTimeInfo := &RunTimeInfo{}
	minTotal := secF / 60.0
	hourTotal := minTotal / 60.0

	runTimeInfo.Day = int64(hourTotal / 24.0)
	runTimeInfo.Hour = int64(hourTotal) - runTimeInfo.Day*24
	runTimeInfo.Min = int64(minTotal) - (runTimeInfo.Day * 60 * 24) - (runTimeInfo.Hour * 60)

	return runTimeInfo, nil
}
예제 #2
0
func (getAllProcessInfoLinux *GetAllProcessInfoLinux) getCmdLine(processInfo *model.ProcessInfo) (err error) {
	cmdline_file := fmt.Sprintf("/proc/%d/cmdline", processInfo.Id)
	if !utils.IsFileExist(cmdline_file) {
		err = errors.New("文件不存在")
		return
	}
	processInfo.Cmdline, err = utils.ReadFileToStringNoLn(cmdline_file)
	return
}
예제 #3
0
func (getLoaderAvgInfoLinux *GetLoaderAvgInfoLinux) GetInfo() (*LoaderAvgInfo, error) {
	loaderAvgInfo := LoaderAvgInfo{}

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

	slice := strings.Split(string(data), " ")
	if loaderAvgInfo.Avg1min, err = strconv.ParseFloat(slice[0], 64); nil != err {
		return nil, err
	}
	if loaderAvgInfo.Avg5min, err = strconv.ParseFloat(slice[1], 64); nil != err {
		return nil, err
	}
	if loaderAvgInfo.Avg15min, err = strconv.ParseFloat(slice[2], 64); nil != err {
		return nil, err
	}

	return &loaderAvgInfo, nil
}