Beispiel #1
0
func LoadAvg() (*LoadAvgStat, error) {
	name := C.CString("vm.loadavg")
	defer C.free(unsafe.Pointer(name))

	var size C.size_t
	var res C.int = C.sysctlbyname(name, nil, &size, nil, 0)

	if res != 0 {
		return nil, fmt.Errorf("errno %d", res)
	}

	buf := make([]byte, int(size))

	res = C.sysctlbyname(name, (unsafe.Pointer(&buf[0])), &size, nil, 0)
	if res != 0 {
		return nil, fmt.Errorf("errno %d", res)
	}

	var out C.struct_loadavg = *(*C.struct_loadavg)(unsafe.Pointer(&buf[0]))

	scale := float64(out.fscale)

	ret := &LoadAvgStat{
		Load1:  float64(out.ldavg[0]) / scale,
		Load5:  float64(out.ldavg[1]) / scale,
		Load15: float64(out.ldavg[2]) / scale,
	}

	return ret, nil
}
Beispiel #2
0
// GetInt64 gets a numeric value from sysctl(3) by name
func GetInt64(name string) (value int64, err error) {
	oldlen := C.size_t(8)
	_, err = C.sysctlbyname(C.CString(name), unsafe.Pointer(&value), &oldlen, nil, 0)
	if err != nil {
		return
	}
	return
}
Beispiel #3
0
// GetString gets a string value from sysctl(3) by name
func GetString(name string) (value string, err error) {
	oldlen := C.size_t(0)

	// Call C.sysctlbyname once to get the required size of the buffer.
	_, err = C.sysctlbyname(C.CString(name), nil, &oldlen, nil, 0)
	if err != nil {
		return
	}

	oldp := C.CString(string(make([]byte, uint32(oldlen))))
	_, err = C.sysctlbyname(C.CString(name), unsafe.Pointer(oldp), &oldlen, nil, 0)
	if err != nil {
		return
	}
	value = C.GoString(oldp)

	return
}
Beispiel #4
0
func (self *FDUsage) Get() error {
	val := C.uint32_t(0)
	sc := C.size_t(4)

	name := C.CString("kern.openfiles")
	_, err := C.sysctlbyname(name, unsafe.Pointer(&val), &sc, nil, 0)
	C.free(unsafe.Pointer(name))
	if err != nil {
		return err
	}
	self.Open = uint64(val)

	name = C.CString("kern.maxfiles")
	_, err = C.sysctlbyname(name, unsafe.Pointer(&val), &sc, nil, 0)
	C.free(unsafe.Pointer(name))
	if err != nil {
		return err
	}
	self.Max = uint64(val)

	self.Unused = self.Max - self.Open

	return nil
}