mirror of
https://github.com/shirou/gopsutil.git
synced 2025-05-06 19:29:13 +08:00
Merge pull request #91 from client9/master
shirou/gopsutil#90 make hostinfo more robust
This commit is contained in:
commit
7f6e8da3d0
@ -23,10 +23,9 @@ func HostInfo() (*HostInfoStat, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
if err != nil {
|
if err == nil {
|
||||||
return ret, err
|
ret.Hostname = hostname
|
||||||
}
|
}
|
||||||
ret.Hostname = hostname
|
|
||||||
|
|
||||||
platform, family, version, err := GetPlatformInformation()
|
platform, family, version, err := GetPlatformInformation()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -46,9 +45,8 @@ func HostInfo() (*HostInfoStat, error) {
|
|||||||
v := strings.Replace(values[2], ",", "", 1)
|
v := strings.Replace(values[2], ",", "", 1)
|
||||||
t, err := strconv.ParseUint(v, 10, 64)
|
t, err := strconv.ParseUint(v, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ret, err
|
ret.Uptime = t
|
||||||
}
|
}
|
||||||
ret.Uptime = t
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret, nil
|
return ret, nil
|
||||||
|
@ -29,10 +29,9 @@ func HostInfo() (*HostInfoStat, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
if err != nil {
|
if err == nil {
|
||||||
return ret, err
|
ret.Hostname = hostname
|
||||||
}
|
}
|
||||||
ret.Hostname = hostname
|
|
||||||
|
|
||||||
platform, family, version, err := GetPlatformInformation()
|
platform, family, version, err := GetPlatformInformation()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -51,10 +50,9 @@ func HostInfo() (*HostInfoStat, error) {
|
|||||||
// ex: { sec = 1392261637, usec = 627534 } Thu Feb 13 12:20:37 2014
|
// ex: { sec = 1392261637, usec = 627534 } Thu Feb 13 12:20:37 2014
|
||||||
v := strings.Replace(values[2], ",", "", 1)
|
v := strings.Replace(values[2], ",", "", 1)
|
||||||
t, err := strconv.ParseUint(v, 10, 64)
|
t, err := strconv.ParseUint(v, 10, 64)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
return ret, err
|
ret.Uptime = t
|
||||||
}
|
}
|
||||||
ret.Uptime = t
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret, nil
|
return ret, nil
|
||||||
|
@ -26,14 +26,13 @@ type LSB struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func HostInfo() (*HostInfoStat, error) {
|
func HostInfo() (*HostInfoStat, error) {
|
||||||
hostname, err := os.Hostname()
|
ret := &HostInfoStat{
|
||||||
if err != nil {
|
OS: runtime.GOOS,
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret := &HostInfoStat{
|
hostname, err := os.Hostname()
|
||||||
Hostname: hostname,
|
if err == nil {
|
||||||
OS: runtime.GOOS,
|
ret.Hostname = hostname
|
||||||
}
|
}
|
||||||
|
|
||||||
platform, family, version, err := GetPlatformInformation()
|
platform, family, version, err := GetPlatformInformation()
|
||||||
|
@ -3,11 +3,11 @@
|
|||||||
package host
|
package host
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/StackExchange/wmi"
|
"github.com/StackExchange/wmi"
|
||||||
|
|
||||||
@ -17,26 +17,25 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
procGetSystemTimeAsFileTime = common.Modkernel32.NewProc("GetSystemTimeAsFileTime")
|
procGetSystemTimeAsFileTime = common.Modkernel32.NewProc("GetSystemTimeAsFileTime")
|
||||||
osInfo *Win32_OperatingSystem
|
osInfo *Win32_OperatingSystem
|
||||||
)
|
)
|
||||||
|
|
||||||
type Win32_OperatingSystem struct {
|
type Win32_OperatingSystem struct {
|
||||||
Version string
|
Version string
|
||||||
Caption string
|
Caption string
|
||||||
ProductType uint32
|
ProductType uint32
|
||||||
BuildNumber string
|
BuildNumber string
|
||||||
LastBootUpTime time.Time
|
LastBootUpTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func HostInfo() (*HostInfoStat, error) {
|
func HostInfo() (*HostInfoStat, error) {
|
||||||
hostname, err := os.Hostname()
|
ret := &HostInfoStat{
|
||||||
if err != nil {
|
OS: runtime.GOOS,
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret := &HostInfoStat{
|
hostname, err := os.Hostname()
|
||||||
Hostname: hostname,
|
if err == nil {
|
||||||
OS: runtime.GOOS,
|
ret.Hostname = hostname
|
||||||
}
|
}
|
||||||
|
|
||||||
platform, family, version, err := GetPlatformInformation()
|
platform, family, version, err := GetPlatformInformation()
|
||||||
@ -44,15 +43,13 @@ func HostInfo() (*HostInfoStat, error) {
|
|||||||
ret.Platform = platform
|
ret.Platform = platform
|
||||||
ret.PlatformFamily = family
|
ret.PlatformFamily = family
|
||||||
ret.PlatformVersion = version
|
ret.PlatformVersion = version
|
||||||
} else {
|
|
||||||
return ret, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret.Uptime, err = BootTime()
|
ret.Uptime, err = BootTime()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ret, err
|
return ret, err
|
||||||
}
|
}
|
||||||
|
|
||||||
procs, err := process.Pids()
|
procs, err := process.Pids()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ret, err
|
return ret, err
|
||||||
@ -70,9 +67,9 @@ func GetOSInfo() (Win32_OperatingSystem, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return Win32_OperatingSystem{}, err
|
return Win32_OperatingSystem{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
osInfo = &dst[0]
|
osInfo = &dst[0]
|
||||||
|
|
||||||
return dst[0], nil
|
return dst[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +95,7 @@ func GetPlatformInformation() (platform string, family string, version string, e
|
|||||||
|
|
||||||
// Platform
|
// Platform
|
||||||
platform = strings.Trim(osInfo.Caption, " ")
|
platform = strings.Trim(osInfo.Caption, " ")
|
||||||
|
|
||||||
// PlatformFamily
|
// PlatformFamily
|
||||||
switch osInfo.ProductType {
|
switch osInfo.ProductType {
|
||||||
case 1:
|
case 1:
|
||||||
@ -108,7 +105,7 @@ func GetPlatformInformation() (platform string, family string, version string, e
|
|||||||
case 3:
|
case 3:
|
||||||
family = "Server"
|
family = "Server"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Platform Version
|
// Platform Version
|
||||||
version = fmt.Sprintf("%s Build %s", osInfo.Version, osInfo.BuildNumber)
|
version = fmt.Sprintf("%s Build %s", osInfo.Version, osInfo.BuildNumber)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user