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

This commit is contained in:
WAKAYAMA Shirou 2015-02-15 22:26:18 +09:00
parent 4694ce0e4d
commit 4b0f5a03dc
2 changed files with 31 additions and 32 deletions

View File

@ -128,7 +128,7 @@ Current Status
================= ====== ======= ====== =======
name Linux FreeBSD MacOSX Windows
cpu_times x x
cpu_times x x x
cpu_count x x x x
cpu_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_usage x x x 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
pids x x x x
pid_exists x x x x
@ -202,14 +202,14 @@ hostname x x x x
platformfamiliy x x x
virtualization x
**CPU**
VendorID x x x
Family x x x
Model x x x
Stepping x x x
VendorID x x x x
Family x x x x
Model x x x x
Stepping x x x x
PhysicalID x
CoreID x
Cores x
ModelName x x x
Cores x x
ModelName x x x x
**LoadAvg**
Load1 x x x
Load5 x x x

View File

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