Merge pull request #228 from sean-/master

Correctly detect host Procs on all platforms.
This commit is contained in:
shirou 2016-07-12 08:19:29 +09:00 committed by GitHub
commit ca63fa830d
6 changed files with 42 additions and 5 deletions

View File

@ -15,6 +15,7 @@ import (
"unsafe"
"github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/process"
)
// from utmpx.h
@ -37,6 +38,7 @@ func Info() (*InfoStat, error) {
ret.PlatformFamily = family
ret.PlatformVersion = version
}
system, role, err := Virtualization()
if err == nil {
ret.VirtualizationSystem = system
@ -49,6 +51,11 @@ func Info() (*InfoStat, error) {
ret.Uptime = uptime(boot)
}
procs, err := process.Pids()
if err == nil {
ret.Procs = uint64(len(procs))
}
return ret, nil
}

View File

@ -15,6 +15,7 @@ import (
"unsafe"
"github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/process"
)
const (
@ -40,6 +41,7 @@ func Info() (*InfoStat, error) {
ret.PlatformFamily = family
ret.PlatformVersion = version
}
system, role, err := Virtualization()
if err == nil {
ret.VirtualizationSystem = system
@ -52,6 +54,11 @@ func Info() (*InfoStat, error) {
ret.Uptime = uptime(boot)
}
procs, err := process.Pids()
if err == nil {
ret.Procs = uint64(len(procs))
}
return ret, nil
}

View File

@ -15,7 +15,7 @@ import (
"strings"
"time"
common "github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/internal/common"
)
type LSB struct {
@ -44,17 +44,23 @@ func Info() (*InfoStat, error) {
ret.PlatformFamily = family
ret.PlatformVersion = version
}
system, role, err := Virtualization()
if err == nil {
ret.VirtualizationSystem = system
ret.VirtualizationRole = role
}
boot, err := BootTime()
if err == nil {
ret.BootTime = boot
ret.Uptime = uptime(boot)
}
if numProcs, err := common.NumProcs(); err == nil {
ret.Procs = numProcs
}
return ret, nil
}

View File

@ -14,6 +14,9 @@ func TestHostInfo(t *testing.T) {
if v == empty {
t.Errorf("Could not get hostinfo %v", v)
}
if v.Procs == 0 {
t.Errorf("Could not determine the number of host processes")
}
}
func TestUptime(t *testing.T) {

View File

@ -54,12 +54,10 @@ func Info() (*InfoStat, error) {
}
procs, err := process.Pids()
if err != nil {
return ret, err
if err == nil {
ret.Procs = uint64(len(procs))
}
ret.Procs = uint64(len(procs))
return ret, nil
}

View File

@ -1,3 +1,19 @@
// +build linux
package common
import "os"
func NumProcs() (uint64, error) {
f, err := os.Open(HostProc())
if err != nil {
return 0, err
}
list, err := f.Readdir(-1)
defer f.Close()
if err != nil {
return 0, err
}
return uint64(len(list)), err
}