1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-29 13:49:21 +08:00

Update host.Info() to return the number of processes on all platforms.

Fixes: #227
This commit is contained in:
Sean Chittenden 2016-07-10 23:47:29 -05:00
parent d2ca7e8d2c
commit 69f7f8eaeb
No known key found for this signature in database
GPG Key ID: 4EBC9DC16C2E5E16
5 changed files with 27 additions and 4 deletions

View File

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

View File

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

View File

@ -16,6 +16,7 @@ import (
"time" "time"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/process"
) )
type LSB struct { type LSB struct {
@ -44,17 +45,24 @@ func Info() (*InfoStat, error) {
ret.PlatformFamily = family ret.PlatformFamily = family
ret.PlatformVersion = version ret.PlatformVersion = version
} }
system, role, err := Virtualization() system, role, err := Virtualization()
if err == nil { if err == nil {
ret.VirtualizationSystem = system ret.VirtualizationSystem = system
ret.VirtualizationRole = role ret.VirtualizationRole = role
} }
boot, err := BootTime() boot, err := BootTime()
if err == nil { if err == nil {
ret.BootTime = boot ret.BootTime = boot
ret.Uptime = uptime(boot) ret.Uptime = uptime(boot)
} }
procs, err := process.Pids()
if err == nil {
ret.Procs = uint64(len(procs))
}
return ret, nil return ret, nil
} }

View File

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

View File

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