2017-10-05 15:36:05 +08:00
|
|
|
// +build freebsd openbsd
|
2014-04-18 15:34:47 +08:00
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package load
|
2014-04-18 15:34:47 +08:00
|
|
|
|
|
|
|
import (
|
2017-12-31 14:25:49 +08:00
|
|
|
"context"
|
2016-02-20 21:52:16 +08:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2017-10-04 21:34:47 +08:00
|
|
|
"unsafe"
|
2014-11-27 09:18:15 +08:00
|
|
|
|
2017-10-04 21:34:47 +08:00
|
|
|
"golang.org/x/sys/unix"
|
2014-04-18 15:34:47 +08:00
|
|
|
)
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func Avg() (*AvgStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return AvgWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func AvgWithContext(ctx context.Context) (*AvgStat, error) {
|
2017-10-04 21:34:47 +08:00
|
|
|
// 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
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
2017-10-04 21:34:47 +08:00
|
|
|
b, err := unix.SysctlRaw("vm.loadavg")
|
2014-04-18 15:34:47 +08:00
|
|
|
if err != nil {
|
2014-05-20 18:29:41 +08:00
|
|
|
return nil, err
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
2017-10-04 21:34:47 +08:00
|
|
|
load := *(*loadavg)(unsafe.Pointer((&b[0])))
|
|
|
|
scale := float64(load.scale)
|
2016-03-22 22:09:12 +08:00
|
|
|
ret := &AvgStat{
|
2017-10-04 21:34:47 +08:00
|
|
|
Load1: float64(load.load[0]) / scale,
|
|
|
|
Load5: float64(load.load[1]) / scale,
|
|
|
|
Load15: float64(load.load[2]) / scale,
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2016-02-20 21:52:16 +08:00
|
|
|
|
2017-10-05 15:36:05 +08:00
|
|
|
// Misc returns miscellaneous host-wide statistics.
|
2016-02-20 22:17:20 +08:00
|
|
|
// darwin use ps command to get process running/blocked count.
|
|
|
|
// Almost same as Darwin implementation, but state is different.
|
2016-02-20 21:52:16 +08:00
|
|
|
func Misc() (*MiscStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return MiscWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func MiscWithContext(ctx context.Context) (*MiscStat, error) {
|
2016-02-20 21:52:16 +08:00
|
|
|
bin, err := exec.LookPath("ps")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-31 20:35:53 +08:00
|
|
|
out, err := invoke.CommandWithContext(ctx, bin, "axo", "state")
|
2016-02-20 21:52:16 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lines := strings.Split(string(out), "\n")
|
|
|
|
|
|
|
|
ret := MiscStat{}
|
|
|
|
for _, l := range lines {
|
|
|
|
if strings.Contains(l, "R") {
|
2016-04-01 20:34:39 +08:00
|
|
|
ret.ProcsRunning++
|
2016-02-20 21:52:16 +08:00
|
|
|
} else if strings.Contains(l, "D") {
|
2016-04-01 20:34:39 +08:00
|
|
|
ret.ProcsBlocked++
|
2016-02-20 21:52:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ret, nil
|
|
|
|
}
|