From 6a2bc5afe81d92762277bf18fd09d081ebc78c32 Mon Sep 17 00:00:00 2001 From: mayowa Date: Tue, 25 Aug 2015 22:08:35 -0700 Subject: [PATCH 1/6] removed l2CacheSize as its not present in all wmi databases --- .gitignore | 1 + cpu/cpu_windows.go | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 9204bd2..194eab8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *~ #* _obj +*.tmp \ No newline at end of file diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index 808bbb3..d0e93c0 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -15,7 +15,6 @@ import ( type Win32_Processor struct { LoadPercentage uint16 - L2CacheSize uint32 Family uint16 Manufacturer string Name string @@ -67,7 +66,6 @@ func CPUInfo() ([]CPUInfoStat, error) { cpu := CPUInfoStat{ CPU: int32(i), Family: fmt.Sprintf("%d", l.Family), - CacheSize: int32(l.L2CacheSize), VendorID: l.Manufacturer, ModelName: l.Name, Cores: int32(l.NumberOfLogicalProcessors), From 7889ce3e03c795d73fb0e9acdfd22adbe9f45d72 Mon Sep 17 00:00:00 2001 From: mayowa Date: Tue, 25 Aug 2015 22:21:10 -0700 Subject: [PATCH 2/6] changed ProccessorId type to *string, cause wmi sometimes returns nil for it --- cpu/cpu_windows.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index d0e93c0..d148ee4 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -19,7 +19,7 @@ type Win32_Processor struct { Manufacturer string Name string NumberOfLogicalProcessors uint32 - ProcessorId string + ProcessorId *string Stepping *string MaxClockSpeed uint32 } @@ -62,14 +62,17 @@ func CPUInfo() ([]CPUInfoStat, error) { if err != nil { return ret, err } - for i, l := range dst { + + procID := "" + + for i, l := range dst { cpu := CPUInfoStat{ CPU: int32(i), Family: fmt.Sprintf("%d", l.Family), VendorID: l.Manufacturer, ModelName: l.Name, Cores: int32(l.NumberOfLogicalProcessors), - PhysicalID: l.ProcessorId, + PhysicalID: procID, Mhz: float64(l.MaxClockSpeed), Flags: []string{}, } From 56d70ecc5ec21d49e6308f691876fd17e1c527d0 Mon Sep 17 00:00:00 2001 From: mayowa Date: Tue, 25 Aug 2015 23:03:25 -0700 Subject: [PATCH 3/6] implemented HostStatInfo.Platform* for windows --- host/host_windows.go | 59 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/host/host_windows.go b/host/host_windows.go index 052faf8..3f894aa 100644 --- a/host/host_windows.go +++ b/host/host_windows.go @@ -5,6 +5,8 @@ package host import ( "os" "time" + "runtime" + "strings" "github.com/StackExchange/wmi" @@ -14,9 +16,13 @@ import ( var ( procGetSystemTimeAsFileTime = common.Modkernel32.NewProc("GetSystemTimeAsFileTime") + osInfo *Win32_OperatingSystem ) type Win32_OperatingSystem struct { + Version string + Caption string + ProductType uint32 LastBootUpTime time.Time } @@ -27,11 +33,34 @@ func HostInfo() (*HostInfoStat, error) { return ret, err } - ret.Hostname = hostname - uptime, err := BootTime() - if err == nil { - ret.Uptime = uptime + _, err = GetOSInfo() + if err != nil { + return ret, err } + + ret.Hostname = hostname + ret.Uptime, err = BootTime() + if err != nil { + return ret, err + } + + // PlatformFamily + switch osInfo.ProductType { + case 1: + ret.PlatformFamily = "Desktop OS" + case 2: + ret.PlatformFamily = "Server OS (Domain Controller)" + case 3: + ret.PlatformFamily = "Server OS" + } + + // Platform + ret.Platform = strings.Trim(osInfo.Caption, " ") + + // Platform Version + ret.PlatformVersion = osInfo.Version + + ret.OS = runtime.GOOS procs, err := process.Pids() if err != nil { @@ -43,16 +72,28 @@ func HostInfo() (*HostInfoStat, error) { return ret, nil } -func BootTime() (uint64, error) { - now := time.Now() - +func GetOSInfo() (Win32_OperatingSystem, error) { var dst []Win32_OperatingSystem q := wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { - return 0, err + return Win32_OperatingSystem{}, err } - t := dst[0].LastBootUpTime.Local() + + 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() return uint64(now.Sub(t).Seconds()), nil } From ee43e655c203eee36db396d9c99daff3c9e47ffc Mon Sep 17 00:00:00 2001 From: mayowa Date: Tue, 25 Aug 2015 23:27:25 -0700 Subject: [PATCH 4/6] implemented GetPlatformInfo and refactored HostInfo() to use it --- host/host_windows.go | 64 +++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/host/host_windows.go b/host/host_windows.go index 3f894aa..6ad1314 100644 --- a/host/host_windows.go +++ b/host/host_windows.go @@ -27,41 +27,30 @@ type Win32_OperatingSystem struct { } func HostInfo() (*HostInfoStat, error) { - ret := &HostInfoStat{} hostname, err := os.Hostname() if err != nil { - return ret, err + return nil, err } - _, err = GetOSInfo() - if err != nil { - return ret, err + 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 } - ret.Hostname = hostname ret.Uptime, err = BootTime() if err != nil { return ret, err } - // PlatformFamily - switch osInfo.ProductType { - case 1: - ret.PlatformFamily = "Desktop OS" - case 2: - ret.PlatformFamily = "Server OS (Domain Controller)" - case 3: - ret.PlatformFamily = "Server OS" - } - - // Platform - ret.Platform = strings.Trim(osInfo.Caption, " ") - - // Platform Version - ret.PlatformVersion = osInfo.Version - - ret.OS = runtime.GOOS - procs, err := process.Pids() if err != nil { return ret, err @@ -97,6 +86,33 @@ func BootTime() (uint64, error) { return uint64(now.Sub(t).Seconds()), nil } +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: + family = "Desktop OS" + case 2: + family = "Server OS (Domain Controller)" + case 3: + family = "Server OS" + } + + // Platform Version + version = osInfo.Version + + return +} + func Users() ([]UserStat, error) { var ret []UserStat From 6fa0704dbfdef13d2ab81e19043a3118d6226a13 Mon Sep 17 00:00:00 2001 From: mayowa Date: Fri, 28 Aug 2015 06:56:05 +0100 Subject: [PATCH 5/6] fixed: ProcessorId doesnt return a value --- cpu/cpu_windows.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index d148ee4..d70237a 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -63,9 +63,14 @@ func CPUInfo() ([]CPUInfoStat, error) { return ret, err } - procID := "" - + var procID string for i, l := range dst { + procID = "" + if l.ProcessorId != nil { + procID = *l.ProcessorId + } + + cpu := CPUInfoStat{ CPU: int32(i), Family: fmt.Sprintf("%d", l.Family), From bf16d0a6a7a797d8aa46804925ce3b2a65306bb1 Mon Sep 17 00:00:00 2001 From: mayowa Date: Fri, 28 Aug 2015 07:39:20 +0100 Subject: [PATCH 6/6] modified PlatformVersion and PlatformFamily string to (somewhat) match the output from sysinfo --- host/host_windows.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/host/host_windows.go b/host/host_windows.go index 6ad1314..4e1d44f 100644 --- a/host/host_windows.go +++ b/host/host_windows.go @@ -4,6 +4,7 @@ package host import ( "os" + "fmt" "time" "runtime" "strings" @@ -23,6 +24,7 @@ type Win32_OperatingSystem struct { Version string Caption string ProductType uint32 + BuildNumber string LastBootUpTime time.Time } @@ -100,15 +102,15 @@ func GetPlatformInformation() (platform string, family string, version string, e // PlatformFamily switch osInfo.ProductType { case 1: - family = "Desktop OS" + family = "Standalone Workstation" case 2: - family = "Server OS (Domain Controller)" + family = "Server (Domain Controller)" case 3: - family = "Server OS" + family = "Server" } // Platform Version - version = osInfo.Version + version = fmt.Sprintf("%s Build %s", osInfo.Version, osInfo.BuildNumber) return }