Skip to content

Commit

Permalink
Refactor "host" package
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Vinogradov committed Sep 11, 2020
1 parent 8a625ec commit 5209442
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 573 deletions.
100 changes: 100 additions & 0 deletions host/host.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package host

import (
"context"
"encoding/json"
"os"
"runtime"
"time"

"github.com/shirou/gopsutil/internal/common"
)
Expand Down Expand Up @@ -52,3 +56,99 @@ func (t TemperatureStat) String() string {
s, _ := json.Marshal(t)
return string(s)
}

func Info() (*InfoStat, error) {
return InfoWithContext(context.Background())
}

func InfoWithContext(ctx context.Context) (*InfoStat, error) {
var err error
ret := &InfoStat{
OS: runtime.GOOS,
}

ret.Hostname, err = os.Hostname()
if err != nil && err != common.ErrNotImplementedError {
return nil, err
}

ret.Platform, ret.PlatformFamily, ret.PlatformVersion, err = PlatformInformationWithContext(ctx)
if err != nil && err != common.ErrNotImplementedError {
return nil, err
}

ret.KernelVersion, err = KernelVersionWithContext(ctx)
if err != nil && err != common.ErrNotImplementedError {
return nil, err
}

ret.KernelArch, err = KernelArch()
if err != nil && err != common.ErrNotImplementedError {
return nil, err
}

ret.VirtualizationSystem, ret.VirtualizationRole, err = VirtualizationWithContext(ctx)
if err != nil && err != common.ErrNotImplementedError {
return nil, err
}

ret.BootTime, err = BootTimeWithContext(ctx)
if err != nil && err != common.ErrNotImplementedError {
return nil, err
}

ret.Uptime, err = UptimeWithContext(ctx)
if err != nil && err != common.ErrNotImplementedError {
return nil, err
}

ret.Procs, err = numProcs(ctx)
if err != nil && err != common.ErrNotImplementedError {
return nil, err
}

ret.HostID, err = HostIDWithContext(ctx)
if err != nil && err != common.ErrNotImplementedError {
return nil, err
}

return ret, nil
}

// BootTime returns the system boot time expressed in seconds since the epoch.
func BootTime() (uint64, error) {
return BootTimeWithContext(context.Background())
}

func Uptime() (uint64, error) {
return UptimeWithContext(context.Background())
}

func Users() ([]UserStat, error) {
return UsersWithContext(context.Background())
}

func PlatformInformation() (string, string, string, error) {
return PlatformInformationWithContext(context.Background())
}

// HostID returns the unique host ID provided by the OS.
func HostID() (string, error) {
return HostIDWithContext(context.Background())
}

func Virtualization() (string, string, error) {
return VirtualizationWithContext(context.Background())
}

func KernelVersion() (string, error) {
return KernelVersionWithContext(context.Background())
}

func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
}

func timeSince(ts uint64) uint64 {
return uint64(time.Now().Unix()) - ts
}
15 changes: 1 addition & 14 deletions host/host_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ package host
import (
"context"
"sync/atomic"
"time"

"golang.org/x/sys/unix"
)

// cachedBootTime must be accessed via atomic.Load/StoreUint64
var cachedBootTime uint64

func BootTime() (uint64, error) {
return BootTimeWithContext(context.Background())
}

