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 (
|
|
|
|
"os"
|
2015-08-28 07:39:20 +01:00
|
|
|
"fmt"
|
2015-02-15 22:26:18 +09:00
|
|
|
"time"
|
2015-08-25 23:03:25 -07:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2014-11-27 22:28:05 +09:00
|
|
|
|
2015-04-20 00:05:31 +09:00
|
|
|
"github.com/StackExchange/wmi"
|
|
|
|
|
2014-11-27 22:28:05 +09:00
|
|
|
common "github.com/shirou/gopsutil/common"
|
|
|
|
process "github.com/shirou/gopsutil/process"
|
2014-04-22 22:10:13 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-11-27 22:28:05 +09:00
|
|
|
procGetSystemTimeAsFileTime = common.Modkernel32.NewProc("GetSystemTimeAsFileTime")
|
2015-08-25 23:03:25 -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-08-25 23:03:25 -07:00
|
|
|
Version string
|
|
|
|
Caption string
|
|
|
|
ProductType uint32
|
2015-08-28 07:39:20 +01:00
|
|
|
BuildNumber string
|
2015-04-20 00:05:31 +09:00
|
|
|
LastBootUpTime time.Time
|
|
|
|
}
|
|
|
|
|
2014-05-12 12:10:28 +09:00
|
|
|
func HostInfo() (*HostInfoStat, error) {
|
2014-04-18 21:28:00 +09:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
2015-08-25 23:27:25 -07:00
|
|
|
return nil, err
|
2014-04-18 21:28:00 +09:00
|
|
|
}
|
|
|
|
|
2015-08-25 23:27:25 -07:00
|
|
|
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
|
2015-08-25 23:03:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ret.Uptime, err = BootTime()
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2014-11-27 22:28:05 +09:00
|
|
|
procs, err := process.Pids()
|
2014-04-19 01:05:35 +09:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.Procs = uint64(len(procs))
|
|
|
|
|
2014-04-18 21:28:00 +09:00
|
|
|
return ret, nil
|
|
|
|
}
|
2014-04-22 21:39:18 +09:00
|
|
|
|
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-08-25 23:03:25 -07:00
|
|
|
|
|
|
|
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()
|
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
|
|
|
|
2015-08-25 23:27:25 -07:00
|
|
|
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:
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|