Skip to content

Commit

Permalink
Merge pull request #91 from client9/master
Browse files Browse the repository at this point in the history
#90 make hostinfo more robust
  • Loading branch information
shirou committed Oct 14, 2015
2 parents 6a274c3 + 99d93f9 commit 7f6e8da
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 38 deletions.
8 changes: 3 additions & 5 deletions host/host_darwin.go
Expand Up @@ -23,10 +23,9 @@ func HostInfo() (*HostInfoStat, error) {
}

hostname, err := os.Hostname()
if err != nil {
return ret, err
if err == nil {
ret.Hostname = hostname
}
ret.Hostname = hostname

platform, family, version, err := GetPlatformInformation()
if err == nil {
Expand All @@ -46,9 +45,8 @@ func HostInfo() (*HostInfoStat, error) {
v := strings.Replace(values[2], ",", "", 1)
t, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return ret, err
ret.Uptime = t
}
ret.Uptime = t
}

return ret, nil
Expand Down
10 changes: 4 additions & 6 deletions host/host_freebsd.go
Expand Up @@ -29,10 +29,9 @@ func HostInfo() (*HostInfoStat, error) {
}

hostname, err := os.Hostname()
if err != nil {
return ret, err
if err == nil {
ret.Hostname = hostname
}
ret.Hostname = hostname

platform, family, version, err := GetPlatformInformation()
if err == nil {
Expand All @@ -51,10 +50,9 @@ func HostInfo() (*HostInfoStat, error) {
// ex: { sec = 1392261637, usec = 627534 } Thu Feb 13 12:20:37 2014
v := strings.Replace(values[2], ",", "", 1)
t, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return ret, err
if err == nil {
ret.Uptime = t
}
ret.Uptime = t
}

return ret, nil
Expand Down
11 changes: 5 additions & 6 deletions host/host_linux.go
Expand Up @@ -26,14 +26,13 @@ type LSB struct {
}

func HostInfo() (*HostInfoStat, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
ret := &HostInfoStat{
OS: runtime.GOOS,
}

ret := &HostInfoStat{
Hostname: hostname,
OS: runtime.GOOS,
hostname, err := os.Hostname()
if err == nil {
ret.Hostname = hostname
}

platform, family, version, err := GetPlatformInformation()
Expand Down
39 changes: 18 additions & 21 deletions host/host_windows.go
Expand Up @@ -3,11 +3,11 @@
package host

import (
"os"
"fmt"
"time"
"os"
"runtime"
"strings"
"time"

"github.com/StackExchange/wmi"

Expand All @@ -17,42 +17,39 @@ import (

var (
procGetSystemTimeAsFileTime = common.Modkernel32.NewProc("GetSystemTimeAsFileTime")
osInfo *Win32_OperatingSystem
osInfo *Win32_OperatingSystem
)

type Win32_OperatingSystem struct {
Version string
Caption string
ProductType uint32
BuildNumber string
Version string
Caption string
ProductType uint32
BuildNumber string
LastBootUpTime time.Time
}

func HostInfo() (*HostInfoStat, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
ret := &HostInfoStat{
OS: runtime.GOOS,
}

ret := &HostInfoStat{
Hostname: hostname,
OS: runtime.GOOS,
hostname, err := os.Hostname()
if err == nil {
ret.Hostname = hostname
}

platform, family, version, err := GetPlatformInformation()
if err == nil {
ret.Platform = platform
ret.PlatformFamily = family
ret.PlatformVersion = version
} else {
return ret, err
}

ret.Uptime, err = BootTime()
if err != nil {
return ret, err
}

procs, err := process.Pids()
if err != nil {
return ret, err
Expand All @@ -70,9 +67,9 @@ func GetOSInfo() (Win32_OperatingSystem, error) {
if err != nil {
return Win32_OperatingSystem{}, err
}

osInfo = &dst[0]

return dst[0], nil
}

Expand All @@ -98,7 +95,7 @@ func GetPlatformInformation() (platform string, family string, version string, e

// Platform
platform = strings.Trim(osInfo.Caption, " ")

// PlatformFamily
switch osInfo.ProductType {
case 1:
Expand All @@ -108,7 +105,7 @@ func GetPlatformInformation() (platform string, family string, version string, e
case 3:
family = "Server"
}

// Platform Version
version = fmt.Sprintf("%s Build %s", osInfo.Version, osInfo.BuildNumber)

Expand Down

0 comments on commit 7f6e8da

Please sign in to comment.