Add durable HostID generation for Windows.
https://github.com/shirou/gopsutil/pull/312
This commit is contained in:
parent
137f03814a
commit
6a787ec6f7
|
@ -7,7 +7,9 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"github.com/StackExchange/wmi"
|
"github.com/StackExchange/wmi"
|
||||||
|
|
||||||
|
@ -33,34 +35,77 @@ func Info() (*InfoStat, error) {
|
||||||
OS: runtime.GOOS,
|
OS: runtime.GOOS,
|
||||||
}
|
}
|
||||||
|
|
||||||
hostname, err := os.Hostname()
|
{
|
||||||
if err == nil {
|
hostname, err := os.Hostname()
|
||||||
ret.Hostname = hostname
|
if err == nil {
|
||||||
|
ret.Hostname = hostname
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
platform, family, version, err := PlatformInformation()
|
{
|
||||||
if err == nil {
|
platform, family, version, err := PlatformInformation()
|
||||||
ret.Platform = platform
|
if err == nil {
|
||||||
ret.PlatformFamily = family
|
ret.Platform = platform
|
||||||
ret.PlatformVersion = version
|
ret.PlatformFamily = family
|
||||||
} else {
|
ret.PlatformVersion = version
|
||||||
return ret, err
|
} else {
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boot, err := BootTime()
|
{
|
||||||
if err == nil {
|
boot, err := BootTime()
|
||||||
ret.BootTime = boot
|
if err == nil {
|
||||||
ret.Uptime, _ = Uptime()
|
ret.BootTime = boot
|
||||||
|
ret.Uptime, _ = Uptime()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
procs, err := process.Pids()
|
{
|
||||||
if err == nil {
|
hostID, err := getMachineGuid()
|
||||||
ret.Procs = uint64(len(procs))
|
if err == nil {
|
||||||
|
ret.HostID = hostID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
procs, err := process.Pids()
|
||||||
|
if err == nil {
|
||||||
|
ret.Procs = uint64(len(procs))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getMachineGuid() (string, error) {
|
||||||
|
var h syscall.Handle
|
||||||
|
err := syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE, syscall.StringToUTF16Ptr(`SOFTWARE\Microsoft\Cryptography`), 0, syscall.KEY_READ, &h)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer syscall.RegCloseKey(h)
|
||||||
|
|
||||||
|
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
|
||||||
|
err = syscall.RegQueryValueEx(h, syscall.StringToUTF16Ptr(`MachineGuid`), nil, &valType, (*byte)(unsafe.Pointer(®Buf[0])), &bufLen)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
hostID := syscall.UTF16ToString(regBuf[:])
|
||||||
|
hostIDLen := len(hostID)
|
||||||
|
if hostIDLen != uuidLen {
|
||||||
|
return "", fmt.Errorf("HostID incorrect: %q\n", hostID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return hostID, nil
|
||||||
|
}
|
||||||
|
|
||||||
func GetOSInfo() (Win32_OperatingSystem, error) {
|
func GetOSInfo() (Win32_OperatingSystem, error) {
|
||||||
var dst []Win32_OperatingSystem
|
var dst []Win32_OperatingSystem
|
||||||
q := wmi.CreateQuery(&dst, "")
|
q := wmi.CreateQuery(&dst, "")
|
||||||
|
@ -130,7 +175,6 @@ func PlatformInformation() (platform string, family string, version string, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Users() ([]UserStat, error) {
|
func Users() ([]UserStat, error) {
|
||||||
|
|
||||||
var ret []UserStat
|
var ret []UserStat
|
||||||
|
|
||||||
return ret, nil
|
return ret, nil
|
||||||
|
|
Loading…
Reference in New Issue