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
*.tmp

View File

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

View File

@ -4,7 +4,10 @@ package host
import (
"os"
"fmt"
"time"
"runtime"
"strings"
"github.com/StackExchange/wmi"
@ -14,25 +17,42 @@ import (
var (
procGetSystemTimeAsFileTime = common.Modkernel32.NewProc("GetSystemTimeAsFileTime")
osInfo *Win32_OperatingSystem
)
type Win32_OperatingSystem struct {
Version string
Caption string
ProductType uint32
BuildNumber string
LastBootUpTime time.Time
}
func HostInfo() (*HostInfoStat, error) {
ret := &HostInfoStat{}
hostname, err := os.Hostname()
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
}
ret.Uptime, err = BootTime()
if err != nil {
return ret, err
}
ret.Hostname = hostname
uptime, err := BootTime()
if err == nil {
ret.Uptime = uptime
}
procs, err := process.Pids()
if err != nil {
return ret, err
@ -43,19 +63,58 @@ func HostInfo() (*HostInfoStat, error) {
return ret, nil
}
func BootTime() (uint64, error) {
now := time.Now()
func GetOSInfo() (Win32_OperatingSystem, error) {
var dst []Win32_OperatingSystem
q := wmi.CreateQuery(&dst, "")
err := wmi.Query(q, &dst)
if err != nil {
return 0, err
return Win32_OperatingSystem{}, err
}
t := dst[0].LastBootUpTime.Local()
osInfo = &dst[0]
return dst[0], nil
}
func BootTime() (uint64, error) {
if osInfo == nil {
_, err := GetOSInfo()
if err != nil {
return 0, err
}
}
now := time.Now()
t := osInfo.LastBootUpTime.Local()
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) {
var ret []UserStat