Exemple #1
0
func SystemUptime() (arr [3]int64, err error) {
	var content string
	content, err = filetool.ReadFileToStringNoLn("/proc/uptime")
	if err != nil {
		return
	}

	fields := strings.Fields(content)
	if len(fields) < 2 {
		err = errors.New("/proc/uptime parse error")
		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 [3]int64{days, hours, mins}, nil
}
Exemple #2
0
func initVersion() {
	var err error
	Version, err = filetool.ReadFileToStringNoLn("VERSION")
	if err != nil {
		log.Fetal("read VERSION file fail")
		os.Exit(1)
	}
}
Exemple #3
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
}
Exemple #4
0
func LoadAvg() (*Loadavg, error) {

	loadAvg := Loadavg{}

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

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

	return &loadAvg, nil
}