Skip to content

Commit

Permalink
Merge pull request #60 from mayowa/master
Browse files Browse the repository at this point in the history
Bug fix and HostStatInfo.Platform* windows implementation
  • Loading branch information
shirou committed Aug 28, 2015
2 parents 3a625ab + bf16d0a commit 3c958a8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*~
#*
_obj
*.tmp
16 changes: 11 additions & 5 deletions cpu/cpu_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{},
}
Expand Down
81 changes: 70 additions & 11 deletions host/host_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ package host

import (
"os"
"fmt"
"time"
"runtime"
"strings"

"github.com/StackExchange/wmi"

Expand All @@ -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 ret, err
return nil, err
}

ret.Hostname = hostname
uptime, err := BootTime()
if err == nil {
ret.Uptime = uptime
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
}

procs, err := process.Pids()
if err != nil {
return ret, err
Expand All @@ -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
Expand Down

0 comments on commit 3c958a8

Please sign in to comment.