func BootTimeWithContext(ctx context.Context) (uint64, error) {
t := atomic.LoadUint64(&cachedBootTime)
if t != 0 {
Expand All @@ -32,18 +27,10 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) {
return uint64(tv.Sec), nil
}

func uptime(boot uint64) uint64 {
return uint64(time.Now().Unix()) - boot
}

func Uptime() (uint64, error) {
return UptimeWithContext(context.Background())
}

func UptimeWithContext(ctx context.Context) (uint64, error) {
boot, err := BootTimeWithContext(ctx)
if err != nil {
return 0, err
}
return uptime(boot), nil
return timeSince(boot), nil
}
78 changes: 10 additions & 68 deletions host/host_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
"unsafe"

Expand All @@ -21,65 +20,20 @@ import (
// from utmpx.h
const USER_PROCESS = 7

func Info() (*InfoStat, error) {
return InfoWithContext(context.Background())
}

func InfoWithContext(ctx context.Context) (*InfoStat, error) {
ret := &InfoStat{
OS: runtime.GOOS,
PlatformFamily: "darwin",
}

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

kernelVersion, err := KernelVersionWithContext(ctx)
if err == nil {
ret.KernelVersion = kernelVersion
}

kernelArch, err := kernelArch()
if err == nil {
ret.KernelArch = kernelArch
}

platform, family, pver, err := PlatformInformation()
if err == nil {
ret.Platform = platform
ret.PlatformFamily = family
ret.PlatformVersion = pver
}

system, role, err := Virtualization()
if err == nil {
ret.VirtualizationSystem = system
ret.VirtualizationRole = role
}

boot, err := BootTime()
if err == nil {
ret.BootTime = boot
ret.Uptime = uptime(boot)
}

procs, err := process.Pids()
if err == nil {
ret.Procs = uint64(len(procs))
}

func HostIDWithContext(ctx context.Context) (string, error) {
uuid, err := unix.Sysctl("kern.uuid")
if err == nil && uuid != "" {
ret.HostID = strings.ToLower(uuid)
if err != nil {
return "", err
}

return ret, nil
return strings.ToLower(uuid), err
}

func Users() ([]UserStat, error) {
return UsersWithContext(context.Background())
func numProcs(ctx context.Context) (uint64, error) {
procs, err := process.PidsWithContext(ctx)
if err != nil {
return 0, err
}
return uint64(len(procs)), nil
}

func UsersWithContext(ctx context.Context) ([]UserStat, error) {
Expand Down Expand Up @@ -126,10 +80,6 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {

}

func PlatformInformation() (string, string, string, error) {
return PlatformInformationWithContext(context.Background())
}

func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
platform := ""
family := ""
Expand Down Expand Up @@ -163,18 +113,10 @@ func PlatformInformationWithContext(ctx context.Context) (string, string, string
return platform, family, pver, nil
}

func Virtualization() (string, string, error) {
return VirtualizationWithContext(context.Background())
}

func VirtualizationWithContext(ctx context.Context) (string, string, error) {
return "", "", common.ErrNotImplementedError
}

func KernelVersion() (string, error) {
return KernelVersionWithContext(context.Background())
}

func KernelVersionWithContext(ctx context.Context) (string, error) {
version, err := unix.Sysctl("kern.osrelease")
return strings.ToLower(version), err
Expand Down
4 changes: 0 additions & 4 deletions host/host_darwin_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ package host
import "C"
import "context"

func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
}

func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
temperatureKeys := []string{
C.AMBIENT_AIR_0,
Expand Down
4 changes: 0 additions & 4 deletions host/host_darwin_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"github.com/shirou/gopsutil/internal/common"
)

func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
}

func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
return []TemperatureStat{}, common.ErrNotImplementedError
}
40 changes: 12 additions & 28 deletions host/host_fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,42 @@ import (
"github.com/shirou/gopsutil/internal/common"
)

func Info() (*InfoStat, error) {
return InfoWithContext(context.Background())
}

func InfoWithContext(ctx context.Context) (*InfoStat, error) {
return nil, common.ErrNotImplementedError
func HostIDWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func BootTime() (uint64, error) {
return BootTimeWithContext(context.Background())
func numProcs(ctx context.Context) (uint64, error) {
return 0, common.ErrNotImplementedError
}

func BootTimeWithContext(ctx context.Context) (uint64, error) {
return 0, common.ErrNotImplementedError
}

func Uptime() (uint64, error) {
return UptimeWithContext(context.Background())
}

func UptimeWithContext(ctx context.Context) (uint64, error) {
return 0, common.ErrNotImplementedError
}

func Users() ([]UserStat, error) {
return UsersWithContext(context.Background())
}

func UsersWithContext(ctx context.Context) ([]UserStat, error) {
return []UserStat{}, common.ErrNotImplementedError
}

func Virtualization() (string, string, error) {
return VirtualizationWithContext(context.Background())
}

func VirtualizationWithContext(ctx context.Context) (string, string, error) {
return "", "", common.ErrNotImplementedError
}

func KernelVersion() (string, error) {
return KernelVersionWithContext(context.Background())
}

func KernelVersionWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func PlatformInformation() (string, string, string, error) {
return PlatformInformationWithContext(context.Background())
}

func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
return "", "", "", common.ErrNotImplementedError
}

func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
return []TemperatureStat{}, common.ErrNotImplementedError
}

func KernelArch() (string, error) {
return "", common.ErrNotImplementedError
}
Loading

0 comments on commit 5209442

Please sign in to comment.