github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/system/host/host.go (about)

     1  package host
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"github.com/isyscore/isc-gobase/system/common"
     7  	"os"
     8  	"runtime"
     9  	"time"
    10  )
    11  
    12  var invoke common.Invoker = common.Invoke{}
    13  
    14  // InfoStat A HostInfoStat describes the host status.
    15  // This is not in the psutil but it useful.
    16  type InfoStat struct {
    17  	Hostname             string `json:"hostname"`
    18  	Uptime               uint64 `json:"uptime"`
    19  	BootTime             uint64 `json:"bootTime"`
    20  	Procs                uint64 `json:"procs"`           // number of processes
    21  	OS                   string `json:"os"`              // ex: freebsd, linux
    22  	Platform             string `json:"platform"`        // ex: ubuntu, linuxmint
    23  	PlatformFamily       string `json:"platformFamily"`  // ex: debian, rhel
    24  	PlatformVersion      string `json:"platformVersion"` // version of the complete OS
    25  	KernelVersion        string `json:"kernelVersion"`   // version of the OS kernel (if available)
    26  	KernelArch           string `json:"kernelArch"`      // native cpu architecture queried at runtime, as returned by `uname -m` or empty string in case of error
    27  	VirtualizationSystem string `json:"virtualizationSystem"`
    28  	VirtualizationRole   string `json:"virtualizationRole"` // guest or host
    29  	HostID               string `json:"hostid"`             // ex: uuid
    30  }
    31  
    32  type UserStat struct {
    33  	User     string `json:"user"`
    34  	Terminal string `json:"terminal"`
    35  	Host     string `json:"host"`
    36  	Started  int    `json:"started"`
    37  }
    38  
    39  type TemperatureStat struct {
    40  	SensorKey   string  `json:"sensorKey"`
    41  	Temperature float64 `json:"sensorTemperature"`
    42  }
    43  
    44  func (h InfoStat) String() string {
    45  	s, _ := json.Marshal(h)
    46  	return string(s)
    47  }
    48  
    49  func (u UserStat) String() string {
    50  	s, _ := json.Marshal(u)
    51  	return string(s)
    52  }
    53  
    54  func (t TemperatureStat) String() string {
    55  	s, _ := json.Marshal(t)
    56  	return string(s)
    57  }
    58  
    59  func Info() (*InfoStat, error) {
    60  	return InfoWithContext(context.Background())
    61  }
    62  
    63  func InfoWithContext(ctx context.Context) (*InfoStat, error) {
    64  	var err error
    65  	ret := &InfoStat{
    66  		OS: runtime.GOOS,
    67  	}
    68  
    69  	ret.Hostname, err = os.Hostname()
    70  	if err != nil && err != common.ErrNotImplementedError {
    71  		return nil, err
    72  	}
    73  
    74  	ret.Platform, ret.PlatformFamily, ret.PlatformVersion, err = PlatformInformationWithContext(ctx)
    75  	if err != nil && err != common.ErrNotImplementedError {
    76  		return nil, err
    77  	}
    78  
    79  	ret.KernelVersion, err = KernelVersionWithContext(ctx)
    80  	if err != nil && err != common.ErrNotImplementedError {
    81  		return nil, err
    82  	}
    83  
    84  	ret.KernelArch, err = KernelArch()
    85  	if err != nil && err != common.ErrNotImplementedError {
    86  		return nil, err
    87  	}
    88  
    89  	ret.VirtualizationSystem, ret.VirtualizationRole, err = VirtualizationWithContext(ctx)
    90  	if err != nil && err != common.ErrNotImplementedError {
    91  		return nil, err
    92  	}
    93  
    94  	ret.BootTime, err = BootTimeWithContext(ctx)
    95  	if err != nil && err != common.ErrNotImplementedError {
    96  		return nil, err
    97  	}
    98  
    99  	ret.Uptime, err = UptimeWithContext(ctx)
   100  	if err != nil && err != common.ErrNotImplementedError {
   101  		return nil, err
   102  	}
   103  
   104  	ret.Procs, err = numProcs(ctx)
   105  	if err != nil && err != common.ErrNotImplementedError {
   106  		return nil, err
   107  	}
   108  
   109  	ret.HostID, err = HostIDWithContext(ctx)
   110  	if err != nil && err != common.ErrNotImplementedError {
   111  		return nil, err
   112  	}
   113  
   114  	return ret, nil
   115  }
   116  
   117  // BootTime returns the system boot time expressed in seconds since the epoch.
   118  func BootTime() (uint64, error) {
   119  	return BootTimeWithContext(context.Background())
   120  }
   121  
   122  func Uptime() (uint64, error) {
   123  	return UptimeWithContext(context.Background())
   124  }
   125  
   126  func Users() ([]UserStat, error) {
   127  	return UsersWithContext(context.Background())
   128  }
   129  
   130  func PlatformInformation() (string, string, string, error) {
   131  	return PlatformInformationWithContext(context.Background())
   132  }
   133  
   134  // HostID returns the unique host ID provided by the OS.
   135  func HostID() (string, error) {
   136  	return HostIDWithContext(context.Background())
   137  }
   138  
   139  func Virtualization() (string, string, error) {
   140  	return VirtualizationWithContext(context.Background())
   141  }
   142  
   143  func KernelVersion() (string, error) {
   144  	return KernelVersionWithContext(context.Background())
   145  }
   146  
   147  func SensorsTemperatures() ([]TemperatureStat, error) {
   148  	return SensorsTemperaturesWithContext(context.Background())
   149  }
   150  
   151  func timeSince(ts uint64) uint64 {
   152  	return uint64(time.Now().Unix()) - ts
   153  }