Exemplo n.º 1
0
func CpuTimes() ([]CpuStat, error) {
	var cpuStats []CpuStat
	ncpu := runtime.NumCPU()
	times := make([]int64, ncpu*CPUSTATES)

	ret, err := sysctl.Raw("kern.cp_times")
	if err != nil {
		return cpuStats, err
	}

	br := bytes.NewReader(ret)
	binary.Read(br, binary.LittleEndian, &times)

	for i := 0; i < ncpu; i++ {
		offset := i * CPUSTATES

		cpuStat := CpuStat{
			Name: fmt.Sprintf("CPU %d", i),
			User: times[CP_USER+offset],
			Nice: times[CP_NICE+offset],
			Sys:  times[CP_SYS+offset],
			Intr: times[CP_INTR+offset],
			Idle: times[CP_IDLE+offset],
		}
		cpuStats = append(cpuStats, cpuStat)
	}
	return cpuStats, nil
}
Exemplo n.º 2
0
func boottime() Timeval {
	// boottime
	var boottv Timeval

	tv, err := sysctl.Raw("kern.boottime")
	if err != nil {
		log.Fatal(err)
	}

	br := bytes.NewReader(tv)
	binary.Read(br, binary.LittleEndian, &boottv)
	return boottv
}
Exemplo n.º 3
0
func getVersion(envVar, sysctlName string) string {
	if yes, envVal := isEnvSet(envVar); yes {
		return envVal
	} else {
		version, err := sysctl.Raw(sysctlName)
		if err != nil {
			log.Fatalf("%s - %v\n", sysctlName, err)
		}

		for i, r := range version {
			if r == '\n' || r == '\t' {
				version[i] = ' '
			}
		}

		return string(version)
	}
}
Exemplo n.º 4
0
func LoadAvarage() (LoadAvg, error) {
	var loadAvg LoadAvg

	la := struct {
		LdAvg  [4]uint32 // normally fixed point number fixpt_t[3]
		FScale int64
	}{}

	loadInfo, err := sysctl.Raw("vm.loadavg")
	if err != nil {
		return loadAvg, fmt.Errorf("cannot get sysctl vm.loadavg: %v", err)
	}

	br := bytes.NewReader(loadInfo)
	binary.Read(br, binary.LittleEndian, &la)

	fs := float32(la.FScale)
	loadAvg.OneM = float32(la.LdAvg[0]) / fs
	loadAvg.FiveM = float32(la.LdAvg[1]) / fs
	loadAvg.FifteenM = float32(la.LdAvg[2]) / fs

	return loadAvg, nil
}