host: use wmic OS instead of calling GetTickCount on Windows.

pull/43/merge
WAKAYAMA Shirou 10 years ago
parent 4694ce0e4d
commit 4b0f5a03dc

@ -128,7 +128,7 @@ Current Status
================= ====== ======= ====== ======= ================= ====== ======= ====== =======
name Linux FreeBSD MacOSX Windows name Linux FreeBSD MacOSX Windows
cpu_times x x cpu_times x x x
cpu_count x x x x cpu_count x x x x
cpu_percent x x x cpu_percent x x x
cpu_times_percent x x x cpu_times_percent x x x
@ -138,7 +138,7 @@ disk_partitions x x x x
disk_io_counters x disk_io_counters x
disk_usage x x x x disk_usage x x x x
net_io_counters x x b x net_io_counters x x b x
boot_time x x x b boot_time x x x x
users x x x x users x x x x
pids x x x x pids x x x x
pid_exists x x x x pid_exists x x x x
@ -202,14 +202,14 @@ hostname x x x x
platformfamiliy x x x platformfamiliy x x x
virtualization x virtualization x
**CPU** **CPU**
VendorID x x x VendorID x x x x
Family x x x Family x x x x
Model x x x Model x x x x
Stepping x x x Stepping x x x x
PhysicalID x PhysicalID x
CoreID x CoreID x
Cores x Cores x x
ModelName x x x ModelName x x x x
**LoadAvg** **LoadAvg**
Load1 x x x Load1 x x x
Load5 x x x Load5 x x x

@ -3,9 +3,10 @@
package host package host
import ( import (
"fmt"
"os" "os"
"syscall" "strings"
"unsafe" "time"
common "github.com/shirou/gopsutil/common" common "github.com/shirou/gopsutil/common"
process "github.com/shirou/gopsutil/process" process "github.com/shirou/gopsutil/process"
@ -13,7 +14,6 @@ import (
var ( var (
procGetSystemTimeAsFileTime = common.Modkernel32.NewProc("GetSystemTimeAsFileTime") procGetSystemTimeAsFileTime = common.Modkernel32.NewProc("GetSystemTimeAsFileTime")
procGetTickCount = common.Modkernel32.NewProc("GetTickCount")
) )
func HostInfo() (*HostInfoStat, error) { func HostInfo() (*HostInfoStat, error) {
@ -24,13 +24,11 @@ func HostInfo() (*HostInfoStat, error) {
} }
ret.Hostname = hostname ret.Hostname = hostname
uptimemsec, _, err := procGetTickCount.Call() uptime, err := BootTime()
if uptimemsec == 0 { if err == nil {
return ret, syscall.GetLastError() ret.Uptime = uptime
} }
ret.Uptime = uint64(uptimemsec) / 1000
procs, err := process.Pids() procs, err := process.Pids()
if err != nil { if err != nil {
return ret, err return ret, err
@ -42,25 +40,26 @@ func HostInfo() (*HostInfoStat, error) {
} }
func BootTime() (uint64, error) { func BootTime() (uint64, error) {
var lpSystemTimeAsFileTime common.FILETIME lines, err := common.GetWmic("os", "LastBootUpTime")
if err != nil {
r, _, _ := procGetSystemTimeAsFileTime.Call(uintptr(unsafe.Pointer(&lpSystemTimeAsFileTime))) return 0, err
if r == 0 {
return 0, syscall.GetLastError()
} }
if len(lines) == 0 || lines[0] == "" {
// TODO: This calc is wrong. return 0, fmt.Errorf("could not get LastBootUpTime")
ll := (uint32(lpSystemTimeAsFileTime.DwHighDateTime))<<32 + lpSystemTimeAsFileTime.DwLowDateTime
pt := (uint64(ll) - 116444736000000000) / 10000000
u, _, _ := procGetTickCount.Call()
if u == 0 {
return 0, syscall.GetLastError()
} }
uptime := uint64(u) / 1000 l := strings.Split(lines[0], ",")
if len(l) != 2 {
return uint64(pt - uptime), nil return 0, fmt.Errorf("could not parse LastBootUpTime")
}
format := "20060102150405"
t, err := time.Parse(format, strings.Split(l[1], ".")[0])
if err != nil {
return 0, err
} }
now := time.Now()
return uint64(now.Sub(t).Seconds()), nil
}
func Users() ([]UserStat, error) { func Users() ([]UserStat, error) {
var ret []UserStat var ret []UserStat

Loading…
Cancel
Save