2015-09-16 15:19:33 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package common
|
2016-07-12 02:05:30 +08:00
|
|
|
|
2016-08-11 15:48:24 +08:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func DoSysctrl(mib string) ([]string, error) {
|
|
|
|
sysctl, err := exec.LookPath("/sbin/sysctl")
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
2017-08-17 05:54:50 +08:00
|
|
|
cmd := exec.Command(sysctl, "-n", mib)
|
2017-08-17 06:32:21 +08:00
|
|
|
cmd.Env = getSysctrlEnv(os.Environ())
|
2017-08-17 05:54:50 +08:00
|
|
|
out, err := cmd.Output()
|
2016-08-11 15:48:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
v := strings.Replace(string(out), "{ ", "", 1)
|
|
|
|
v = strings.Replace(string(v), " }", "", 1)
|
|
|
|
values := strings.Fields(string(v))
|
|
|
|
|
|
|
|
return values, nil
|
|
|
|
}
|
2016-07-12 02:05:30 +08:00
|
|
|
|
|
|
|
func NumProcs() (uint64, error) {
|
|
|
|
f, err := os.Open(HostProc())
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-02-22 21:46:23 +08:00
|
|
|
defer f.Close()
|
2016-07-12 02:05:30 +08:00
|
|
|
|
2017-05-24 23:04:41 +08:00
|
|
|
list, err := f.Readdirnames(-1)
|
2016-07-12 02:05:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2016-07-12 02:07:55 +08:00
|
|
|
return uint64(len(list)), err
|
2016-07-12 02:05:30 +08:00
|
|
|
}
|