You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gopsutil/load/load_bsd.go

60 lines
1.2 KiB
Go

// +build freebsd openbsd
11 years ago
package load
11 years ago
import (
"os/exec"
"strings"
"unsafe"
"golang.org/x/sys/unix"
11 years ago
)
func Avg() (*AvgStat, error) {
// This SysctlRaw method borrowed from
// https://github.com/prometheus/node_exporter/blob/master/collector/loadavg_freebsd.go
type loadavg struct {
load [3]uint32
scale int
11 years ago
}
b, err := unix.SysctlRaw("vm.loadavg")
11 years ago
if err != nil {
11 years ago
return nil, err
11 years ago
}
load := *(*loadavg)(unsafe.Pointer((&b[0])))
scale := float64(load.scale)
ret := &AvgStat{
Load1: float64(load.load[0]) / scale,
Load5: float64(load.load[1]) / scale,
Load15: float64(load.load[2]) / scale,
11 years ago
}
return ret, nil
}
// Misc returns miscellaneous host-wide statistics.
// darwin use ps command to get process running/blocked count.
// Almost same as Darwin implementation, but state is different.
func Misc() (*MiscStat, error) {
bin, err := exec.LookPath("ps")
if err != nil {
return nil, err
}
out, err := invoke.Command(bin, "axo", "state")
if err != nil {
return nil, err
}
lines := strings.Split(string(out), "\n")
ret := MiscStat{}
for _, l := range lines {
if strings.Contains(l, "R") {
9 years ago
ret.ProcsRunning++
} else if strings.Contains(l, "D") {
9 years ago
ret.ProcsBlocked++
}
}
return &ret, nil
}