2014-04-18 21:28:00 +09:00
|
|
|
// +build windows
|
|
|
|
|
2014-12-30 22:09:05 +09:00
|
|
|
package host
|
2014-04-18 21:28:00 +09:00
|
|
|
|
|
|
|
import (
|
2015-08-28 07:39:20 +01:00
|
|
|
"fmt"
|
2015-10-13 13:02:02 -07:00
|
|
|
"os"
|
2015-08-25 23:03:25 -07:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2017-06-01 18:11:14 +02:00
|
|
|
"sync/atomic"
|
2015-10-13 13:02:02 -07:00
|
|
|
"time"
|
2017-02-02 16:05:11 -08:00
|
|
|
"unsafe"
|
2014-11-27 22:28:05 +09:00
|
|
|
|
2015-04-20 00:05:31 +09:00
|
|
|
"github.com/StackExchange/wmi"
|
2015-10-20 00:04:57 +09:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
|
|
|
process "github.com/shirou/gopsutil/process"
|
2017-06-02 13:51:00 -07:00
|
|
|
"golang.org/x/sys/windows"
|
2014-04-22 22:10:13 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-11-27 22:28:05 +09:00
|
|
|
procGetSystemTimeAsFileTime = common.Modkernel32.NewProc("GetSystemTimeAsFileTime")
|
2015-10-13 13:02:02 -07:00
|
|
|
osInfo *Win32_OperatingSystem
|
2014-04-18 21:28:00 +09:00
|
|
|
)
|
|
|
|
|
2015-04-20 00:05:31 +09:00
|
|
|
type Win32_OperatingSystem struct {
|
2015-10-13 13:02:02 -07:00
|
|
|
Version string
|
|
|
|
Caption string
|
|
|
|
ProductType uint32
|
|
|
|
BuildNumber string
|
2015-04-20 00:05:31 +09:00
|
|
|
LastBootUpTime time.Time
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:09:12 +09:00
|
|
|
func Info() (*InfoStat, error) {
|
|
|
|
ret := &InfoStat{
|
2015-10-13 13:02:02 -07:00
|
|
|
OS: runtime.GOOS,
|
2014-04-18 21:28:00 +09:00
|
|
|
}
|
|
|
|
|
2017-02-02 16:05:11 -08:00
|
|
|
{
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err == nil {
|
|
|
|
ret.Hostname = hostname
|
|
|
|
}
|
2015-08-25 23:27:25 -07:00
|
|
|
}
|
|
|
|
|
2017-02-02 16:05:11 -08:00
|
|
|
{
|
|
|
|
platform, family, version, err := PlatformInformation()
|
|
|
|
if err == nil {
|
|
|
|
ret.Platform = platform
|
|
|
|
ret.PlatformFamily = family
|
|
|
|
ret.PlatformVersion = version
|
|
|
|
} else {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
boot, err := BootTime()
|
|
|
|
if err == nil {
|
|
|
|
ret.BootTime = boot
|
|
|
|
ret.Uptime, _ = Uptime()
|
|
|
|
}
|
2015-08-25 23:03:25 -07:00
|
|
|
}
|
2015-10-13 13:02:02 -07:00
|
|
|
|
2017-02-02 16:05:11 -08:00
|
|
|
{
|
|
|
|
hostID, err := getMachineGuid()
|
|
|
|
if err == nil {
|
2017-02-06 16:10:46 -08:00
|
|
|
ret.HostID = strings.ToLower(hostID)
|
2017-02-02 16:05:11 -08:00
|
|
|
}
|
2015-08-25 23:03:25 -07:00
|
|
|
}
|
2015-10-13 13:02:02 -07:00
|
|
|
|
2017-02-02 16:05:11 -08:00
|
|
|
{
|
|
|
|
procs, err := process.Pids()
|
|
|
|
if err == nil {
|
|
|
|
ret.Procs = uint64(len(procs))
|
|
|
|
}
|
2014-04-19 01:05:35 +09:00
|
|
|
}
|
|
|
|
|
2014-04-18 21:28:00 +09:00
|
|
|
return ret, nil
|
|
|
|
}
|
2014-04-22 21:39:18 +09:00
|
|
|
|
2017-02-02 16:05:11 -08:00
|
|
|
func getMachineGuid() (string, error) {
|
2017-06-02 13:51:00 -07:00
|
|
|
var h windows.Handle
|
|
|
|
err := windows.RegOpenKeyEx(windows.HKEY_LOCAL_MACHINE, windows.StringToUTF16Ptr(`SOFTWARE\Microsoft\Cryptography`), 0, windows.KEY_READ|windows.KEY_WOW64_64KEY, &h)
|
2017-02-02 16:05:11 -08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-06-02 13:51:00 -07:00
|
|
|
defer windows.RegCloseKey(h)
|
2017-02-02 16:05:11 -08:00
|
|
|
|
|
|
|
const windowsRegBufLen = 74 // len(`{`) + len(`abcdefgh-1234-456789012-123345456671` * 2) + len(`}`) // 2 == bytes/UTF16
|
|
|
|
const uuidLen = 36
|
|
|
|
|
|
|
|
var regBuf [windowsRegBufLen]uint16
|
|
|
|
bufLen := uint32(windowsRegBufLen)
|
|
|
|
var valType uint32
|
2017-06-02 13:51:00 -07:00
|
|
|
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`MachineGuid`), nil, &valType, (*byte)(unsafe.Pointer(®Buf[0])), &bufLen)
|
2017-02-02 16:05:11 -08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-06-02 13:51:00 -07:00
|
|
|
hostID := windows.UTF16ToString(regBuf[:])
|
2017-02-02 16:05:11 -08:00
|
|
|
hostIDLen := len(hostID)
|
|
|
|
if hostIDLen != uuidLen {
|
|
|
|
return "", fmt.Errorf("HostID incorrect: %q\n", hostID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostID, nil
|
|
|
|
}
|
|
|
|
|
2015-08-25 23:03:25 -07:00
|
|
|
func GetOSInfo() (Win32_OperatingSystem, error) {
|
2015-04-20 00:05:31 +09:00
|
|
|
var dst []Win32_OperatingSystem
|
|
|
|
q := wmi.CreateQuery(&dst, "")
|
|
|
|
err := wmi.Query(q, &dst)
|
2015-02-15 22:26:18 +09:00
|
|
|
if err != nil {
|
2015-08-25 23:03:25 -07:00
|
|
|
return Win32_OperatingSystem{}, err
|
2015-02-15 22:26:18 +09:00
|
|
|
}
|
2015-10-13 13:02:02 -07:00
|
|
|
|
2015-08-25 23:03:25 -07:00
|
|
|
osInfo = &dst[0]
|
2015-10-13 13:02:02 -07:00
|
|
|
|
2015-08-25 23:03:25 -07:00
|
|
|
return dst[0], nil
|
|
|
|
}
|
|
|
|
|
2016-06-12 23:20:51 +09:00
|
|
|
func Uptime() (uint64, error) {
|
2015-08-25 23:03:25 -07:00
|
|
|
if osInfo == nil {
|
|
|
|
_, err := GetOSInfo()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
t := osInfo.LastBootUpTime.Local()
|
2015-02-15 22:26:18 +09:00
|
|
|
return uint64(now.Sub(t).Seconds()), nil
|
2014-04-22 21:39:18 +09:00
|
|
|
}
|
2015-02-15 22:26:18 +09:00
|
|
|
|
2016-06-12 23:20:51 +09:00
|
|
|
func bootTime(up uint64) uint64 {
|
|
|
|
return uint64(time.Now().Unix()) - up
|
2015-11-23 13:14:56 -07:00
|
|
|
}
|
|
|
|
|
2017-06-01 18:11:14 +02:00
|
|
|
// cachedBootTime must be accessed via atomic.Load/StoreUint64
|
|
|
|
var cachedBootTime uint64
|
|
|
|
|
2016-06-12 23:20:51 +09:00
|
|
|
func BootTime() (uint64, error) {
|
2017-06-01 18:11:14 +02:00
|
|
|
t := atomic.LoadUint64(&cachedBootTime)
|
|
|
|
if t != 0 {
|
|
|
|
return t, nil
|
2016-10-13 11:18:11 -04:00
|
|
|
}
|
2016-06-12 23:20:51 +09:00
|
|
|
up, err := Uptime()
|
2015-11-23 13:14:56 -07:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-06-01 18:11:14 +02:00
|
|
|
t = bootTime(up)
|
|
|
|
atomic.StoreUint64(&cachedBootTime, t)
|
|
|
|
return t, nil
|
2015-11-23 13:14:56 -07:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:09:12 +09:00
|
|
|
func PlatformInformation() (platform string, family string, version string, err error) {
|
2015-08-25 23:27:25 -07:00
|
|
|
if osInfo == nil {
|
|
|
|
_, err = GetOSInfo()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Platform
|
|
|
|
platform = strings.Trim(osInfo.Caption, " ")
|
2015-10-13 13:02:02 -07:00
|
|
|
|
2015-08-25 23:27:25 -07:00
|
|
|
// PlatformFamily
|
|
|
|
switch osInfo.ProductType {
|
|
|
|
case 1:
|
2015-08-28 07:39:20 +01:00
|
|
|
family = "Standalone Workstation"
|
2015-08-25 23:27:25 -07:00
|
|
|
case 2:
|
2015-08-28 07:39:20 +01:00
|
|
|
family = "Server (Domain Controller)"
|
2015-08-25 23:27:25 -07:00
|
|
|
case 3:
|
2015-08-28 07:39:20 +01:00
|
|
|
family = "Server"
|
2015-08-25 23:27:25 -07:00
|
|
|
}
|
2015-10-13 13:02:02 -07:00
|
|
|
|
2015-08-25 23:27:25 -07:00
|
|
|
// Platform Version
|
2015-08-28 07:39:20 +01:00
|
|
|
version = fmt.Sprintf("%s Build %s", osInfo.Version, osInfo.BuildNumber)
|
2015-08-25 23:27:25 -07:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-22 21:39:18 +09:00
|
|
|
func Users() ([]UserStat, error) {
|
2014-04-30 15:32:05 +09:00
|
|
|
var ret []UserStat
|
2014-04-22 21:39:18 +09:00
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2017-03-19 02:05:46 +01:00
|
|
|
|
|
|
|
func SensorsTemperatures() ([]TemperatureStat, error) {
|
2017-04-10 22:24:36 +09:00
|
|
|
return []TemperatureStat{}, common.ErrNotImplementedError
|
2017-03-19 02:05:46 +01:00
|
|
|
}
|