示例#1
0
func GetIdleCpu() float64 {
	command := ""
	if global.IsLinux() {
		command = `uptime | awk '{print $10}'`
	} else if global.IsMac() {
		command = `uptime | awk '{print $8}'`
	} else {
		return 1
	}

	cmd := exec.Command("/bin/sh", "-c", command)
	var out bytes.Buffer
	cmd.Stdout = &out
	err := cmd.Run()
	if err != nil {
		return 1
	}

	line, err := out.ReadString('\n')
	line = strings.Replace(line, "\n", "", -1)
	if err != nil || line == "" {
		return 1
	}

	tokens := strings.Split(line, " ")

	use, err := strconv.ParseFloat(tokens[0], 64)
	if err != nil {
		return 1
	}

	if global.IsLinux() {
		return (1 - use) / 1
	} else if global.IsMac() {
		return (100 - use) / 100
	} else {
		return 1
	}
}
示例#2
0
func GetIdleMems() float64 {
	//此命令仅在linux下使用
	if global.IsLinux() == false {
		return 1
	}

	cmd := exec.Command("/bin/sh", "-c", `free -m | grep 'Mem' | awk '{print $2" "$3" "$4" "$5" "$6" "$7}'`)
	var out bytes.Buffer
	cmd.Stdout = &out
	err := cmd.Run()
	if err != nil {
		return 1
	}

	line, err := out.ReadString('\n')
	line = strings.Replace(line, "\n", "", -1)
	if err != nil || line == "" {
		return 1
	}

	tokens := strings.Split(line, " ")

	total, err := strconv.ParseFloat(tokens[0], 64)
	if err != nil {
		return 1
	}
	idle1, err := strconv.ParseFloat(tokens[2], 64)
	if err != nil {
		return 1
	}
	idle2, err := strconv.ParseFloat(tokens[4], 64)
	if err != nil {
		return 1
	}
	idle3, err := strconv.ParseFloat(tokens[5], 64)
	if err != nil {
		return 1
	}

	return (idle1 + idle2 + idle3) / total
}