1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-05-08 19:29:25 +08:00

Merge pull request #60 from mayowa/master

Bug fix and HostStatInfo.Platform* windows implementation
This commit is contained in:
shirou 2015-08-28 16:26:02 +09:00
commit 3c958a8ee6
3 changed files with 84 additions and 18 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*~ *~
#* #*
_obj _obj
*.tmp

View File

@ -15,12 +15,11 @@ import (
type Win32_Processor struct { type Win32_Processor struct {
LoadPercentage uint16 LoadPercentage uint16
L2CacheSize uint32
Family uint16 Family uint16
Manufacturer string Manufacturer string
Name string Name string
NumberOfLogicalProcessors uint32 NumberOfLogicalProcessors uint32
ProcessorId string ProcessorId *string
Stepping *string Stepping *string
MaxClockSpeed uint32 MaxClockSpeed uint32
} }
@ -63,15 +62,22 @@ func CPUInfo() ([]CPUInfoStat, error) {
if err != nil { if err != nil {
return ret, err return ret, err
} }
var procID string
for i, l := range dst { for i, l := range dst {
procID = ""
if l.ProcessorId != nil {
procID = *l.ProcessorId
}
cpu := CPUInfoStat{ cpu := CPUInfoStat{
CPU: int32(i), CPU: int32(i),
Family: fmt.Sprintf("%d", l.Family), Family: fmt.Sprintf("%d", l.Family),
CacheSize: int32(l.L2CacheSize),
VendorID: l.Manufacturer, VendorID: l.Manufacturer,
ModelName: l.Name, ModelName: l.Name,
Cores: int32(l.NumberOfLogicalProcessors), Cores: int32(l.NumberOfLogicalProcessors),
PhysicalID: l.ProcessorId, PhysicalID: procID,
Mhz: float64(l.MaxClockSpeed), Mhz: float64(l.MaxClockSpeed),
Flags: []string{}, Flags: []string{},
} }

View File

@ -4,7 +4,10 @@ package host
import ( import (
"os" "os"
"fmt"
"time" "time"
"runtime"
"strings"
"github.com/StackExchange/wmi" "github.com/StackExchange/wmi"
@ -14,23 +17,40 @@ import (
var ( var (
procGetSystemTimeAsFileTime = common.Modkernel32.NewProc("GetSystemTimeAsFileTime") procGetSystemTimeAsFileTime = common.Modkernel32.NewProc("GetSystemTimeAsFileTime")
osInfo *Win32_OperatingSystem
) )
type Win32_OperatingSystem struct { type Win32_OperatingSystem struct {
Version string
Caption string
ProductType uint32
BuildNumber string
LastBootUpTime time.Time LastBootUpTime time.Time
} }
func HostInfo() (*HostInfoStat, error) { func HostInfo() (*HostInfoStat, error) {
ret := &HostInfoStat{}
hostname, err := os.Hostname() hostname, err := os.Hostname()
if err != nil { if err != nil {
return nil, err
}
ret := &HostInfoStat{
Hostname: hostname,
OS: runtime.GOOS,
}
platform, family, version, err := GetPlatformInformation()
if err == nil {
ret.Platform = platform
ret.PlatformFamily = family
ret.PlatformVersion = version
} else {
return ret, err return ret, err
} }
ret.Hostname = hostname ret.Uptime, err = BootTime()
uptime, err := BootTime() if err != nil {
if err == nil { return ret, err
ret.Uptime = uptime
} }
procs, err := process.Pids() procs, err := process.Pids()
@ -43,19 +63,58 @@ func HostInfo() (*HostInfoStat, error) {
return ret, nil return ret, nil
} }
func BootTime() (uint64, error) { func GetOSInfo() (Win32_OperatingSystem, error) {
now := time.Now()
var dst []Win32_OperatingSystem var dst []Win32_OperatingSystem
q := wmi.CreateQuery(&dst, "") q := wmi.CreateQuery(&dst, "")
err := wmi.Query(q, &dst) err := wmi.Query(q, &dst)
if err != nil {
return Win32_OperatingSystem{}, err
}
osInfo = &dst[0]
return dst[0], nil
}
func BootTime() (uint64, error) {
if osInfo == nil {
_, err := GetOSInfo()
if err != nil { if err != nil {
return 0, err return 0, err
} }
t := dst[0].LastBootUpTime.Local() }
now := time.Now()
t := osInfo.LastBootUpTime.Local()
return uint64(now.Sub(t).Seconds()), nil return uint64(now.Sub(t).Seconds()), nil
} }
func GetPlatformInformation() (platform string, family string, version string, err error) {
if osInfo == nil {
_, err = GetOSInfo()
if err != nil {
return
}
}
// Platform
platform = strings.Trim(osInfo.Caption, " ")
// PlatformFamily
switch osInfo.ProductType {
case 1:
family = "Standalone Workstation"
case 2:
family = "Server (Domain Controller)"
case 3:
family = "Server"
}
// Platform Version
version = fmt.Sprintf("%s Build %s", osInfo.Version, osInfo.BuildNumber)
return
}
func Users() ([]UserStat, error) { func Users() ([]UserStat, error) {
var ret []UserStat var ret []UserStat